Created
March 25, 2013 18:49
-
-
Save dhcole/5239548 to your computer and use it in GitHub Desktop.
Make links on page bookmarkable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// loop through the node list to get the innerHTML from each node | |
// copy that as the id for the node | |
var addId = function(list){ | |
for (i = 0; i < list.length; i++) { | |
element = list[i]; | |
sectionCopy = element.innerHTML.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,''); //substitute space with dash | |
element.id = sectionCopy; | |
} | |
}; | |
var allH3 = document.getElementsByTagName('h3'); | |
addId(allH3); | |
var allH2 = document.getElementsByTagName('h2'); | |
addId(allH2); | |
// make the titles linkable | |
$('h2[id]','.main').attr('title', 'Link to this section').click(function(e) { | |
// chapterName = $('h2[id]','.main')[0].id; | |
window.location.hash = $(e.currentTarget).attr('id'); | |
}); | |
$('h3[id]','.main').attr('title', 'Link to this section').click(function(e) { | |
// chapterName = $('h2[id]','.main')[0].id; | |
window.location.hash = $(e.currentTarget).attr('id'); | |
}); | |
// mark image captions | |
var caption = $('img').next().addClass('caption'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment