google.script.history 類別 (用戶端 API)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
google.script.history
是能與使用者互動的非同步用戶端 JavaScript API
瀏覽器記錄堆疊只能在使用
IFRAME
。
但不適用於外掛程式或外掛程式中的側欄和對話方塊
容器型式的內容詳情請參閱
瀏覽器使用指南
歷史記錄
內容詳盡的說明文件
push(stateObject, params, hash)
將提供的狀態物件、網址參數和網址片段推送至瀏覽器記錄
狀態物件是由開發人員定義的簡單 JavaScript 物件,可以
包含任何與應用程式目前狀態相關的資料。這個方法就類似於
pushState()
JavaScript 方法。
Index.html
var now = new Date();
var state = {
'timestamp': now.getTime()
};
var params = {
'options': "none"
};
google.script.history.push(state, params, "anchor1");
參數
名稱 | 類型 | 說明 |
stateObject | Object | 開發人員定義的物件
與瀏覽器記錄事件相關,並在狀態彈出時重新顯示。一般價格
儲存應用程式狀態資訊 (例如網頁資料),以供日後擷取。 |
params | Object | 一個物件,其中包含要匯入的網址參數
與這個狀態建立關聯例如,{foo: “bar”, fiz: “baz”} 相當於
"?foo=bar&fiz=baz" 。或者,您也可以使用陣列:
{foo: [“bar”, “cat”], fiz: “baz”} 相當於 "?foo=bar&foo=cat&fiz=baz" 。
如果值為空值或未定義,則現有的網址參數不會變更。如果留空,網址參數就會是
已清除。
|
hash | String | 字串網址片段出現在
「#」字元。如果為空值或未定義,則目前網址片段不會變更。如果空白,
已清除網址片段。
|
replace(stateObject, params, hash)
將瀏覽器記錄堆疊中的熱門事件替換為提供的 (開發人員定義) 狀態
物件、網址參數和網址片段。在其他情況下,這與
push() 物件。
Index.html
var now = new Date();
var state = {
'timestamp': now.getTime()
};
var params = {
'options': "none"
};
google.script.history.replace(state, params, "anchor1");
參數
名稱 | 類型 | 說明 |
stateObject | Object | 開發人員定義的物件
與瀏覽器記錄事件相關,並在狀態彈出時重新顯示。一般價格
儲存應用程式狀態資訊 (例如網頁資料),以供日後擷取。 |
params | Object | 一個物件,其中包含要匯入的網址參數
與這個狀態建立關聯例如,{foo: “bar”, fiz: “baz”} 相當於
"?foo=bar&fiz=baz" 。或者,您也可以使用陣列:
{foo: [“bar”, “cat”], fiz: “baz”} 相當於 "?foo=bar&foo=cat&fiz=baz" 。
如果值為空值或未定義,則現有的網址參數不會變更。如果留空,網址參數就會是
已清除。
|
hash | String | 字串網址片段出現在
「#」字元。如果為空值或未定義,則目前網址片段不會變更。如果空白,
已清除網址片段。
|
setChangeHandler(function)
設定回呼函式,回應瀏覽器記錄的變更。回呼函式
應該只有一個事件物件做為引數。
Index.html
google.script.history.setChangeHandler(function (e) {
console.log(e.state);
console.log(e.location.parameters);
console.log(e.location.hash);
// Adjust web app UI to match popped state here...
});
參數
名稱 | 類型 | 說明 |
function | Function | 用戶端
要對記錄變更事件執行的回呼函式,
event object 做為唯一的引數。
|
事件物件
欄位 |
e.state |
與彈出事件相關聯的狀態物件。這個物件
相應 push() 或
新增彈出狀態的 replace() 方法
記錄堆疊
{"page":2, "name":"Wilbur"}
|
e.location |
位置物件
與跳出事件相關聯
{"hash":"", "parameter":{"name": "alice", "n": "1"}, "parameters":{"name": ["alice"], "n": ["1", "2"]}}
|
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003egoogle.script.history\u003c/code\u003e enables interaction with the browser history stack within web apps using \u003ccode\u003eIFRAME\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods to push, replace, and monitor browser history state, including URL parameters and fragments.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can define custom state objects to store and retrieve application-specific data associated with history events.\u003c/p\u003e\n"],["\u003cp\u003eA change handler can be set to trigger a callback function in response to history changes, allowing dynamic UI updates.\u003c/p\u003e\n"]]],[],null,["# Class google.script.history (Client-side API)\n\n`google.script.history` is an asynchronous client-side JavaScript API that can interact\nwith the browser history stack. It can only be used in the context of a web app that uses\n[`IFRAME`](/apps-script/reference/html/sandbox-mode#properties).\nIt is not intended for use with sidebars and dialogs in an add-on or\ncontainer-script context. For more information, see the\n[guide to using browser\nhistory in web apps](/apps-script/guides/web#web_apps_and_browser_history). \n\n### Methods\n\n| Method | Return type | Brief description |\n|----------------------------------------------------------------------|-------------|----------------------------------------------------------------------------------------------------------------------|\n| [push(stateObject, params, hash)](#push(Object,Object,String)) | `void` | Pushes the provided state object, URL parameters and URL fragment onto the browser history stack. |\n| [replace(stateObject, params, hash)](#replace(Object,Object,String)) | `void` | Replaces the top event on the browser history stack with the provided state object, URL parameters and URL fragment. |\n| [setChangeHandler(function)](#setChangeHandler(Function)) | `void` | Sets a callback function to respond to changes in the browser history |\n\nDetailed documentation\n----------------------\n\n### `push(stateObject, params, hash)`\n\nPushes the provided state object, URL parameters and URL fragment onto the browser history\nstack. The state object is a simple JavaScript Object that is defined by the developer and can\ncontain any data relevant to the app's current state. This method is analogous to the\n[pushState()](https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method)\nJavaScript method. \n\n### Index.html\n\n```html\nvar now = new Date();\nvar state = {\n 'timestamp': now.getTime()\n};\nvar params = {\n 'options': \"none\"\n};\ngoogle.script.history.push(state, params, \"anchor1\");\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `stateObject` | `Object` | An developer-defined object to be associated with a browser history event, and which resurfaces when the state is popped. Typically used to store application state information (such as page data) for future retrieval. |\n| `params` | `Object` | An object containing URL parameters to associate with this state. For example, `{foo: \"bar\", fiz: \"baz\"}` equates to `\"?foo=bar&fiz=baz\"`. Alternatively, arrays can be used: `{foo: [\"bar\", \"cat\"], fiz: \"baz\"}` equates to `\"?foo=bar&foo=cat&fiz=baz\"`. If null or undefined, the current URL parameters are not changed. If empty, the URL parameters are cleared. |\n| `hash` | `String` | The string URL fragment appearing after the '#' character. If null or undefined, the current URL fragment is not changed. If empty, the URL fragment is cleared. |\n\n*** ** * ** ***\n\n*** ** * ** ***\n\n### `replace(stateObject, params, hash)`\n\nReplaces the top event on the browser history stack with the provided (developer-defined) state\nobject, URL parameters and URL fragment. This is otherwise identical to\n[push()](#push(Object,Object,String)). \n\n### Index.html\n\n```html\nvar now = new Date();\nvar state = {\n 'timestamp': now.getTime()\n};\nvar params = {\n 'options': \"none\"\n};\ngoogle.script.history.replace(state, params, \"anchor1\");\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `stateObject` | `Object` | An developer-defined object to be associated with a browser history event, and which resurfaces when the state is popped. Typically used to store application state information (such as page data) for future retrieval. |\n| `params` | `Object` | An object containing URL parameters to associate with this state. For example, `{foo: \"bar\", fiz: \"baz\"}` equates to `\"?foo=bar&fiz=baz\"`. Alternatively, arrays can be used: `{foo: [\"bar\", \"cat\"], fiz: \"baz\"}` equates to `\"?foo=bar&foo=cat&fiz=baz\"`. If null or undefined, the current URL parameters are not changed. If empty, the URL parameters are cleared. |\n| `hash` | `String` | The string URL fragment appearing after the '#' character. If null or undefined, the current URL fragment is not changed. If empty, the URL fragment is cleared. |\n\n*** ** * ** ***\n\n### `setChangeHandler(function)`\n\nSets a callback function to respond to changes in the browser history. The callback function\nshould take only a single [event object](#eventObject) as an argument.\n\n### Index.html\n\n```html\ngoogle.script.history.setChangeHandler(function (e) {\n console.log(e.state);\n console.log(e.location.parameters);\n console.log(e.location.hash);\n\n // Adjust web app UI to match popped state here...\n});\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|------------|----------------------------------------------------------------------------------------------------------------------------------|\n| `function` | `Function` | a client-side callback function to run upon a history change event, using the [event object](#eventObject) as the only argument. |\n\n#### Event object\n\n| Fields ||\n|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `e.state` | The state object associated with the popped event. This object is identical to the state object that used in the corresponding [push()](#push(Object,Object,String)) or [replace()](#replace(Object,Object,String)) method that added the popped state to the history stack. ``` {\"page\":2, \"name\":\"Wilbur\"} ``` |\n| `e.location` | A [location object](/apps-script/guides/html/reference/url#locationObject) associated with the popped event ``` {\"hash\":\"\", \"parameter\":{\"name\": \"alice\", \"n\": \"1\"}, \"parameters\":{\"name\": [\"alice\"], \"n\": [\"1\", \"2\"]}} ``` |\n\n*** ** * ** ***"]]