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
Displaying a Number in Indian Format Using JavaScript
Clearrect Function Doesn't Clear the Canvas
Merge Two Arrays So That the Values Alternate
How to Remove a Key from a JavaScript Object
Why Does an Onclick Property Set with Setattribute Fail to Work in Ie
Display a Popup Only Once Per User
In JavaScript, How to Check If an Array Has Duplicate Values
Jasmine: Async Callback Was Not Invoked Within Timeout Specified by Jasmine.Default_Timeout_Interval
How to Use JavaScript or Jquery to Read a Pixel of an Image When User Clicks It
Select All Contents of Textbox When It Receives Focus (Vanilla Js or Jquery)
Short-Polling VS Long-Polling for Real Time Web Applications
How to Check Whether <Ng-Content> Is Empty? (In Angular 2+ Till Now)
How to Declare a Global Variable in a .Js File
Getting "Typeerror: Failed to Fetch" When the Request Hasn't Actually Failed
Is It Evil to Use Eval to Convert a String to a Function
What Does ${} (Dollar Sign and Curly Braces) Mean in a String in JavaScript