history.back
是一個函數。你必須把它稱爲一個函數。通過分配到window.location
返回導航歷史記錄不起作用。
讓您的代碼根據點擊鏈接的href有所不同。
$('a').click(function (e)
{
e.preventDefault();
var newLocation = this.href;
$('body').fadeOut(250, newPage);
function newPage()
{
if(newLocation == "javascript:history.back()")
{
history.back();
}
else
{
window.location = newLocation;
}
}
});
完整的測試設置:
Caller.html
<html>
<head><title>Caller</title></head>
<body>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
XXXXXXXXXXXXX<br/>
<a href="Callee.html">Go to Callee</a>
</body>
</html>
Callee.html
<html>
<head><title>Callee</title></head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<a href="javascript:history.back()">Go back</a>
<script>
$('a').click(function (e)
{
e.preventDefault();
var newLocation = this.href;
$('body').fadeOut(250, newPage);
function newPage()
{
if(newLocation == "javascript:history.back()")
{
history.back();
}
else
{
window.location = newLocation;
}
}
});
</script>
</body>
</html>
你爲什麼不就叫'history.back()''中的新頁'? – NineBerry
該代碼應該在頁面上的所有鏈接上工作,並且大多數鏈接都是常規鏈接......設置固定值會打敗整個目的。 –
(1)正如文中建議'javascript:history.back()'是歷史對象上的JavaScript方法,它不是一個url。 (2)使用'document.referrer'作爲url。 – Abhitalks