在我工作的這導航如下網站:如何啓用後退按鈕AJAX頁面
$(document).ready(function() {
if (!$('#ajax').length) { // Check if index.html was loaded. If not, navigate to index.html and load the hash part with ajax.
document.location = document.location.href.substring(0, document.location.href.lastIndexOf('/') + 1)
+ "#" + document.location.href.substr(document.location.href.lastIndexOf('/') + 1);
}
if (window.location.hash) // Check if the page is being loaded with a '#'. Load the file.
$('#ajax').load(document.location.href.substr(document.location.href.lastIndexOf('#') + 1));
});
$(document).on("click", "a:not(.regular)", function (e) {
var url = this.href;
if (url.indexOf("https") != -1 || url.indexOf('.html') == -1) // External link or picture
return;
e.preventDefault();
$('#ajax').load(url, function() {
document.location.href = '#' + url.substr(url.lastIndexOf('/') + 1);
});
});
$(window).on('popstate', function (e) {
// Back only!
//location.reload();
//$('#ajax').load(this.location);
});
網址時,系統用戶定位,即使用戶按下後退按鈕改變。但是,導航回頁面時不會刷新頁面,因此只有在實際頁面保持不變時才更改網址。
我該如何解決這個問題?
Fiddle
Actual site
你的意思是你想要「後退」按鈕來強制一個頁面重新加載與URL中的查詢字符串? – Sergio
是的,或者只是定期導航到歷史記錄中的網址。 'document.ready'代碼將修復頁面。 – Jonathan