JavaScript Tutorial: Check if a URL Contains a Specific Parameter using includes()

← PrevNext →

Last Updated: 11th June 2026

The browser's address bar contains the page URL, including query parameters that pass information such as filters, search terms, or tracking data. In JavaScript, you can quickly check whether a URL contains a specific string by using the includes() method. It’s a simple and readable way to detect parameters and control page behavior based on the URL.

In this tutorial, I'am sharing 3 simple examples that explain how to use the JavaScript includes() method to check if a URL contains a specific query parameter, keyword, or tracking value.

In-addition, I have shared a jQuery example at the end of this tutorial. It shows how to use includes() in jQuery.

🚀 Don't forget to read (at the end), why this method is still relevant.

You can extract or read URL parameters at the Server side also using ASP.NET (such as using QueryString).

Syntax of includes() Method

string.includes(searchString, position)

* searchString (required): The substring you want to search for.
* position (optional): The index in the string at which to begin searching. Default is 0.

The includes() method in JavaScript is considered more expressive because it clearly conveys the intent of the code, you're checking whether a specific substring exists within a larger string. Unlike older methods like indexOf(), which return a numeric position and require additional comparison logic (e.g., > -1), includes() returns a simple boolean value (true or false). This makes your code easier to read and understand at a glance. It also aligns more naturally with how we describe the task in plain language: "Does this string include that part?"

👉 Similar example: How to build a simple AutoComplete feature using JSON and JavaScript includes() method?

Example 1:

In the first example, the includes() method returns "true" if searchString is found, otherwise it returns a "false".

<script>
  function checkUrl() {
    if (window.location.href.includes('?pg=')) {
        console.log("The URL contains ?pg");
    }
}
</script>
Try it

Example 2:

This particular code checks if a URL contains a specific query parameter.

const url = "https://www.encodedna.com/products?category=electronics&page=2";

if (url.includes("category=")) {
  console.log("The URL contains the category parameter.");
} else {
  console.log("The category parameter was not found.");
}

Example 3:

This code will check if a URL contains a Tracking parameter

<script>
    const url = "https://www.encodedna.com/?utm_source=google&utm_medium=cpc";

    if (url.includes("utm_source=")) {
      console.log("Tracking parameter detected.");
    } else {
      console.log("No tracking parameter found.");
    }
</script>

Using includes() in jQuery

Alternatively, you can use the includes() method in your jQuery code.

👉 Remember: jQuery does not have its own includes() method. The method is native JavaScript feature, introduced in ES6. But you can use it inside jQuery code, it does not belong to the jQuery library.

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
  <button id="checkUrl">Check URL</button>
</body>
<script>
  const url = "https://www.encodedna.com/?pg=12";
  $(document).ready(function () {
    $("#checkUrl").on("click", function () {
      if (url.includes('?pg=')) {
        console.log("The URL contains ?pg=");
      } else {
        console.log("The URL does not contain ?pg=");
      }
    });
  });
  </script>
</html>

Conclusion

Using JavaScript's includes() method is one of the quickest and easiest ways to check whether a URL contains a specific string or query parameter. As shown in the examples above, it requires minimal code and is easy to read, making it a popular choice for client-side URL validation and conditional logic.

Is it still relevant?

Even with newly added APIs such as URL and URLSearchParams, the includes() method remains highly relevant because it is simple, fast, and supported by all modern browsers.

← PreviousNext →