• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to see if URL Contains Specific Text

Using jQuery to see if URL Contains Specific Text

April 12, 2022 Leave a Comment

We can use jQuery to see if the URL contains a word or parameter by using window.location, the href property, and the indexOf() method.

if( window.location.href.indexOf("wordWeWantToCheck") > -1) {
  //Do something
}

To see if the URL contains a word we first need to get the url of the current page we are on. We do this by using window.location. We then need to get the href property of window.location. This will return the current url of the page.

Once we have the url of the page, we can check it for our word using the indexOf() method. In the code above, the word we are looking for is, wordWeWantToCheck.

Finally, we put this in an if else conditional statement so that if the word we want is in the URL, we do one thing, and if it is not, we do something else.

Let’s see this example in action below.

How to Get the Current URL and see if it Contains any Word

We will have 2 steps in this example. In the first step, we will get the current URL of the page. We will store it as a string.

In the second step, we will let the user input any characters or word they want. We will then check to see if that is contained in our URL. The current url of this page is: http://theprogrammingexpert.com/using-jquery-to-see-if-url-contains/.

Here is our HTML set up:

<div id="div1">
  <input type="text" id="textInput">
  <p>Check if the String above is in the current url: http://theprogrammingexpert.com/using-jquery-to-see-if-url-contains/</p>
  <div class="click-me">Check URL</div>
  <div id="result"></div>
</div>

In the JavaScript code, we will first get the url as we did in the first part using the window.location object and the href property.

Once we have the URL, we will need to get the string the user has entered using the val() method. We will then use the indexOf() method to check if this string is contained in our URL.

We will finally let the user know if the string is contained in the URL by using the text() method.

Here is the JavaScript code:

$('.click-me').click(function(){
  //Get the current URL
  var url = window.location.href;
  
  //Get the user input
  var userString = $("#textInput").val();
  
  //Check if User string is in URL
  if( url.indexOf(userString) > -1 ){
    $("#result").text(userString + " IS in the URL above");
  } else {
    $("#result").text(userString + " is NOT in the URL above");
  }
});

The final code and output for this example is below:

Code Output:

Check if the String above is in the current url:
http://theprogrammingexpert.com/using-jquery-to-see-if-url-contains/

Check URL

Full Code:

<div id="div1">
  <input type="text" id="textInput">
  <p>Check if the String above is in the current url: http://theprogrammingexpert.com/using-jquery-to-see-if-url-contains/</p>
  <div class="click-me">Check URL</div>
  <div id="result"></div>
</div>

<script>

$('.click-me').click(function(){
  var url = window.location.href;
  var userString = $("#textInput").val();
  if( url.indexOf(userString) > -1 ){
    $("#result").text(userString + " IS in the URL above");
  } else {
    $("#result").text(userString + " is NOT in the URL above");
  }
});

</script>

Hopefully this article has been useful to help you understand how to use jQuery to see if URL Contains a certain string.

Other Articles You'll Also Like:

  • 1.  jQuery Get Last Child
  • 2.  Using jQuery to Remove Background Color
  • 3.  Using jQuery to Increment Value on Click
  • 4.  jQuery append – Insert Element at End of Another Element
  • 5.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 6.  jQuery next – Get the Next Element of a Div
  • 7.  Using jQuery to Remove the Id of a Div
  • 8.  Using jQuery to Change Text of HTML Element
  • 9.  Using jQuery to Add Class to HTML Element
  • 10.  How to Set Checkbox Checked With jQuery

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy