0

I have a button that is purposely designed to open multiple pages at once, it is the main feature of the button.

I tried using:

(1)

urls.forEach(url => {
  window.open(url);
});

(2) promises with a delay on them but that did not work either.

(3) multiple a tags and trying to simulate a human click however, that did not work either.

var element = document.createElement("a");
element.href = tempUrl;
element.innerHTML = "temp";
element.id = "tempAtag";
element.target = "_blank";
document.getElementById("dashboardID").appendChild(element);
element = document.getElementById("tempAtag");
var box = element.getBoundingClientRect(),
coordX = box.left + (box.right - box.left) / 2,
coordY = box.top + (box.bottom - box.top) / 2;
var simulateMouseEvent = function (element, eventName, coordX, coordY) {
  element.dispatchEvent(
    new MouseEvent(eventName, {
      view: window,
      bubbles: true,
      cancelable: true,
      clientX: coordX,
      clientY: coordY,
      //button: 0,
    })
  );
};
simulateMouseEvent(element, "mousedown", coordX, coordY);
simulateMouseEvent(element, "mouseup", coordX, coordY);
simulateMouseEvent(element, "click", coordX, coordY);

This does work for the first link but I get a warning in the console saying I am trying to open multiple tabs with only one interaction. So it works for one link but not for the rest.

I got the code from Simulate a REAL HUMAN mouse click in pure javascript?

(Note: I did delete my a tags after each iteration and I did test it out, there are no duplicates)

I ran out of ideas. Any ideas?

(I have looked at other solutions and none of what I came across has worked for me)

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Arneev
  • 39
  • 8
  • Does this answer your question? [Open a URL in a new tab (and not a new window)](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window) – Tigger Jan 31 '21 at 02:16
  • I looked through the various answers and no, sadly it does not answer my question. – Arneev Jan 31 '21 at 02:35
  • Most browsers prevent opening many new windows to get rid of pop-ups. This might be why nothing works. – Davedude Jan 31 '21 at 02:50
  • Yea I've noticed lol. I was hoping there would be a workaround but over the years I guess they got "patched". – Arneev Jan 31 '21 at 03:22

1 Answers1

0

The following works with a big IF attached.

That if is you need to click the "allow pop-up" warning that will appear in the address bar and continue to allow pop-ups from that site.

You also need to have pop-ups to load in a new tab.

I still think this is a duplicate question with Open a URL in a new tab (and not a new window)

var id = 0, u = ['12','34','56','78'];
function openNextTab(){
    if (u[id]){
        var x = window.open("https://some.site/id:"+ u[id],"id"+u[id]);
        id++;
        setTimeout(openNextTab,2000);
    }
}
function openTabs(e){
    e.stopPropagation();e.preventDefault();
    openNextTab();
}
window.onload = function(){
    var b = document.getElementById("openLinks");
    b.addEventListener("click",openTabs,false);
}
Tigger
  • 8,980
  • 5
  • 36
  • 40
  • Thanks! It works! I tried using promises with a delay so I'm not sure why this is working but it is :) – Arneev Jan 31 '21 at 18:21