Potential problems setting window.location.hash

Cameron picture Cameron · Aug 12, 2009 · Viewed 30.8k times · Source

I have some javascript code which, at one point, sets window.location.hash to a specific string. This works fine in Firefox 3, but I want to know if I will run into problems with this later, i.e. is this a cross-browser solution (IE6 included)?

Also, I am using ReallySimpleHistory. Will this mess up its internal state?

Thanks

Answer

NickFitz picture NickFitz · Aug 12, 2009

window.location.hash has been around since JavaScript was introduced in Netscape Navigator 2 back in 1995. It was first supported by Microsoft in Internet Explorer 3 in 1996. I think you can be reasonably certain that every JS-capable browser supports it.

From a quick glance through the source, it looks as if ReallySimpleHistory makes pretty extensive use of this property, so you may well break it. You might want to use its add(newLocation) method instead (which works by setting window.location.hash).