2014-02-14 36 views
0

我使用這個代碼在我的網站如果「返回」與document.referrer鏈接是空去某些網址

<script> 
document.write('<button onclick="location.href=\'' + document.referrer + '\';">Go back</button>'); 
</script> 

產生一個「返回」鏈接,但在直接用戶登陸此頁面上(例如谷歌搜索後),「回去」按鈕不起作用。

如何在這種情況下進入mypage.html?

謝謝

+0

每一個瀏覽器碰巧有這樣的按鈕已經...或按鍵組合,或鼠標手勢...爲什麼重新實現已經存在的功能? – CBroe

回答

0

您可以使用JavaScript API的歷史:javascript:history.go(-1)

2

您還可以檢查引用是空的,if語句是這樣的:

if (document.referrer != ""){ 
document.write('<button onclick="location.href=\'' + document.referrer + '\';">Go back</button>'); 
} 
else{ 
document.write('<button onclick="location.href=\'mypage.html\'">Go back</button>'); 
} 

那你去的mypage.html並且不與javascript:history.go(-1)

相關問題