I ran across something new (at least, new to me) today. Using
window.location.hash, if the same value is being assigned to the hash, the browser looks at it as a non-operation. The value needs to be reset first before reassigning the same value (e.g. for the purpose of resetting the view to the top of a dynamic list). I’ve noticed this behavior in Firefox 3 and Safari 3+.
I rarely write about coding issues, but it might be a good way to catalog my findings for my own reference. My apologies to everyone else.
Update: I thought this could use an example.
var hashVar = 'content-top'; // ASSERT: hashVar contains the same value as window.location.hash window.location.hash = hashVar; // Expected behavior: Window scrolls back to the 'content-top' id // Demonstrated behavior: Window does nothing
The way that I solved this was to reset the window.location.hash value to an empty string before the reassignment.
var hashVar = 'content-top'; window.location.hash = 'adkcj'; window.location.hash = hashVar; // Expected behavior: Window scrolls back to the 'content-top' id // Demonstrated behavior: Window scrolls back to the 'content-top' id
Update 2: Apparently, Firefox, in the latest update, changed the behavior of the hash property. If the value is “zero-ed out”—if you will—the entire page refreshes. I’ve amended the script above so that hash is given a (hopefully) non-id value. Feel free to use a real word, but I didn’t want to take the off-chance of hitting an id I’m using.