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!

Be Sociable, Share!
    • Bryce

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

    • http://www.meetao.cn jim

      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.

    • http://www.meetao.cn jim

      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.

    • http://monto.ru/ ethaniel

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

    • ryan

      you can add IE8 to this too

    • scinos

      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.

    • scinos

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

    • smccabe

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

    • Max

      Hi,

      have you ever tried to think about one of your first html lines could be the reason for this bug?!
      I have spend some hours debugging this issue and figured out that it has to do with the encoding meta tag. The utf-8 / utf-16 encoding messes up in this tricky constellation.

      window.location.hash = '';

      There are 3 solutions so far:
      1. you change the charset=utf-8 to iso-8859-1 or utf-32 on your static page
      2. you are on the server side and set the header(‘Content-type: text/html; charset=utf-8)
      3. the smoothest solution for me is on Apache: AddDefaultCharset UTF-8 (there is an IIS alternative as well)

      Don’t ask me, ask IE

      • Max

        see post before, the blog removed the code section…

        html
        head
        meta http-equiv=”content-type” content=”text/html;charset=utf-8″
        /head
        body
        script
        window.location.hash = “”;
        /script
        /body
        /html

      • Max

        and the redirect ;)
        Redirect 301 /test/ /test/test.htm#123