Jump to content

list three websites in this like - "or" operator


toolman

Recommended Posts

Hi,

 

I am trying to specify three URLS in this line, but can't work out how to list them. I tried this, but it dodn't work:

 

if (links[i].getAttribute("href") == "http://www.website1.com" || "http://www.website2.com" || "http://www.website3.com") {

 

 

Any ideas how to acheive this?

 

Thanks

I've also tried this, but it doesn't work. My JS is not great:

 

var newWin=new Array();
newWin[0]="website1.com";	  
newWin[1]="website2.com";
window.onload = function() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
  if (links[i].getAttribute("href") == 'newWin') {
	 links[i].onclick = function() {
	    return !window.open(this.href);
	 }
  }
  }
}

You need to make a nested loop.  The outer loop would loop through links, and the inner loop would loop through your newWin array.  

 

Also, this is wrong:

 

if (links[i].getAttribute("href") == 'newWin') {

 

you are comparing the href value to the literal string 'newWin'. You want to compare it to newWin[ii] where ii is the counter for the inner loop (which you don't currently have).  But also, this would be an exact match.  You would actually want to either do a substring match or else first pull out the domain part of the href.  But even then, your newWin values don't have any subdomains listed, so you will need to add them in if you want to stick with exact matches... 

 

Also, supposing all that is sorted out, FYI, you are overwriting the onclick attribute w/ that new function.  So if anything else is already hooked to it, you are overwriting it. 

 

Also, are you using a framework like jQuery? This whole thing would be a lot easier if you were...

 

Also, it would help if you explained what you are actually trying to accomplish when you find matches..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.