11

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!

Joe SharePoint
  • 113
  • 1
  • 1
  • 4

7 Answers7

35

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.

void
  • 36,090
  • 8
  • 62
  • 107
3
<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

2

You can use regexp

url = window.location.href;
if( url.match(/desktop/gi) ) {
   alert("Alert: Desktop!");
}

Using this you can check two or more words as well like "/desktop|home/gi"

Vegeta
  • 1,319
  • 7
  • 17
1

Use indexOf instead

console.log(window.location.href.indexOf("javascript"));

Same rules apply, anything > -1 means it has found something

Datsik
  • 14,453
  • 14
  • 80
  • 121
1

If you want to use jQuery, you'll need to link to its source code. Also, use indexOf instead of contains.

<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    if(window.location.href.indexOf("Desktop") > -1) {
       alert("Alert: Desktop!");
    } else {
        alert("window.location.href: " + window.location.href);
    }
});
</script>
</head>
<body>
    <h1>Test001</h1>
</body>
</html>

Tip: When you have Javascript problems, hit f12 in your browser to open the development tools and then f5 to refresh the page. Then check your console tab for error messages, e.g. 'Uncaught ReferenceError: $ is not defined', and your sources tab for any parts of your code that are underlined in red.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
0
var url = window.location.href;
            console.log(url);
            console.log(~url.indexOf("#product-consulation"));
            if (~url.indexOf("#product-consulation")) {
                console.log('YES');
                // $('html, body').animate({
                //     scrollTop: $('#header').offset().top - 80
                // }, 1000);
            } else {
                console.log('NOPE');
            }
alemac852
  • 99
  • 1
  • 11
0

if(window.location.href.indexOf("YOUR DESIRED WORD PUT HERE") > -1) { alert("The word is there!"); }

Works exactly you want!!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 22 '22 at 17:45