Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...