Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one of the strings from an array are in the URL/window.location.pathname

I have an array:

var checkURL = ['abc123', 'abc124', 'abc125'];

How can I check if one of the strings in the array exists in the window.location.pathname?

I know individually I can use:

<script type="text/javascript">
    $(document).ready(function () {
        if(window.location.href.indexOf("abc123") > -1) {
           alert("your url contains the string abc123");
        }
    });
</script> 
like image 417
JayDee Avatar asked Mar 11 '13 17:03

JayDee


2 Answers

Use a for-loop for a linear search.

$(document).ready(function () {
    var checkURL = ['abc123', 'abc124', 'abc125'];

    for (var i = 0; i < checkURL.length; i++) {
        if(window.location.href.indexOf(checkURL[i]) > -1) {
            alert("your url contains the string "+checkURL[i]);
        }
    }
});
like image 191
tymeJV Avatar answered Oct 18 '22 14:10

tymeJV


Use a for loop:

$(document).ready(function () {
    for (var i = 0; i < checkURL.length; i++) {
        if(window.location.href.indexOf(checkURL[i]) > -1) {
           alert("your url contains the string " + checkURL[i]);
        }
    }
});
like image 40
MattDiamant Avatar answered Oct 18 '22 14:10

MattDiamant