-1

I need to test long list of links to see if they're real links or broken, i had the daft idea that rather than just click them one at a time, open every one at once, the work my way through the open tabs. The html looks something like this:

<a href="www.somelink.com"></a>
<a href="www.somelink.com"></a>
<a href="www.somelink.com"></a>
<a href="www.somelink.com"></a>

with the jQuery being:

$(document).ready(function(e) {
  $('a').attr('target','_blank');
  $('a').click();
});

The target="_blank" works just fine, but the click does not, why is my browser not going crazy and trying to open 50 news pages at once?

Huangism
  • 16,278
  • 7
  • 48
  • 74
Tim Wilkinson
  • 3,761
  • 11
  • 34
  • 62
  • Are the number of 'clicks' coinciding with the number of anchors in your html? Id look up how Jquery selectors work again. – EyeOfTheHawks Jul 17 '14 at 12:35
  • this is done for obvious security reasins...would you want your browser opening all sorts of unexpected windows/pages/sites just beacuse someone can throw that in a script? – charlietfl Jul 17 '14 at 12:36
  • i half assumed that to be the case, but for running a local test site i wondered if there was a way round it. someone clearly doesn't like the question running round down voting! if it cant be done then it cant be done, cheers! – Tim Wilkinson Jul 17 '14 at 12:39
  • i wish you had to explain a downvote rather than just downvote without constructive feedback. – Tim Wilkinson Jul 17 '14 at 12:50

2 Answers2

3

WHat you can do is loop through the elements and capture their href and use window.open

$("a").each(function()  {
   window.open( this.href)
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

You can try this:

$('a')[0].click();

But it will fire click on only first anchor tag.

Complete code:

$('a').attr('target','_blank');
alert("After this alert 4 link will open");
for(var i=0;i<=3;i++)
{
    $('a')[i].click();
}

Demo

Manwal
  • 23,450
  • 12
  • 63
  • 93