views:

1119

answers:

6

I am using ajax to update the location of a page in a frame. But when setting the location of the hash (on Chrome and some versions of IE (5.5) specifically, but occasionally on IE7) the page is being reloaded.

The following html demonstrates the problem.

the main frame.... frame.html is

<html><head>
<frameset rows="*">
<frame src=sethash.html frameborder=0 scrolling=auto name=somebody>
</frameset>
</head></html>

the sethash.html page is .

<html><head>
<script language=JavaScript>
var Count = 0;
function sethash()
{  
  top.document.location.hash = "hash" + Count;  
  Count++;
}
</script>
</head>
<body onload="alert('loaded')">
<h1>Hello</h1>
<input type='button' onClick='sethash()' value='Set Hash'>
</body>
</html>`

On most browsers loading the frame.html will show the loaded alert once when the page is loaded. Then when the set hash button is pressed the url will be changed but the hash the loaded alert will not show again. On chrome and some versions of I.E

Microsoft report possibly the same problem with Internet Explorer 5.5 link text

I can't use the microsoft suggested solution, which is to capture the event and not fire it, but just scroll into view, as am using set the top.location.hash as part of the onLoad event.

A: 

I have the same problem.. it is annoying.. can't do hash url rewrite because of this bug in Safari..

+1  A: 

Webkit (and by extension, Chrome) behave strangely with location.hash. There are a few open bugs about it, the most relevant is probably this one: https://bugs.webkit.org/show_bug.cgi?id=24578 that documents your problem of having the page refresh when location.hash is changed. It looks like your best option right now is to cross your fingers and hope that it gets promptly fixed.

I can't reproduce the bug in IE7 though, and you're the first person in ages I've seen that supports IE5.5 so I can't really help you there ;)

dfltr
A: 

Have you tried creating hidden links to the hashes and firing the click even on them?

Jesse Brown
A: 

What about doing location.href = '#yourhash'; Maybe it bypasses the bug.

A: 

Hey!

I was having this problem as well. My situation allowed me to create a window unbind event that set the hash with the value I wanted it too as the user browser to the next page or refreshed. This worked for me, but not sure it will work for you.

With jquery, I did:

if($.browser.webkit){
    $(window).unload(function() {
        top.window.location.hash = hash_value;
    });
}else{
    top.window.location.hash = hash_value;
}

Technically you don't need to do the webkit check, but I thought for people who use firefox, it might be nice to see the correct hash value before they refresh the frameset.

-Jacob

Jacob Morrison
A: 

Hmm this is getting annoying...
The location.hash change causes my jQuery animation to stutter :( any progress?

ubunut