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>
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]);
}
}
});
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]);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With