Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable for if window.location.href.indexOf() [duplicate]

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

like image 382
Brandon Hellman Avatar asked Dec 20 '22 03:12

Brandon Hellman


2 Answers

Check like this

if (urls.indexOf(window.location.href) > -1)
like image 87
Sudharsan S Avatar answered Dec 24 '22 03:12

Sudharsan S


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)
like image 31
Adil Avatar answered Dec 24 '22 01:12

Adil