Javascript: How to check if URL contains a word
I want to check if the URL in a Browser contains the word "Desktop" (I startet the html file from the Desktop). Its url is: "file:///C:/Users/Joe/Desktop/TestAlert.html"
But there should appear an alert, but this doesnt works.
Here is my code:
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
if(window.location.href.contains("Desktop") > -1) {
alert("Alert: Desktop!");
}
});
</script>
</head>
<body>
<h1>Test001</h1>
</body>
</html>
I tested this in Firefox, Chrome and Internet Explorer. It would be very nice, if someone can help me!
Solution 1:
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.
Solution 2:
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
if(window.location.href.indexOf("Desktop") > -1) {
alert("Alert: Desktop!");
}
});
</script>
</head>
<body>
<h1>Test001</h1>
</body>
</html>
Please try this it will give you sollution