Skip to content

Instantly share code, notes, and snippets.

@jphastings
Created July 15, 2011 05:50
Show Gist options
  • Save jphastings/1084153 to your computer and use it in GitHub Desktop.
Save jphastings/1084153 to your computer and use it in GitHub Desktop.
Adding hashtags to headings - as an example (useless in the real world!)

This bookmarklet demonstrates adding IDs to headings, so that places on a page can be linked to directly.

Bookmark the following address in a non-IE browser (tested in Firefox) and click it when you're browsing a page with headings in the hub.

Then hover over a heading, and double click to change the URL in your browser bar to the one which will go straight to that point.

Bookmarklet:

javascript:(function(){$('h1,h2,h3,h4,h5,h6').each(function(i,e){try{var%20hash=%22%3E%22+$(e).first().text().replace(/[^a-zA-Z\%20]/g,'').replace(/\%20/g,'_');$(e).attr('title','Double%20click%20to%20get%20the%20URL%20for%20this%20point%20on%20the%20page:%20%23'+hash);$(e).attr('id',hash);$(e).dblclick(function(){window.location.hash=hash;})}catch(e){}})})();
// This is the example Javascript code
$('h1,h2,h3,h4,h5,h6').each(function(i,e) {
try {
var hash = ">"+$(e).first().text().replace(/[^a-zA-Z\ ]/g,'').replace(/\ /g,'_');
$(e).attr('title','Double click to get the URL for this point on the page: #'+hash);
$(e).attr('id',hash);
$(e).dblclick(function() {
window.location.hash = hash;
})
} catch(e) {
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment