The Location Search property in the HTML DOM is used to get or modify the query string of the current URL.
This query string appears after the question mark (?) and is commonly used to pass data in a URL.
- It returns the entire query part, including the ? symbol
- Useful for reading URL parameters like IDs, filters, or search values
- Can be set to update the URL and reload the page
Syntax:
- It returns the location search property.
location.search- It is used to set the location search property.
location.search = searchStringProperty Value: The searchString is used to specify or set the query (search) part of the URL, including the question mark (?).
Return Value: It returns a string which represents the search value.
Example: This example display the search part of URL.
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM Location search Property
</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>
HTML DOM Location search Property
</h2>
<button onclick="myGeeks()">Click Here!</button>
<!-- script to return location search property -->
<script>
function myGeeks() {
const url = document.createElement('a');
url.setAttribute('href',
'https://www.geeksforgeeks.org/pathname/');
window.alert("URL search value of current page: "
+ url.search);
}
</script>
</body>
</html>