How to Check If the Url Contains a Given String

How to check if the URL contains a given string?

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>

Javascript: How to check if URL contains a word

The method is String.prototype.indexOf()

if(window.location.href.indexOf("Desktop") > -1) {
alert("Alert: Desktop!");
}

and you don't need DOM Ready for this.

Check if url contains string with JQuery

Use Window.location.href to take the url in javascript. it's a
property that will tell you the current URL location of the browser.
Setting the property to something different will redirect the page.

if (window.location.href.indexOf("?added-to-cart=555") > -1) {
alert("found it");
}

How to check if the URL contains a number?

As gaetanoM suggested, a regular expression would be the easiest way to do it.

if (window.location.href.match(/\d/)) {
alert('contains a number');
} else {
alert('does not contain a number');
}

Check if router.url contains specific string?

If you want something basic that works:

if (this.router.url.indexOf('/test/sort') > -1) {
this.active = 0;
}

how to check if the url contains multiple strings. Javascript/jquery

You could break it into two parts and call indexOf twice instead of trying to check for both strings in the same call. Something like this:

if(window.location.href.indexOf("name=franky") > -1 && 
window.location.href.indexOf("gender=male") > -1)

How to check if URL contains something

If you want to check what the URL contains, you can just use the in method built in with Python strings.

if "Block.aspx?c=475412" in driver.current_url: # check if "Block.aspx?c=475412" is in URL
print("Block.aspx is in the URL")

check if string contains url anywhere in string using javascript

Try:

if(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?").test(status_text)) {
alert("url inside");
}


Related Topics



Leave a reply



Submit