First, don't use location.hash = "#foo" to jump to "foo" link, because Safari will jump to %23foo. Safari's behaviour seems very logic, but I don't really know who is right, Safari or the IE / Firefox crowd.
Second, don't use
<a h ref="javascript:void(foo())" >foo</a>
You should really do
<a h ref="something.html" onclick="foo(); return false">foo</a>
For the purist out there, yeah, that's not exactly equivalent, onclick="try { foo() } finally { return false } gets you closer, but the value of this will still be different in the two foo's.
Putting javascript:void(foo()) in the href turns out to produce buggy results, because once a user clicks in a link, and Internet Explorer engine passes onclick's or any other guard and reaches href, then it enters in a different state, that only quits when the new page loads (a href to an anchor is of course an exception).
In this new state, animated gif images stop their animation and Bad Things Happen™. Among others, expect your javascript code to fail in subtle ways. Explorer is not expecting to execute javascript code after you click on a link. It's already hard enough to make it behave correctly on its natural state, so trying to do something useful after a successful click on a link is roulette.
P.S.: WordPress 2.0 is nice and all, but it's html editor is a pain. I had to put the above html code in a pre tag, using entities, and breaking h ref (like here) because otherwise it changes it!
The obvious come-back question is: if not location.hash = “#foo”, then what should one use to set the location hash to alter page URL without forcing a page reload?
ReplyDeleteI forgot to put in the correct way to do it:
ReplyDeletelocation.hash = "foo"
the point is that explorer / firefox throw away the first # if it exists, Safari encodes it and makes it part of the hash.
Oh, now I feel a bit silly, having known (and always done) that all along. :-) Well, thanks for mentioning it, anyway; it's also good to know why such code should always be fixed when encountered.
ReplyDeleteVery thans... Now it work :D
ReplyDelete