window.location hash

I have been developing for quite a while now (6 years at the time of writing), and it never ceases to amaze me the amount of things left to learn. I thought that I had learned all that I needed to from the DOM World and today I was thrown right back in with something I had never had the need to use before.

I was working on a page that has a javascript tab system. The downside of tab systems like that is they do not maintain state. When you trigger a full page reload you need some way to determine what tab you should show. I figured an easy way would be to detect an anchor reference in the URL. This actually proved to be a fairly simple task.

$(function() {
	var anchor = window.location.hash;

	if (anchor) {
		// Code for tab activation here...
	}
});

To me, this is pretty cool. I thought I would need to come up with some regular expression to match the URL anchor reference. Just goes to show that no matter how long you work with something you always have room for improvement.

Also, I would like to point out how invaluable our developer tools are. I stumbled upon this method while playing around in the Chrome Javascript Console. It has the helpful feature of showing all methods and properties off of an object is has built, very very handy.

Categories

javascript
browser
dom

Comments