Last Updated: 21st October 2025
The address bar of a web browser displays the URL of the current webpage. URLs often include parameters, extra bits of information used to pass data between pages or interact with elements on the site. Sometimes, you may need to check if a specific parameter exists in the URL. In JavaScript, you can use the indexOf() method to find out if a particular string is present. This can also be done using jQuery.👉 Here's an updated version: You can improve the readability and clarity of the below examples by using the includes() method instead of "indexOf()". The includes() method is more expressive and directly checks if a string contains a specific substring. This method was introduced in ES6 (ECMAScript 2015), which means it's supported in modern browsers only.

Note: You can extract or read URL parameters at the Server side also using Asp.Net (such as using QueryString).
Whether you are using jQuery or JavaScript, you can simply use the indexOf() method with the href property of windows.location object.
Syntax for indexOf()
string.indexOf(searchvalue)
The indexOf() method "returns the position" (as a number) of the first occurrence of a specified substring within a string. If the substring isn't found, it returns -1. This makes it useful for checking whether a particular value exists.
In JavaScript, you can use indexOf() with the href property of the window.location object to check if a URL contains a specific string. For example:
let position = window.location.href.indexOf('&book=');If the method finds the string "&book=" in the URL, it will return the location (a number) and assign the location to the variable "position".
Note: The character & indicates that I am passing an extra parameter to the URL.
Let's see an example each in jQuery and JavaScript.
1) Using jQuery
I wish to check if the URL contains the parameter "?book=" in jQuery. My script would be …
if (window.location.href.indexOf('?book=') > 0) { // ... do something }
Here's the complete code (with markup) using jQuery.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<input id="bt" type="button" value="Check URL" />
</body>
<script>
$(document).ready(function () {
$("#bt").on("click", function () {
if (window.location.href.indexOf('?book=') > 0) {
console.log("The URL contains ?book");
}
});
});
</script>
</html>2) Using JavaScript
If you're using plain JavaScript, you can trigger a function when a button is clicked. Here's a simple example:
<input id="bt" type="button" onclick="checkUrl()" value="Check URL" /> <script> function checkUrl() { if (window.location.href.indexOf('?book=') > 0) { console.log("The URL contains ?book"); } } </script>
The result will be the same as we saw in the jQuery example above.
In my next post, I have shared an example showing how to replace a given string with another string in the URL bar using JavaScript or jQuery.
