The web doesn't have any good way of bookmarking any spot in a web page. With some help from the web page author, we can bookmark a specific anchor or node id in the page, but most particular spots are still not reachable for bookmarks. I just tossed up a little user script that makes any node in the page addressable by an XPath query bookmarkable. It's mostly for XPath power users, for now, but works well (and lets you load bookmarks using that technique, which you might have gotten from such people).
The source code (
install from here) is extremely short:
var path, node, hash = decodeURIComponent( location.hash||'' );
if( (path = /^#xpath:(.+)/.exec( hash )) &&
(node = $X( path[1] )) )
node.scrollIntoView();
function $X( xpath ) {
return document.evaluate( xpath, document, null, 0, null ).iterateNext();
}
Having installed that, you can load bookmarks like
http://tibet.dharmakara.net/TibetABC.html#xpath:/html/body/h2[3], and get zoomed in to the right part of the page immediately (here, the part featuring how the Tibetan numbers are spelled, what they look like, and approximately how to pronounce them, for us westeners).
Hi, I really like the idea.
ReplyDeleteI've often thought that it'd be nice if there was some standard way of mixing xpath into a uri to make it point to a specific element in a document - it would be especially useful in Piggbank/Solvent type situations. Is it a valid uri, and is it distinguishable from a url that might happen to have that fragment identifier?
It's a valid uri, and in theory, you could say it's an invalid anchor, as (at least according to the XHTML 1.0 Strict DTD) /, for instance, is not a valid part of an <a name=""> anchor name attribute. Ergo: yes.
ReplyDeleteMore interestingly though, it's very, very unlikely people would use such anchors themselves, except perhaps as inspired by this post. And then, more likely for programmatic purposes IMO.
Please take a look at the W3C’s XPointer (supported in Firefox), and don’t invent something that is custom :).
ReplyDeleteI'll happily err on the side of pragmatism; the XPointer framework seems to have stuck its head in the clouds in not being defined neother for non-XML doctypes nor non-well-formed documents, and messy HTML being the norm on the web today, I don't see how XPointer would be the more useful approach at present. Quote:
ReplyDelete"The [XPointer] framework is intended to be used as a basis for fragment identifiers for any resource whose Internet media type is one of text/xml, application/xml, text/xml-external-parsed-entity, or application/xml-external-parsed-entity."
The XPath bookmarks defined above quite purposefully neither comply with nor water out the XPointer standard, as they would if they did.
Question back: could you give an XPointer example link that Firefox supports and manages to resolve, or any more detail of what you mean by "supports", in summary or reference? this XPointer link does not seem to be supported by my Firefox in any useful fashion, anyway. That page is arguably "just" well-formed application/xhtml+xml though, so it might not be a proper example of anything but how limiting the XPointer framework scope is (when really anally interpreted by the letter, anyway).