Javascript 在ie9及以下版本中使用History.js时,页面刷新会返回主页
我已经建立了一个网站,它使用History.js插件使用AJAX从一个页面导航到另一个页面,并相应地更新URL。除了在IE中,所有的工作都很好;当您刷新页面时,它实际上加载的是您访问的第一个页面的内容,而不是当前页面的内容。在“体面的”浏览器中,它不会加载任何页面的内容,它只是加载该URL的整个页面,这是IE应该做的 我想它不知道该怎么处理散列。如果您访问它,它可以正常工作,但当您访问(使用散列)时,它就不能正常工作 如果页面在路径名中检测到#,我尝试重定向页面,但无法将其检测为window.location.pathname和History.getHash()返回没有任何哈希的路径 有什么建议吗?我见过一些使用这个插件的网站也有同样的问题,这里也有类似的问题,但没有解决方案 提前谢谢 这对我很有用:Javascript 在ie9及以下版本中使用History.js时,页面刷新会返回主页,javascript,history.js,Javascript,History.js,我已经建立了一个网站,它使用History.js插件使用AJAX从一个页面导航到另一个页面,并相应地更新URL。除了在IE中,所有的工作都很好;当您刷新页面时,它实际上加载的是您访问的第一个页面的内容,而不是当前页面的内容。在“体面的”浏览器中,它不会加载任何页面的内容,它只是加载该URL的整个页面,这是IE应该做的 我想它不知道该怎么处理散列。如果您访问它,它可以正常工作,但当您访问(使用散列)时,它就不能正常工作 如果页面在路径名中检测到#,我尝试重定向页面,但无法将其检测为window.l
<script>
var url = new String(document.location);
if (url.indexOf("#") > -1) {
alert("There is a Hash in the path");
}
</script>
样本来源:可能是一种解决方案:
您可以从以下位置试用my fork的History.js非官方版本1.8a2吗
…并给出反馈?多谢各位 我在重写tarheelreader.org时遇到了同样的问题。我正在使用History.js,除了IE8中的刷新问题外,它工作正常。这个黑客对我有用 在仅在初始页面加载时运行的启动代码中,我执行以下操作:
var url = window.location.href;
if (url.indexOf('#') > -1) {
// ie refresh hack
controller.stateChange();
}
其中controller.stateChange()
是我用于所有历史记录更改的状态更改处理程序
function stateChange() {
// handle changes in the URL
var hist = History.getState(),
url = hist.url,
context = hist.data;
renderUrl(url, context).then(function(title) {
document.title = title;
});
}
您可以在以下位置查看main.js和controller.js中的所有代码:
编辑
进一步的探索导致了History.js使用初始URL而不是根URL的情况。这个黑客似乎能处理那个案子
function stateChange() {
// handle changes in the URL
var hist = History.getState(),
url = hist.url,
bar = window.location.href,
context = hist.data;
//console.log("State changed...", url, context);
if (url != bar && bar.indexOf('#') > -1) {
//console.log('bar = ', bar);
// I think we only get here in IE8
// hack for hash mode urls
var root = History.getRootUrl(),
hashIndex = bar.indexOf('#');
if (root != bar.slice(0, hashIndex)) {
// try to fix the url
url = root + bar.slice(hashIndex);
//console.log('new url =', url);
window.location.href = url;
}
}
renderUrl(url, context).then(function(title) {
document.title = title;
});
}
谢谢你的帮助,但恐怕运气不好。我在ie7中遇到以下错误:“location.href”为null或不是对象。ie8中没有错误,但仍然没有运气。尚未在ie9中测试…Hanlets answer目前将完成这项工作,但我将更详细地将此问题添加到您的git hub页面,以便您有希望修复它。谢谢你,继续努力!是的,这就行了。虽然不是完美的解决方案,因为它会在一瞬间显示上一页。一旦你继续导航,URL就会变得有点混乱:“clients/#/services/?&suid…”这有点混乱,但是嘿,大部分ie用户都不知道到底发生了什么。你能解释一下为什么window.location.pathname和History.getHash()不会返回“hash”吗?
location.href
应该给你同样的结果document.location
是一个强类型对象,它有很多属性,其中的hash
属性也会为您提供url的哈希(仅哈希)。将对象强制转换为字符串将得到与location.href
相同的结果。我不太熟悉History.js
,所以我不能肯定地告诉您为什么它不适合您。我已经使用document.location更新了代码,以显示几个示例。感谢您的回答。这对我来说很好,但也有点复杂,这就是为什么我选择了@Hanlet Escaño answer。干杯复杂的是在处理各种问题时,比如你在上面的评论中描述的URL出错。嗨,我在IE8/9中遇到了与history.js相同的问题,当我刷新时,IE会加载第一页,而不是像其他html5浏览器那样加载当前页。你能帮我解决这个问题吗?
function stateChange() {
// handle changes in the URL
var hist = History.getState(),
url = hist.url,
bar = window.location.href,
context = hist.data;
//console.log("State changed...", url, context);
if (url != bar && bar.indexOf('#') > -1) {
//console.log('bar = ', bar);
// I think we only get here in IE8
// hack for hash mode urls
var root = History.getRootUrl(),
hashIndex = bar.indexOf('#');
if (root != bar.slice(0, hashIndex)) {
// try to fix the url
url = root + bar.slice(hashIndex);
//console.log('new url =', url);
window.location.href = url;
}
}
renderUrl(url, context).then(function(title) {
document.title = title;
});
}