--- title: 'Location: search' slug: Web/API/Location/search translation_of: Web/API/Location/search ---
{{ApiRef("Location")}}

{{domxref("Location")}} 接口的 search 属性会返回一段 {{domxref("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
{{SpecName('HTML WHATWG', '#dom-location-search', 'search')}} {{Spec2('HTML WHATWG')}} Initial definition.

Browser compatibility

{{Compat("api.Location.search")}}