2013-02-18 66 views
0

我正在嘗試在JS中創建一個鏈接,將該用戶的頁面從他來自哪裏。這是代碼。用javascript創建鏈接

<script language="javascript"> 
function Jump() 
{ 
document.href=document.referrer; 
} 
</script> 

下面是HTML,

<a href="#" onclick="Jump();">Skip and Continue</a> 

現在,當用戶點擊這個鏈接時,沒有任何反應。請指導我在哪裏做錯了。由於

+3

試試這個:document.location.href = document.referrer; – terabaud 2013-02-18 00:34:44

+0

也許你的推薦人是空的 – cIph3r 2013-02-18 00:36:03

+0

@terabaud:像魅力一樣工作。 – 2013-02-18 00:42:27

回答

1

怎麼樣使用下面的代碼回遷

history.back(); 
+0

謝謝。不過,我創建的那個有什麼問題? – 2013-02-18 00:35:09

+0

添加此代碼以查看您正在使用支持引用鏈的瀏覽器。 if('referrer'in document){ console.log(document.referrer); } – 2013-02-18 00:39:07

0

許多瀏覽器將不會使用document.referrer隱私的原因,特別是如果引用來自另一個域。

而是嘗試使用onclick="history.go(-1)"而不是Jump()函數。

0

綁定點擊監聽器比使用onclick更好。

嘗試將其更改爲這樣:

<a id="myLink" href="#">Skip and Continue</a> 

和Javascript:

<script type="text/javascript"> 
document.getElementById('myLink').addEventListener('click', function(e) { 
    e.preventDefault(); 
    document.location.href=document.referrer; //actually better as "history.back()" 
} 
</script> 
+0

'document.href'不是一個東西。 – sachleen 2013-02-18 00:35:59

+0

@sachleen哦,哎呀,複製/粘貼。編輯。 – Doorknob 2013-02-18 00:36:25

0

它不是document.href但window.location.href ...

<script> 
function Jump(){ 
    if(document.referrer)location.href=document.referrer; 
    else history.back(); 
} 
</script> 

(如果您使用跳轉到當前頁面,則沒有引用者。)