JS open new tab and avoid popup blocker

423 views Asked by At

This is the code:

function openUrl(url) {
            var newTab = window.open(url, '_blank');
            if (!newTab || newTab.closed) {
                
            } 
            }   
        }

if there is popup blocker the tab isnt opening so i need to do something that it will be open with popup blocker on. If you have any solution that doesnt involve new button I will be happy to hear! If there is no solution so maybe add confirm and work with that. I tried to add confirm in the if and open new tab with the click trigger but nothing.

I tried to put this in the if:

var a = document.createElement('a');
  a.target="_blank";
  a.href=url;
  a.click();

but nothing Please help Thank you!

1

There are 1 answers

1
pgksunilkumar On
var windowReference = window.open("http://www.example.com", "_blank");
if (!windowReference) {
    // popup was blocked
    window.location.href = "http://www.example.com";
}