JavaScript Window History

window.history 物件包含瀏覽器的歷史。


Window History

window.history物件在編寫時可不使用 window 這個字首。

為了保護使用者隱私,對 JavaScript 訪問該物件的方法做出了限制。

一些方法:

  • history.back() - 與在瀏覽器點選後退按鈕相同
  • history.forward() - 與在瀏覽器中點擊向前按鈕相同

Window history.back()

history.back() 方法載入歷史列表中的前一個 URL。

這與在瀏覽器中點選後退按鈕是相同的:

例項

在頁面上建立後退按鈕:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <head> <script> function goBack() { window.history.back() } </script> </head> <body> <input type="button" value="Back" onclick="goBack()"> </body> </html>

以上程式碼輸出為:



Window history.forward()

history forward() 方法載入歷史列表中的下一個 URL。

這與在瀏覽器中點選前進按鈕是相同的:

例項

在頁面上建立一個向前的按鈕:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function goForward() { window.history.forward() } </script> </head> <body> <input type="button" value="Forward" onclick="goForward()"> </body> </html>

以上程式碼輸出為: