window.location = #anchor 在 IE 中不起作用
在此地图上:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在生产中有这段代码,它在 IE7 中运行良好...
但是,如果您只是想滚动到顶部,这应该容易得多...
I have this code in production and it works fine in IE7...
However, if you are just trying to scroll to the top, this ought to be a lot easier...
location
对象 分为多个属性 -href
只是其中之一,另一个
hash
就是您要查找的内容。您也可以完全不使用 location/href 来完成此操作 - 只需使用 scrollTo()
The
location
object is broken up into several properties -href
is only one of themAnother one,
hash
, is what you're looking for.You can also do this without using the location/href at all - just use scrollTo()
我还遇到了 windows.location.hash 在除 IE7 和 IE8 之外的所有浏览器中工作的问题(至少在 Vista 上)。 经过多次实验,我发现页面重定向破坏了哈希分配。
如果您从通过 HTTP“Location”标头作为重定向结果加载的页面内向 windows.location.hash 分配值,则 IE7 或 IE8 中将会出现错误。
发现这一点后,我在 StackOverflow 上的其他地方找到了修复程序(参见在这里)。 解决方案是通过 Javascript 让浏览器重定向。 在这里,我重新发布了其他 StackOverflow 页面的解决方案:
这可以解释为什么有些人在设置哈希时遇到问题而有些人没有,但我不知道线程的发起者正在重定向。
我还应该指出,我不能只使用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:
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.
location.href = location.href.split("#")[0] + "#top"
编辑:避免出现两个哈希值的可能性。
location.href = location.href.split("#")[0] + "#top"
EDIT: to avoid the possibility of ever having two hashes.
在附加哈希之前,您必须检查哈希。 我这样做了,
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);
如果这不起作用,请尝试完整的 URL
And if this doesn't work, try the full URL