2014-02-12 38 views
0

我努力使這項工作與location.href和document.referrer HTML按鈕一起

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

應該只是一個簡單的「後退」按鈕,previsous頁面。 當我做這樣一個「A HREF」它的工作原理

<script> 
    document.write('<a href="' + document.referrer + '">Go back</a>'); 
</script> 

而是試圖湯姆AKE它作爲一個「按鈕」,當它失敗。該按鈕在那裏,但沒有導航到預知的頁面

+0

我認爲問題是與括號和引號...請幫助:) –

回答

2

你也可以實現原生回來了:

window.history.back(); 
//in your onclick="history.back()" 
//also can try with: 
//javascript:history.go(-1)"> 

或者調用檢查是否history.back()可在瀏覽器中第一個else實現自定義的回調函數的函數/ code這裏顯示的(我寧願從代碼中取出代碼) HTML只是在處理我的函數的onclick)

Using javascript history.back() fails in Safari .. how do I make it cross-browser?

對於一個強大的解決方案檢查history.js:

https://github.com/browserstate/history.js

4

在href正文中添加另一個轉義引號(以及最後的分號,儘管我不知道這是否會產生影響)並查看是否工程...

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

你錯過了周圍的網址報價在href,所以嘗試逃避像這樣(爲我工作)

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