toolman Posted January 23, 2013 Share Posted January 23, 2013 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2013 Share Posted January 23, 2013 Either use an array or do the comparison three times. Quote Link to comment Share on other sites More sharing options...
toolman Posted January 23, 2013 Author Share Posted January 23, 2013 Would this be correct: if (links[i].getAttribute("href") == ('http://www.website1.com','http://www.website2.com')) { Quote Link to comment Share on other sites More sharing options...
toolman Posted January 23, 2013 Author Share Posted January 23, 2013 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); } } } } Quote Link to comment Share on other sites More sharing options...
.josh Posted January 23, 2013 Share Posted January 23, 2013 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.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.