2012-02-05 68 views
5

例如,如果用戶在http://example.com上,則用戶轉到http://example.com#comments。如果用戶在瀏覽器上點擊「返回」,我怎麼能讓他「忽略」http://example.com並直接訪問他之前訪問過的URL?當單擊後退按鈕時,使瀏覽器忽略URL哈希

我有jQuery加載。

+2

你得阻止他把你點擊'#comments'首先,可能使用'preventDefault()'並使用jQuery來滾動那裏。否則,這一切都成爲正常歷史的一部分。 – Sparky 2012-02-05 02:45:17

回答

9

而不必像一個鏈接的:

<a href='#comments'>Link</a> 

使用location.replace()在瀏覽器的歷史「覆蓋」的http://example.com記錄。

https://developer.mozilla.org/en/DOM/window.location

例子:

HTML:

<a id='commentsLink'>Link</a> 

的JavaScript:

$("#commentsLink").click(function(){ 
    window.location.replace("#comments"); 
}); 
+4

不要使用w3schools ... http://w3fools.com(window.location信息的替代鏈接:https://developer.mozilla.org/en/DOM/window.location) – 2012-02-05 02:46:32

+0

@Platinum Azure - 當我發佈這個鏈接的時候,這是在我的腦海裏,但懶惰已經成爲我最好的了。感謝您直接設置我:) – 2012-02-05 02:48:41

+1

感謝您實際修復它。 +1爲你的麻煩:-) – 2012-02-05 03:03:30

相關問題