jQuery anchor detection

Here’s a snippet to catch the anchor # in url’s with jQuery and do something with it. I needed this for ultra simple jQuery tabs.

$(document).ready(function() {
	// for page reload
	if(window.location.hash.length > 1) {
		// hash is found, similate click
		$('.tabs a[rel=' +window.location.hash.substring(1)+ ']').click();
	}
});
 
// dynamic change
$(window).bind('hashchange', function() {
	// hash is found, similate click
	$('.tabs a[rel=' +window.location.hash.substring(1)+ ']').click();
});

Freelance webdeveloper & Entreprenerd Specialized in developing, scaling and optimizing of succesful web applications, community websites and small/corporate business websites using technologies such as PHP 5, Laravel, HTML5, jQuery, .. Trying to find optimal Google love for every website. I'm always interested and open for new business opportunities.