borna
borna

Reputation: 924

Opening multiple sites on the browser tabs with window.open

I have the following code and need to open two new tabs pointing to 2 web sites. For example: www.google.com and www.yahoo.com

It works only if I put one site in there

window.open("https://www.google.com");

but not when both lines

window.open("https://www.google.com");
window.open("https://www.yahoo.com");

Can you tell me what i need to do to be able to open both sites?

(function() {
  document.getElementById("btnAsync").addEventListener('click', makeRequest);

  function makeRequest() {
    var httpRequest = new XMLHttpRequest(); // Initiatlization of XMLHttpRequest
    if (!httpRequest) {
      alert(' Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === XMLHttpRequest.DONE) {
        if (httpRequest.status === 200) {
          alert(httpRequest.responseText);
        } else {
          alert('There was a problem with the request.');
        }
      }
    };

    window.open("https://www.google.com");
    window.open("https://www.yahoo.com");
  }
})();
<button id="btnAsync" type="button">Click Me</button>

Upvotes: 0

Views: 73

Answers (2)

Quentin
Quentin

Reputation: 944443

In the bad old days, it was common for malicious websites to fork bomb browsers by triggering an infinite loop of new windows.

They implemented protection against this by allowing new windows (and tabs) to be opened only when the function was triggered by a user event (e.g. a click but not a page load) and restricting this to opening a single window.

Some browsers may prompt the user to allow an additional window to open, but there is no way for a website to simply bypass this important security feature.

Upvotes: 1

Alexander Smirnov
Alexander Smirnov

Reputation: 408

I run your code. Both sites opened. But before it opens browser blocked popup window so i had to allow it to open. Try your code. And dont forget to allow popup after pressing button.

Upvotes: 1

Related Questions