Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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