Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

window.history

大綱

回傳一個 History 物件,提供了一個介面來操控瀏覽器 session 的歷史資料(記錄頁面被瀏覽在一個 tab 或是 frame

語法

var historyObj = window.history;

 History 物件可以被宣告在下列的方法:

方法 敘述 回傳值
history.back()

回到先前的 session 歷史頁面,和按了瀏覽器的返回鈕同樣效果。也等同於 history.go(-1).

Note: Calling this method to go back beyond the first page in the session history has no effect and doesn't raise an exception.
沒有回傳值
history.forward()

到下一個 session 歷史頁面,與往前一個頁面同等的效果。這個效果同等於history.go(1).

Note: Calling this method to go back beyond the last page in the session history has no effect and doesn't raise an exception.
沒有回傳值
history.go(integerDelta)

載入一個頁面從 session 的歷史紀錄,利用記錄相對於目前頁面的 location。例如 -1 是到前一頁或 1 是到下一頁。當 integerDelta 超過了範圍 (例如 -1 當他沒有先前的歷史頁面可以呈現),這個方法不會做任何事情只會多一個新的例外處理。呼叫 go() 但沒有參數或是一個非整數的參數不會有任何的效果(不像是 Internet Explorer,which supports string URLs as the argument

沒有回傳值
history.pushState(data, title [, url])

推一個既有的 data 到 session 的歷史紀錄包含著特定的標題和 URL 如果有的話。這個資料是被非透明的方式處理; 你可能會特定一些 JavaScript 的物件可以序列化。注意 Firefox 目前忽略了標題這個屬性; 更多資訊,看 manipulating the browser history

Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
沒有回傳值
history.replaceState(data, title [, url])

推一個既有的 data 到 session 的歷史紀錄包含著特定的標題和 URL 如果有的話。這個資料是被非透明的方式處理; 你可能會特定一些 JavaScript 的物件可以序列化。注意 Firefox 目前忽略了標題這個屬性; 更多資訊,看 manipulating the browser history

Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
沒有回傳值

看 Manipulating the browser history 給予一些範例和細節。更明確的說,這些文章解釋了安全問題在 pushState() replaceState() 方法中需要注意哪些東西當你在用它的時候。

History 物件可以有下列的屬性:

Property Type Description
length Integer Read-only. Returns the number of elements in the session history, including the currently loaded page. For example, for a page loaded in a new tab this property returns 1.
current String Returns the URL of the active item of the session history. This property is not available to web content and is not supported by other browsers. Use location.href instead.
next String Returns the URL of the next item in the session history. This property is not available to web content and is not supported by other browsers.
previous String Returns the URL of the previous item in the session history. This property is not available to web content and is not supported by other browsers.
state nsIVariant

Returns the state at the top of the history stack. This is a way to look at the state without having to wait for a popstate event. Read only.

範例

history.back();     // 等同於按回復鍵
history.go(-1);     // 等同於 history.back();

注意

For top-level pages you can see the list of pages in the session history, accessible via the History object, in the browser's dropdowns next to the back and forward buttons.

For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.

相關

MSDN: history Object (window)

規範

文件標籤與貢獻者

 此頁面的貢獻者: jsx, ChiLiJung
 最近更新: jsx,