Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  4] [ 2]  / answers: 1 / hits: 89468  / 9 Years ago, thu, january 7, 2016, 12:00:00

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!


More From » javascript

 Answers
16

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.


[#63814] Tuesday, January 5, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
;