2011-12-22 94 views
2

我有相關的瀏覽器後退按鈕幾個問題 -瀏覽器後退按鈕和前一頁的URL

1)如何才能辨別通過我來了當前頁面的前一頁面的網址是什麼? 2)我怎樣才能確定請求是否在瀏覽器歷史記錄中的上一頁? 3)我怎麼知道用戶點擊了瀏覽器的後退按鈕?

我們有一個要求,當用戶點擊應用程序中的後退按鈕時,他會返回到錯誤頁面。

執行必須通過javasctipt。我試圖與歷史對象合作,但截至目前我感到震驚。請幫幫我!!

回答

1

1)如何才能辨別的前一頁,通過它我已經走到了當前頁面

window.document.referrer 

回報從頁面打開其中的URL的URL。

其他兩個問題,下面是MDN一個例子:

Example 

Suppose http://mozilla.org/foo.html executes the following JavaScript: 

var stateObj = { foo: "bar" }; 
history.pushState(stateObj, "page 2", "bar.html"); 
This will cause the URL bar to display http://mozilla.org/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists. 

Suppose now that the user now navigates to http://google.com, then clicks back. At this point, the URL bar will display http://mozilla.org/bar.html, and the page will get a popstate event whose state object contains a copy of stateObj. The page itself will look like foo.html, although the page might modify its contents during the popstate event. 

If we click back again, the URL will change to http://mozilla.org/foo.html, and the document will get another popstate event, this time with a null state object. Here too, going back doesn't change the document's contents from what they were in the previous step, although the document might update its contents manually upon receiving the popstate event.