What I want to do is use a variable with window.location.href.indexOf() This is my code right now, I want to be able to shorten it.
var urls = [
'https://www.example.com/'
,'https://www.example2.com/'
,'https://www.example3.com/'
,'https://www.example4.com/'
,'https://www.example5.com/'
];
// if ((window.location.href.indexOf(""+urls+"") > -1) Does not work
if (
(window.location.href.indexOf("https://www.example.com/") > -1)
|| (window.location.href.indexOf("https://www.example2.com/") > -1)
|| (window.location.href.indexOf("https://www.example3.com/") > -1)
|| (window.location.href.indexOf("https://www.example4.com/") > -1)
|| (window.location.href.indexOf("https://www.example5.com/") > -1)
) {
//do stuff
}
I've included what I've tried in the code but it doesn not work. javascript and jquery both work for me
Check like this
if (urls.indexOf(window.location.href) > -1)
You need to use indexOf
on array to search the window.location.href
within array of urls.
Change
if ((window.location.href.indexOf(""+urls+"") > -1)
To
if (urls.indexOf(window.location.href) > -1)
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