The JavaScript location search property is used to get the search string of the current URL. It does not take any arguments and returns a string value representing the search string of the URL.
Here are some examples of how the search property can be used in JavaScript code:
- To get the search string of the current URL, you can use the following code:
// get the search string of the current URL
const search = location.search;
- To log the search string of the current URL to the console, you can use the following code:
// log the search string of the current URL to the console
console.log('Current URL search string:', location.search);
- To compare the search string of the current URL with a certain value, you can use the following code:
// define the search string to compare
const search = '?key=value';
// compare the search string of the current URL with the specified value
if (location.search === search) {
// do something...
}
Overall, the search property provides a convenient way to get the search string of the current URL. It can be useful for tasks such as parsing the query string parameters or redirecting to a different page with different query string parameters, or for other operations that require accessing the search string of the URL.
Leave a Reply