Location: search

翻译不完整。 请帮助我们翻译这篇文章!

Location 接口的 search 属性会返回一段 USVString,其中包含一个URL标识中的 '?' 以及跟随其后的一串URL查询参数。

现代浏览器提供 URLSearchParams 和 URL.searchParams 两个接口,使得从查询字符串中解析出查询参数变得更加容易。

Syntax

string = object.search;
object.search = string;

Examples

// 声明了一个 <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/Location.search?q=123"> 元素在文档流中
var anchor = document.getElementById("myAnchor");
var queryString = anchor.search; // Returns:'?q=123'

// 进一步解析:
let params = new URLSearchParams(queryString);
let q = parseInt(params.get("q")); // is the number 123

Specifications

Specification Status Comment
HTML Living Standard
search
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
searchChrome Full support YesEdge Full support 12Firefox Full support 22
Notes
Full support 22
Notes
Notes Before Firefox 53, the search property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false".
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22
Notes
Full support 22
Notes
Notes Before Firefox 53, the search property returned wrong parts of the URL. For example, for a URL of http://z.com/x?a=true&b=false, search would return "", rather than "?a=true&b=false".
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.