views:

2009

answers:

4

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;
});
+3  A: 

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

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

geowa4
You forget there might be a query string in the URL. Although never tried with # after the query string. Wonder if it will work.
Itay Moav
http://stackoverflow.com/search?q=gobble#hlogo works fine for me
geowa4
If you do that won't it add the #top multiple time at the end of URL?
Nordes
@Nordes: i'm assuming he's not using the `href` of the anchor.
geowa4
edited to include the alternative without js
geowa4
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.
Peyton
I have edited to avoid the possibility of ever having two hashes in the url
geowa4
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.
Peyton
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.
geowa4
A: 
window.location.href = '#top';

And if this doesn't work, try the full URL

window.location.href = 'http://domain.com/my.html#top';
Itay Moav
yeah, you're going to need +=
geowa4
+4  A: 

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 );
Peter Bailey
Thanks, but still doesn't work in IE 7.
Peyton
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);`
Peter Bailey
+3  A: 

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...

scrollTo(0, 0);
Josh Stodola
Perfect, thank you!
Peyton
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!
Peyton