Skip to content Skip to sidebar Skip to footer

Java Script check string in the URL

Reading Time: < 1 minute

How to check if some string is exist or not in the url using java script.For a example,I want to check “.php” extension exist in the current url.This can be do like this.

   var currentUrl = document.URL;
   var str = currentUrl.indexOf('.php');
   if(str >= 0)
   {
      alert('php extension is exist');
   }
   else {
      alert('php extension doesn't exist');
   }

Usages

  • This can be use to get current page url(document.URL
  • Can be use to check extension of the current page
  • Can be use to check the position of something in the string or url
  • Can be use to check string/numbers,characters,…etc in the current url

The indexOf() method returns the index at which value can be found in the array.
This method returns -1 means that not exist.