IE6/7 后退/前进按钮不改变 window.location.hash
我有一个 ajax web 应用程序 (JSF 2.0),它使用哈希进行导航。
我在 这个答案和setInterval()
来检查旧浏览器(主要是IE6+7)中的值变化。
执行此操作的 Javascript 代码:
window.onload = window.onhashchange = function() {
lastHash = window.location.hash; // To save a refresh on browsers that don't need it.
var fragment = document.getElementById('fragment');
fragment.value = window.location.hash;
fragment.onchange();
}
// Old Browsers That don't support onhashchange...
var lastHash = window.location.hash;
function checkFragment() {
if (window.location.hash != lastHash) {
lastHash = window.location.hash;
var fragment = document.getElementById('fragment');
fragment.value = window.location.hash;
fragment.onchange();
}
}
效果很好。这意味着,当我更改哈希值时,AJAX 应用程序会收到通知并进行更新。 IE 6/7 是其中一个不起作用的实例。当我按后退/前进按钮时,我可以看到网址栏已更新为正确的哈希值,但 windows.location.hash
似乎没有改变,所以我的 SetEvent( )
函数未检测到更改。
有人找到解决这个问题的方法吗? 谢谢!
I have an ajax webapp (JSF 2.0) that uses hash for navigation.
I've used both event firing with help of this answer and setInterval()
to check for value change in older browsers (Mainly IE6+7).
The Javascript code that does this:
window.onload = window.onhashchange = function() {
lastHash = window.location.hash; // To save a refresh on browsers that don't need it.
var fragment = document.getElementById('fragment');
fragment.value = window.location.hash;
fragment.onchange();
}
// Old Browsers That don't support onhashchange...
var lastHash = window.location.hash;
function checkFragment() {
if (window.location.hash != lastHash) {
lastHash = window.location.hash;
var fragment = document.getElementById('fragment');
fragment.value = window.location.hash;
fragment.onchange();
}
}
This works nicely. Meaning, when I change the value of the hash the AJAX app gets the notification and updates.
One instance in which this doesn't work is IE 6/7. When I press Back/Forward buttons, I can see the url bar gets updated with the correct hash values, but the windows.location.hash
doesn't seem to change and so my SetEvent()
function doesn't detect the change.
Anyone found a solution to this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑使用 jQuery Hashchange 插件 以避免 IE6/7 兼容性问题。您可以在此处找到代码示例。可以根据您的情况进行如下更改。
Consider using jQuery Hashchange plugin to avoid IE6/7 compatibility headaches. You can find code example here. It can be altered as follows for your case.