window.location = #anchor doesn't work in IE

24,464

Solution 1

I have this code in production and it works fine in IE7...

location.hash = "#top";

However, if you are just trying to scroll to the top, this ought to be a lot easier...

window.scrollTo(0, 0);

Solution 2

The location object is broken up into several properties - href is only one of them

Another one, hash, is what you're looking for.

top.location.hash = 'top';

You can also do this without using the location/href at all - just use scrollTo()

top.scrollTo( 0, 0 );

Solution 3

I also had a problem with windows.location.hash working in all browsers but IE7 and IE8 (at least on Vista). After much experimenting, I discovered that page redirection was breaking hash assignment.

An error will occur in IE7 or IE8 if you assign a value to windows.location.hash from within a page that was loaded as a result of redirection via the HTTP "Location" header.

After discovering this, I was able to find a fix elsewhere on StackOverflow (see here). The solution is to have the browser redirect via Javascript. Here I repost the solution from the other StackOverflow page:

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

This would explain why some people were having a problem with setting hash and some were not, but I do not know that the originator of the thread was redirecting.

I should also point out that I couldn't just use scrollTo() because my purpose was to remove the hash tag from the address bar without reloading the page, not to scroll.

Solution 4

location.href = location.href.split("#")[0] + "#top"

EDIT: to avoid the possibility of ever having two hashes.

Share:
24,464
Admin
Author by

Admin

Updated on August 14, 2020

Comments

  • Admin
    Admin almost 4 years

    On this map:

    http://web.pacific.edu/documents/marketing/campus-map/version%202/stockton-campus-2.0.htm

    I have an anchor at the top, and I want the page to jump to the anchor when a link is clicked.

    I'm currently using

    window.location = '#top';
    

    It works as expected in FF, Opera, and Chrome, but not in IE 7.

    I've tried all permutations like window.location.hash and window.location.assign() and also scrollIntoView(true) and focus().

    How can I make it work in IE?

    Edit: Nothing seems to work, which makes me think it's not the syntax, but something about the JS... here is the click event handler... could it be because it returns false? I'm grasping at straws.

    // Click handler for each location link
    $('#index a').click(function()
    {
        hideMarkers();
        location.href = location.href + "#top";
        var marker = showMarker( $(this).attr('data-id') );
        GEvent.trigger( marker, "click" );
        return false;
    });
    

    Edit: Assignment to window.location.hash breaks in IE7 and IE8 on pages that were loaded as a result of page redirection via the HTTP "Location" header. The solution is to return a page with Javascript that itself will perform the redirection. See the answer by Joe Lapp.

  • Jim
    Jim almost 15 years
    yeah, you're going to need +=
  • Itay Moav -Malimovka
    Itay Moav -Malimovka almost 15 years
    You forget there might be a query string in the URL. Although never tried with # after the query string. Wonder if it will work.
  • Nordes
    Nordes almost 15 years
    If you do that won't it add the #top multiple time at the end of URL?
  • Jim
    Jim almost 15 years
    @Nordes: i'm assuming he's not using the href of the anchor.
  • Admin
    Admin almost 15 years
    I'm using the href for something else, it's weird to explain but I need to do it with Javascript. And it seems like that would add '#top' multiple times to the URL, but I will try it out.
  • Jim
    Jim almost 15 years
    I have edited to avoid the possibility of ever having two hashes in the url
  • Admin
    Admin almost 15 years
    Thanks - unfortunately, it still doesn't work. Nothing seems to work; I don't think it's necessarily the wrong syntax, maybe something else in my code, but I don't know what.
  • Jim
    Jim almost 15 years
    yeah, run that code in a sandbox-like environment, like Firebug. I did, and it works. Make sure you don't have a scoping problem.
  • Peter Bailey
    Peter Bailey almost 15 years
    Really? Works for me. IE 7.0.5730.13. FYI, you don't need a named anchor for #top to work - browsers read that automatically. Alternatively, you could use top.scrollTo(0,0);
  • Admin
    Admin almost 15 years
    I should clarify, I ended up using scrollTo(0,0). I feel kinda dumb, I wasn't aware of this method - but it does work!
  • David John Welsh
    David John Welsh over 11 years
    To be specific, any hash that does not correspond to any element's id will automatically take you to the top of the page (i.e. not just #top). It could be #top, #GOGOGADGETSCROLLUP or just #.