Moves backward and upward in the DOM to find the closest node with an ID attribute (i.e. one which can be targeted in the URL fragment). If it finds one, updates the document URL to include that fragment.
The main function is parameterized to allow the maximum search depth to be limited. The depth defaults to 2; passing -1 will give an unlimited search depth.
Useful for bookmarklets.
Tested on Chrome and Chrome only.
Doesn't rely on any external frameworks.
Started life as gist #489116.
- Create a Makefile of some variety that spits out minified text suitable for a bookmarklet
- Write some tests
Changed again; now it will only recurse to a depth of 2. I think this is usually what I want: it will be pretty much guaranteed to find IDs in headings but not necessarily in sub-paragraphs. It's all pretty vague and based on the structure of the individual document. Passing a value of -1 in the function call at the end will cause it to do a thorough search of the document for the absolute nearest previous ID. Passing 0 will cause it to ignore IDs in any subnodes..