get()
方法在
URLSearchParams
interface returns the first value associated to the given search parameter.
URLSearchParams.get(name)
The name of the parameter to return.
A
USVString
if the given search parameter is found; otherwise,
null
.
If the URL of your page is
https://example.com/?name=Jonathan&age=18
you could parse out the 'name' and 'age' parameters using:
let params = new URLSearchParams(document.location.search.substring(1));
let name = params.get("name"); // is the string "Jonathan"
let age = parseInt(params.get("age"), 10); // is the number 18
Requesting a parameter that isn't present in the query string will return
null
:
let address = params.get("address"); // null
| 规范 | 状态 | 注释 |
|---|---|---|
|
URL
The definition of 'get()' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
get
|
Chrome 49 | Edge 17 | Firefox 29 | IE 不支持 No | Opera 36 | Safari Yes | WebView Android 49 | Chrome Android 49 | Firefox Android 29 | Opera Android 36 | Safari iOS Yes | Samsung Internet Android 5.0 |
完整支持
不支持
URLSearchParams