Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sunny
Created July 28, 2011 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny/1111349 to your computer and use it in GitHub Desktop.
Save sunny/1111349 to your computer and use it in GitHub Desktop.
JavaScript that scrolls down on page load to `<h3>Important</h3>` if you go to `foo.html#Important`
// Scroll on page load to the first header where the text matches the hash in the URL
// For example going to 'foo.html#Important' will scroll down to '<h3>Important</h3>'
$(function() {
var h = window.location.hash.replace('#', '')
if ($(h).length)
$('h2,h3,h4,h5,h6').each(function() {
if ($.trim($(this).text().replace(/\s+/g, ' ')) == h)
return window.scrollTo(this.offsetLeft, this.offsetTop)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment