在较旧版本的Opera(Opera/9.80 (Macintosh; Intel Mac OS X 10.10.3) Presto/2.12.388 Version/12.16
)中,在ajax请求回调中更新document.location.hash时,历史记录存在问题。
hashchange
并记录document.location.hash
和history.length
document.location.hash
document.location.hash
document.location.hash
document.location.hash
// listen to hash changes
$(window).on('hashchange', function() {
console.log("Updated hash: ", document.location.hash);
console.log("history.length: ", history.length);
});
var updateHash = function(hash) {
document.location.hash = hash;
};
// set the first route
updateHash('#start');
// do async request and change route
promise = $.get('http://beta.json-generator.com/api/json/get/PumsGq6');
promise.done(function() {
updateHash('#done_async');
});
// wait for 5 seconds and change route
setTimeout(function() {
updateHash('#complete_timeout');
}, 5000);

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>History change</title>
</head>
<body>
<a href="#page">set page</a>
</body>
</html>
&#13;
Updated hash: #start
history.length: 2
> XHR finished loading: GET "http://beta.json-generator.com/api/json/get/PumsGq6"
Updated hash: #done_async
history.length: 3
Updated hash: #complete_timeout
history.length: 4
Updated hash: #page
history.length: 5
Updated hash: , #start
history.length: , 2
Updated hash: , #done_async
history.length: , 2
Updated hash: , #complete_timeout
history.length: , 3
Updated hash: , #page
history.length: , 4
可以在异步请求后的控制台输出中看到浏览器历史记录未更改,但 url / hash正确更新。 setTimeout
只是为了验证可以以非同步的方式更改历史记录。
这导致导航问题,因为我无法回到历史记录中不存在的一点。
答案 0 :(得分:0)
虽然我没有完整的解释,但它是通过使用jQuery ^ 1.8.3解决的。