1

I have a webpage page full of links, with check boxes next to them showing that I've clicked them. There are hundreds of links, and I would rather not click through all of them. What I would like to do is write a script that performs a "click" on each of the links, so that I don't have to.

var links = document.getElementsByClassName("some-class");

for(var i = 0; i < links.length; i++) {
   links[i].click();
}

My code simply opens the first link and navigates away from the original page, which of course stops execution of the code.

Can Javascript open links without navigating to them, such as in a new window or tab? If not, with what language can this be scripted?

2
  • You may want to check out Watir Commented Sep 16, 2015 at 0:04
  • Have you tried adding links[i].target = "_blank"; before the line where you .click()? Or JS can get the URL from each element's href property and use window.open() with that URL. Commented Sep 16, 2015 at 0:15

1 Answer 1

1

If opening all the links on a new tab solves your problem you should try the code below.

<a href="https://www.link1.com">First Link</a>
<a href="https://www.link2.com">Second Link</a>
<a href="https://www.link3.com">Third Link</a>
<button id="openAll">Open All</button>

<script>
    $("#openAll").click(function(){
        $("a").each(function(){
            window.open($(this).attr("href"), '_blank'); 
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.