简体   繁体   中英

Fallback if window.open() is blocked by popup blockers?

I want to default to target=_blank if window.open() fails.

Eg. The user clicks a link that invokes window.open() . However, the popup is blocked by a popup blocker. In this scenario, I want a new window to open instead. Is this possible?

PS I have jQuery loaded.

Try this

Assuming that this is your A tag

<a href="http://www.w3schools.com" id="myLink" target="_blank">Click me</a>

Then using jquery, add a click event handler to this:

$("#myLink").click(function(e){
    if(!window.open("http://www.w3schools.com")){
        e.preventDefault();
    } 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM