location:search 属性
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Location
接口的 search
属性会返回一段查询字符串,其中包含 '?'
以及跟随其后的一串 URL 查询参数。
现代浏览器提供 URLSearchParams
和 URL.searchParams
两个接口,使得从查询字符串中解析出查询参数变得更加容易。
值
一个字符串。
示例
js
// 假设文档中有一个 <a id="myAnchor" href="/en-US/docs/Location.search?q=123"> 元素
const anchor = document.getElementById("myAnchor");
const queryString = anchor.search; // 返回:'?q=123'
// 进一步解析:
const params = new URLSearchParams(queryString);
const q = parseInt(params.get("q")); // 是数字 123
规范
Specification |
---|
HTML # dom-location-search-dev |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
search |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
- See implementation notes.
- User must explicitly enable this feature.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.