Home > Blockchain >  Looping through array of file urls opens only first URL Javascript
Looping through array of file urls opens only first URL Javascript

Time:11-11

I've this short script to open URL's:

var x = document.querySelectorAll("a.Mylinks");
var myarray = [];
for (var i = 0; i < x.length; i  ) {
  var link = x[i].href;
  myarray.push(link);
}
for (var i = 0; i < myarray.length; i  ) {
  window.open(myarray[i], "_blank");
}

Array responds back with correct URLs, first URL opens and that is it, loop stops. Any ideas why this is happening and how this can be fixed? Any additional links or information would be helpful.

Thanks in advance.

CodePudding user response:

The problem you're facing is that browser you're using blocks an opening of too much tabs at the same time. Try disabling that blocking, usually you can find the needed button for that in the address bar.

CodePudding user response:

**Note Browser can block as opening of too much tabs at the same time.**

var x = document.querySelectorAll("a.Mylinks");
var myarray = [];
for (var i = 0; i < x.length; i  ) {
  var link = x[i].href;
  window.open(x[i].href,Math.floor(Math.random() * 80000));
}
<a class='Mylinks' href='https://google.com'></a>
<a class='Mylinks' href='https://stackoverflow.com'></a>
<a class='Mylinks' href='https://nodejs.org'></a>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Just change the target name from _blank to random series like "_blank" i, then check it.

 for (var i = 0; i < myarray.length; i  ) {
       window.open(link, '_blank'   i); 
 }
  • Related