Using 'or' when checking against URLS in Javascript

javascript, jquery

Solution

This will be less verbose and cleaner if your list of urls is long:

var arrOfUrls = [url1, url2, url3]; //replace these with your url strings

var atLeastOneMatches = arrOfUrls.some(function(url) {
  return window.location.href.indexOf(url > -1);
});

if (atLeastOneMatches) {
  //do stuff here
}

Problem

I am using the following to do something if the specified URL matches that of the current page: ``` if(window.location.href.indexOf("collections/all/colourful") > -1) { ``` I have a whole bunch of these as I cant seem to specify a list of URLs to apply the function to. I would like to give a list of 10-40 urls to apply it to. I tried (using two URLs as an example): ``` if(window.location.href.indexOf("collections/all/colourful") || ("collections/all/notcolourful") > -1) { ``` and ``` if(window.location.href.indexOf("collections/all/colourful" || "collections/all/notcolourful") > -1) { ``` but neither seems to work. Sorry if this is basic, but I looked and couldn't find an answer anywhere. Thanks in advance.

Original source