javascript location.hash refreshing in IE javascript location.hash refreshing in IE javascript javascript

javascript location.hash refreshing in IE


This appears to be a bug with Internet Explorer (tested with 7 and 8).

Changing window.location.hash should not result in a reload, and it is a common JavaScript technique to use the hash for maintaining state.

If you manually load a page and change the hash using JavaScript it will work.

The problem is when you are redirected to the page from a different location (ie: using HTTP header "Location"), then modifying the hash will result in a reload.

To get around this bug you could:

1) If you can control the redirect, you could replace the Location header with some HTML.

<html><head>    <meta http-equiv="refresh" content="0; url=__REDIRECT_LOCATION__">    <script>window.location = "__REDIRECT_LOCATION__";</script></head></html>

2) if not, you could try reloading the page when it is loaded. To prevent a reload loop you may need to set a cookie.

window.location = window.location; // window.location.reload() didn't work.In pseudo code: // if is Internet Explorer//      if ( cookie "reloadPerformed" is not set )//          set cookie "reloadPerformed" = "1"//          reload page//      else //          clear cookie "reloadPerformed"

The obviously drawback is that loading the page results in two page request & render, so you'll would want the reload to be one of the first things the page does when it loads.


@JarneCook seems to be right - it is a bug in IE.

You might be able to just do:

<script type="text/javascript">  window.location.hash = window.location.hash;</script>

at the top of your page. In normal circumstances, this should be a no-op, but if the user is using IE and has arrived via a redirect, the page will reload before they even notice it has loaded.


The problem is that "The hash is coming from else where in the web app via a redirect.".If you use javascript to redirect the url in the client like this:

location.href = 'test1.aspx#testhash'

it will be ok !

So this is the IE bug: When a web app via a redirect, the browser may only see the prev url, so when you modify the location.hash, the browser sees a url change, so refreshes the page.