1

I'm trying to open all links on a webpage, where the code for each link is approximately the following:

<a href="/tor/download.php/*jibberish_letters*" id="dlLink137836"><img border="0" src="/pic/down.png" alt="Download" title="Download"></a>

The script I'm currently attempting to use is the following, but I only get an 'undefined' error when I try to run it.

var links = document.querySelectorAll('a[title="Download"]');
for (var i = 0; i < links.length; i++) {
 window.open(links[i].href,"_blank");
}

Any help would be much appreciated. Thanks!

1
  • Your sample a tag doesn't have a title attribute but the image does. Any popup blockers installed? Mar 18, 2014 at 13:23

2 Answers 2

2

This worked for me:

var links = document.querySelectorAll('img[title="Download"]');
for (var i = 0; i < links.length; i++) {
    window.open(links[i].parentNode.href,"_blank");
}
1

This is because the attribute title="Download" isn't in the a tag, but in the img tag. This should works:

document.querySelectorAll('a img[title="Download"]');

Fiddle

But as @Nicolae suggested, the pop-up blocker blocks it, at least in my browser(Chrome) it happened. Furthermore, is a strange pratice, indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.