位置:search 属性
Location 接口的 search 属性是一个搜索字符串,也称为查询字符串,其中包含一个 "?" 符号,后跟位置 URL 的参数。如果 URL 没有搜索查询,则此属性包含一个空字符串,""。
可以设置此属性来更改 URL 的查询字符串。设置时,如果未提供,则会在提供的值前面添加一个 "?" 前缀。将其设置为空字符串 "" 会删除查询字符串。
查询在设置时会进行百分比编码,但在读取时不会进行百分比解码。
现代浏览器提供了 URLSearchParams 和 URL.searchParams,可以轻松地从查询字符串中解析参数。
有关更多信息,请参阅 URL.search。
值
字符串。
示例
js
// Let an <a id="myAnchor" href="/en-US/docs/Location.search?q=123"> element be in the document
const anchor = document.getElementById("myAnchor");
const queryString = anchor.search; // Returns:'?q=123'
// Further parsing:
const params = new URLSearchParams(queryString);
const q = parseInt(params.get("q"), 10); // is the number 123
规范
| 规范 |
|---|
| HTML # dom-location-search-dev |
浏览器兼容性
加载中…