+1  A: 

I am not sure I fully understand your needs, but I have used the Really Simple History library (http://code.google.com/p/reallysimplehistory/) for implementing something similar. You can see it here: http://whiteoak.sourceforge.net/

Itay
I'm after a light-weight way of simply keeping track of the hash value and supporting back/forward buttons, with the same behavior in all the commonly used browsers. RSH does not seem to be such a library, as it is over 9 kB when YUI-compressed and still depends on external libraries... My own library currently weighs in at 835 bytes compressed and works in IE6+, Firefox, Chrome and presumably Opera 8+ and Safari 3+ without dependencies... I'm mostly wondering if I'm missing any browsers and how to support them too.
Blixt
+3  A: 

Based on the effort that you've put into this, I would assume that you've seen YUI Browser History Manager, but just in case ...

They give a nice write up of their implementation, and I find their source code very readable.

Here's what it says about Opera

* location.hash is a bit buggy on Opera. I have seen instances where
* navigating the history using the back/forward buttons, and hence
* changing the URL, would not change location.hash. That's ok, the
* implementation of an equivalent is trivial ... more below

Searching the source I found some accomodations for Safari 1.x & 2.0 also. Seems like you'd be interested in it.

Hope that helps.

Keith Bentrup
Yup, a few pieces of my code is inspired by their implementation. I did see their Safari implementation but I couldn't find a simple way to get it to work, and would have to increase the size of my code quite considerably, which is why I'm wondering if Safari 2 and below is still used by enough people to warrant support.
Blixt
Safari 2 was at 0.5% in Jan of 2009 according to: http://www.w3counter.com/globalstats.php?date=2009-01-31. So 1 in 200 as of 6 months ago.
Keith Bentrup
Thanks Keith! I'll decide not to support Safari 2 then, because the code interferes with modern browsers unless browser sniffing is implemented... and that I dislike. =)
Blixt
+17  A: 

First of all, thanks to you guys who answered! =)

I've now done a lot more research and I believe I'm satisfied with my implementation. Here are the results of my research.

First of all, my finished Hash library. It's a stand-alone library with no dependencies. It has two functions: Hash.init(callback, iframe) and Hash.go(newHash). The callback function is called whenever the hash changes with the new hash as its first argument, and as its second argument a flag indicating whether the callback is called due to initial state (true) or an actual change to the hash (false).

Hash.js (MIT license)

I also made a jQuery plugin for making it easier to use. Adds a global hashchange event as well. See example in source code for how to use it.

jquery.hash.js (MIT license)

To see them in use, go to my JavaScript "realm" page:

Blixt's JavaScript realm

Internet Explorer 8

Smooooth cruisin'! Just smack on one o' them onhashchange events to the window object (using attachEvent) and get the location.hash value in the event handler.

It doesn't matter if the user clicks a link with a hash, or if you set the hash programmatically; history is kept perfectly.

Chrome, Firefox, Safari 3+, Opera 8+

Smooth cruisin'! Just poll for changes to the location.hash property using setInterval and a function.

History works perfectly. For Opera, I set history.navigationMode to 'compatible'. To be honest, I'm not sure what it does, I did it on recommendation from a comment in the YUI code.

Note: Opera needs some additional testing, but it has worked fine for me so far.

Surprise quirk bonus! (Can it be?!) It turns out that Firefox (only confirmed in 3.5+) decodes the location.hash property, so this can trigger a hashchange event twice (first for the encoded version then for the unencoded version.) There is a new version of my Hash.js library that takes this into account by using the location.href property instead (changes provided by Aaron Ogle.)

Internet Explorer 6, 7

Now it gets nastier. The navigation history in older Internet Explorer versions ignore hash changes. To work around this, the commonly accepted solution is to create an iframe and set its content to the new hash. This creates a new entry in the navigation history. When the user goes back, this changes the content of the iframe to its previous content. By detecting the change of content, you can get it and set it as the active hash.

Checking for changes to the location.hash property is still needed to manual changes to the current address. Beware of the quirks I've mentioned below, though.

While this solution seems to be the best one out there, it's still not perfect in Internet Explorer 6, which is a bit quirky about the back/forward buttons. Internet Explorer 7 works fine, though.

Surprise quirk bonus #1! In Internet Explorer 6, whenever there's a question mark in the hash, this gets extracted and put in the location.search property! It is removed from the location.hash property. If there is a real query string, however, location.search will contain that one instead, and you'll only be able to get the whole true hash by parsing the location.href property.

Surprise quirk bonus #2! If the location.search property is set, any subsequent # characters will be removed from the location.href (and location.hash) property. In Internet Explorer 6 this means that whenever there's a question mark in the path or the hash, you'll experience this quirk. In Internet Explorer 7, this quirk only occurs when there's a question mark in the path. Don't you just love the consistency in Internet Explorer?

Surprise quirk bonus #3! If another element on the page has the same id as the value of a hash, that hash will totally mess up the history. So rule of thumb is to avoid hashes with the same id as any elements on the page. If the hashes are generated dynamically and may collide with ids, consider using a prefix/suffix.

Other browsers

Unless you've got an out-of-the-ordinary user base, you won't need to support more browsers. The browsers not listed above are in the <1% usage category.

Blixt
"It's a stand-alone library with no dependencies." - Thank you! This is great.
casey
Obvious failing of this: hashchange fires when you click on a new link, iff the link has been created with your software (a plain 'a' is not sufficient, I believe, your code need to tamper with it), or if you visit from another page. It *doesn't* fire on browser back and forward. To do that, I think you need a timer to poll for changes, judging by other implementations.
ijw
thank you for sharing this !
Benoit
A: 

Hi, this code is really helpful to me in my current project. However, when I implement the example provided in the source code:

function handler(newHash) {
alert('Hash changed to "' + newHash + '"');
}
Hash.init(handler, document.getElementById('hidden-iframe'));
Hash.go('abc123');

I get an alert upon new hash value in FF, Opera and Chrome, but it doesn't seem to work for me in IE 8, 7, or 6. Do you have any idea as to why?

Thanks!!

Andrew
Maybe a dumb question, but does your page have an iframe with the ID "hidden-iframe"? If not, add one. Also, you may have to use a blank HTML page as the source for the iframe for it to work properly in IE 6 or something (can't remember...)
casey
A: 

I simply can't get this to work in IE7. It just doesn't set the hash value when calling the Hash.go function.

BTW you might want to change the name of the namespace from Hash to something else. Prototype also has an object with that name, so when used on the same page it crashes.

Patrick
Are you running the page from disk? IE7 enforces additional security limitations in that case. Try putting the page on a web server and then see if the problem persists. If it does, send me the link to the page and I'll have a look.
Blixt