Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the URL contains a given string?

How could I do something like this:

<script type="text/javascript"> $(document).ready(function () {     if(window.location.contains("franky")) // This doesn't work, any suggestions?     {          alert("your url contains the name franky");     } }); </script> 
like image 856
RayLoveless Avatar asked Jan 04 '11 18:01

RayLoveless


People also ask

How do you check if the URL contains a given string in C#?

Use the StartWith() method in C# to check for URL in a String.

How do you check if the URL contains a given string in Python?

get(url) send = driver. find_element_by_id("NextButton") send. click() if (driver. find_elements_by_css_selector("a[class='Error']")): print("Error class found") except ValueError: print("Something went wrong checking the URL.


2 Answers

You need add href property and check indexOf instead of contains

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <script type="text/javascript">    $(document).ready(function() {      if (window.location.href.indexOf("franky") > -1) {        alert("your url contains the name franky");      }    });  </script>
like image 135
J.W. Avatar answered Oct 10 '22 15:10

J.W.


if (window.location.href.indexOf("franky") != -1) 

would do it. Alternatively, you could use a regexp:

if (/franky/.test(window.location.href)) 
like image 34
NickFitz Avatar answered Oct 10 '22 13:10

NickFitz