IE Redirect Bug with Dynamic Location Hash

I discovered the most obscure bug today in IE. For those of you paying attention, this bug is the reason I haven’t been updating — it ate up all my god-damned time. People who aren’t programmers can stop reading here.

What Happens

The browser is redirected when it shouldn’t be after modifying the URL hash (the stuff after #).

Scope

The bug exists on IE7, possibly 6 (why not, right?).

Steps to Reproduce

Assume you are on page A and want to redirect to page B.

  • Go to page A.
  • From page A, do a header redirect to page B in PHP/ASP/whatever. As in, header(‘location: $pageB’);
  • On page B, using JavaScript, modify the document.location.hash variable.

What Should Happen

The anchor text in the address bar should change. As in, http://www.michiknows.com/#someanchor changes to http://www.michiknows.com/#newanchor. This should happen without the page refreshing.

What Actually Happens

The browser refreshes. @#%!*(&$!

Solution / Fixes

On page A, rather than redirect using headers, use JavaScript:

<script>
// if page A was http://www.michiknows.com
document.location.href = 'http://www.michiknows.com/';
</script>
<a href="http://www.michiknows.com">go to page A</a>

For some dumb reason, this fixes the problem.

Damn you, Microsoft!

8 Comments

  1. Bryce says:

    Thanks. This just solved a bug I’ve been trying to fix for 3 days.

  2. jim says:

    i found a related bug.

    in server side a.aspx :

    Response.Redirect(“test.aspx?v=123#a”);

    in test.aspx:

    Request["url"] is “123#a”

    but Request["url"] should be “123″

    it only exists on IE6. IE7 and Firefox are OK.

  3. jim says:

    i found a related bug.

    in server side a.aspx :

    Response.Redirect(“test.aspx?v=123#a”);

    in test.aspx:

    Request["v"] is “123#a”

    but Request["v"] should be “123″

    it only exists on IE6. IE7 and Firefox are OK.

  4. ethaniel says:

    Dude,thanks alot. I wasted several days trying to figure this out.

  5. ryan says:

    you can add IE8 to this too

  6. scinos says:

    I’ve found a related bug in IE8 (maybe in IE7, not in IE6).

    Sometimes, this code displays the alert:

    var a = ;
    document.location.hash=a;
    if (document.location.hash != a) {
    alert (“FAIL!”);
    }

    In that case, the page also get redirected to document.location.href without a hash. Very weird.

  7. scinos says:

    Sorry, my previous comment has a typo. It should read “var a = ‘something’;”

  8. smccabe says:

    Adding my thanks, this bug was owning me for a while until I found your article. Stupid IE!

Leave a Reply