window.location = #anchor 在 IE 中不起作用

发布于 2024-07-27 15:44:47 字数 1066 浏览 8 评论 0原文

在此地图上:

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

我在顶部有一个锚点,我希望页面在链接时跳转到该锚点被点击。

我目前正在使用

window.location = '#top';

它在 FF、Opera 和 Chrome 中按预期工作,但在 IE 7 中则不然。

我已经尝试了所有排列,例如 window.location.hash 和 window.location.assign() 以及 rollIntoView(true) 和重点()。

我怎样才能让它在 IE 中工作?

编辑:似乎没有任何效果,这让我认为这不是语法问题,而是有关 JS 的问题...这是单击事件处理程序...可能是因为它返回 false 吗? 我正在抓住救命稻草。

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

编辑:在 IE7 和 IE8 中,在通过 HTTP“Location”标头进行页面重定向而加载的页面上,对 window.location.hash 的赋值会中断。 解决方案是返回一个带有 Javascript 的页面,该页面本身将执行重定向。 请参阅乔·拉普的回答。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千寻… 2024-08-03 15:44:47

我在生产中有这段代码,它在 IE7 中运行良好...

location.hash = "#top";

但是,如果您只是想滚动到顶部,这应该容易得多...

window.scrollTo(0, 0);

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);
江南烟雨〆相思醉 2024-08-03 15:44:47

location 对象 分为多个属性 - href 只是其中之一

,另一个 hash 就是您要查找的内容。

top.location.hash = 'top';

您也可以完全不使用 location/href 来完成此操作 - 只需使用 scrollTo()

top.scrollTo( 0, 0 );

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 );
话少心凉 2024-08-03 15:44:47

我还遇到了 windows.location.hash 在除 IE7 和 IE8 之外的所有浏览器中工作的问题(至少在 Vista 上)。 经过多次实验,我发现页面重定向破坏了哈希分配。

如果您从通过 HTTP“Location”标头作为重定向结果加载的页面内向 windows.location.hash 分配值,则 IE7 或 IE8 中将会出现错误。

发现这一点后,我在 StackOverflow 上的其他地方找到了修复程序(参见在这里)。 解决方案是通过 Javascript 让浏览器重定向。 在这里,我重新发布了其他 StackOverflow 页面的解决方案:

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

这可以解释为什么有些人在设置哈希时遇到问题而有些人没有,但我不知道线程的发起者正在重定向。

我还应该指出,我不能只使用scrollTo(),因为我的目的是从地址栏中删除哈希标签而不重新加载页面,而不是滚动。

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.

梦巷 2024-08-03 15:44:47

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

编辑:避免出现两个哈希值的可能性。

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

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

欢烬 2024-08-03 15:44:47

在附加哈希之前,您必须检查哈希。 我这样做了,

window.location = ((location.href).indexOf('#') == -1 ? location.href + "#top" : location.href);

You have to check for hash before appending it. I did it with this,

window.location = ((location.href).indexOf('#') == -1 ? location.href + "#top" : location.href);

毁梦 2024-08-03 15:44:47
window.location.href = '#top';

如果这不起作用,请尝试完整的 URL

window.location.href = 'http://domain.com/my.html#top';
window.location.href = '#top';

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

window.location.href = 'http://domain.com/my.html#top';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文