google.script.url (用戶端 API) 類別
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
google.script.url
是非同步用戶端 JavaScript
可以查詢網址的 API,取得目前的網址參數和片段。
這個 API 支援 google.script.history
也能使用 Google Cloud CLI 或
Compute Engine API只能在使用
IFRAME
。
但不適用於外掛程式或外掛程式中的側欄和對話方塊
容器型式的內容詳情請參閱
瀏覽器使用指南
歷史記錄
內容詳盡的說明文件
getLocation(function)
取得網址位置物件,並傳送至指定的回呼
函式 (做為唯一的引數),
Index.html
google.script.url.getLocation(function(location) {
console.log(location.parameters);
console.log(location.hash);
});
參數
位置物件
下表列舉了在
查詢的網址:
http://www.example.com?n=1&name=alice&n=2#headingAnchor
欄位 |
location.hash |
# 字元後方的網址片段字串值。
如果沒有網址片段,則傳回空白字串
headingAnchor
|
location.parameter |
與網址要求相對應的鍵/值組合物件
參數。參數只會傳回第一個值
顯示多個值如果沒有參數,這個
不得為空物件
{"name": "alice", "n": "1"}
|
location.parameters |
與 location.parameter 類似的物件,但
各鍵的值陣列。如果沒有參數,這個
不得為空物件
{"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.url\u003c/code\u003e is a client-side JavaScript API for querying URLs to get parameters and fragments within web apps.\u003c/p\u003e\n"],["\u003cp\u003eThis API works with \u003ccode\u003egoogle.script.history\u003c/code\u003e and requires the use of \u003ccode\u003eIFRAME\u003c/code\u003e in your web app.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetLocation()\u003c/code\u003e method retrieves a URL location object containing parameters and hash information, passing it to a callback function.\u003c/p\u003e\n"],["\u003cp\u003eThe location object provides access to URL components like hash, individual parameters, and all parameter values.\u003c/p\u003e\n"]]],[],null,["# Class google.script.url (Client-side API)\n\n`google.script.url` is an asynchronous client-side JavaScript\nAPI that can query URLs to obtain the current URL parameters and fragment.\nThis API supports the [`google.script.history`](/apps-script/guides/html/reference/history)\nAPI. 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| [getLocation(function)](#getLocation(Function)) | `void` | Gets a URL location object and passes it to the specified callback function. |\n\nDetailed documentation\n----------------------\n\n### `getLocation(function)`\n\nGets a [URL location object](#locationObject) and passes it to the specified callback\nfunction (as the only argument). \n\n### Index.html\n\n```html\ngoogle.script.url.getLocation(function(location) {\n console.log(location.parameters);\n console.log(location.hash);\n});\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|------------|------------------------------------------------------------------------------------------------------------|\n| `function` | `Function` | a client-side callback function to run, using the [location object](#locationObject) as the only argument. |\n\n#### Location object\n\nThe following table shows examples of the location elements that would be specified if the following URL was queried: `http://www.example.com?n=1&name=alice&n=2#headingAnchor`\n\n| Fields ||\n|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `location.hash` | The string value of URL fragment after the `#` character, or an empty string if no URL fragment is present ``` headingAnchor ``` |\n| `location.parameter` | An object of key/value pairs that correspond to the URL request parameters. Only the first value will be returned for parameters that have multiple values. If no parameters are present, this will be an empty object. ``` {\"name\": \"alice\", \"n\": \"1\"} ``` |\n| `location.parameters` | An object similar to `location.parameter`, but with an array of values for each key. If no parameters are present, this will be an empty object. ``` {\"name\": [\"alice\"], \"n\": [\"1\", \"2\"]} ``` |\n\n*** ** * ** ***"]]