📅  最后修改于: 2023-12-03 15:21:06.901000             🧑  作者: Mango
The URL.search
property returns the query string part of a URL, including the leading ?
character. It allows you to access and manipulate the query parameters present in the URL.
url.search
A string containing the query parameters of the URL.
const url = new URL("https://www.example.com/search?q=webapi&lang=en");
// Get the query parameters
console.log(url.search);
// Output: "?q=webapi&lang=en"
// Change the query parameters
url.search = "page=1&category=books";
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books"
// Access individual query parameters
console.log(url.searchParams.get('q'));
// Output: "webapi"
// Add a new query parameter
url.searchParams.append('sort', 'asc');
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books&sort=asc"
// Remove a query parameter
url.searchParams.delete('lang');
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books&sort=asc"
URL.search
returns a string that represents the query string of a URL. If the URL doesn't have a query string, an empty string is returned.&
and represented as key=value
. The leading ?
character is part of the query string.url.search
, you can modify the query parameters of the URL.URL.searchParams
property provides methods to access and manipulate individual query parameters conveniently, such as get()
, append()
, and delete()
.For more information and detailed documentation, refer to the MDN web docs for URL.search.
Markdown version:
## Web API URL.search 属性
The `URL.search` property returns the query string part of a URL, including the leading `?` character. It allows you to access and manipulate the query parameters present in the URL.
### Syntax
url.search
### Return Value
A string containing the query parameters of the URL.
### Example Usage
```javascript
const url = new URL("https://www.example.com/search?q=webapi&lang=en");
// Get the query parameters
console.log(url.search);
// Output: "?q=webapi&lang=en"
// Change the query parameters
url.search = "page=1&category=books";
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books"
// Access individual query parameters
console.log(url.searchParams.get('q'));
// Output: "webapi"
// Add a new query parameter
url.searchParams.append('sort', 'asc');
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books&sort=asc"
// Remove a query parameter
url.searchParams.delete('lang');
console.log(url.href);
// Output: "https://www.example.com/search?page=1&category=books&sort=asc"
URL.search
returns a string that represents the query string of a URL. If the URL doesn't have a query string, an empty string is returned.&
and represented as key=value
. The leading ?
character is part of the query string.url.search
, you can modify the query parameters of the URL.URL.searchParams
property provides methods to access and manipulate individual query parameters conveniently, such as get()
, append()
, and delete()
.For more information and detailed documentation, refer to the MDN web docs for URL.search.