views:

857

answers:

4

Hello,

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

A: 

In my code I use this:

 loc=window.location.href.split('#');

and hash is in loc[1].

Edit: Then, to save it, just put it back into href:

 window.location.href='#somestring';
Thinker
I'm trying to change the hash, not read it back out
Cameron
Sorry, I updated my post.
Thinker
Why not just use the location.hash property? That's what it's for, and as it's been around since Netscape Navigator 2 (1995) you can bet it's supported everywhere.
NickFitz
A: 

Setting window.location.hash works fine in IE6 & IE7.

In some occasions, reading window.location.hash under IE6 right after a set will return the old value, but the browser has set the hash successfully. An example:

alert(window.location.hash);
window.location.hash = '#newHash';

/* Sometimes, it will return the old value,
   I haven't figured out why it does that, and
   it's rather rare. */
alert(window.location.hash);

If you are just using it to set it, you shouldn't run into any problems.

Andrew Moore
+1  A: 

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).

NickFitz
Thanks.I was avoiding the add() method on purpose because I foolishly wanted to avoid setting a history point, but this turns out to be impossible
Cameron
A: 

All "modern" (a.k.a A-Graded) browsers allow to set hash and do not reload the page when doing so. The ones that do reload the page are some of the older ones, such as Safari 2.0.4 and Opera 8.5x.

See this post where I explain it in a bit more detail.

Also note, that HTML5 finally specifies that hash setter should change actual hash but not reload the page.

kangax