question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window.open() triggers popup blocker

See original GitHub issue

I used to use the original SWAL and I have a simple modal with two different actions on the two buttons (confirm & cancel) : confirm will open a new modal, cancel will open a page in a new tab. My code looked like this :

var dialog = swal({
    title: 'My Title',
    html: html,
}, function( isConfirm ){
    if ( isConfirm ) {
        swal({
            title: 'Other title',
            html: html2,
        });
    }else{
        window.open( myUrl, '_blank' );
        swal.closeModal();
    }
});

No problems with my old code, did the just just fine. I’m in the process of upgrading to SWAL 2 which I much prefer. However, I’m experiencing an issue with the similar code ported to SWAL 2 : the window.open() method will trigger the popup blocker because the browser thinks it’s not triggered by the user (in reality though, it totally is since the user has to click on my “cancel” button). Here’s what my code looks like now :

var dialog = swal({
    title: 'My Title',
    html: html,
}).then(function(){
    swal({
        title: 'Other title',
        html: html2,
        showCancelButton: true,
    });
},function(dismiss){
    if ( dismiss === 'cancel' ) {
        window.open( myUrl, '_blank' );
        swal.closeModal();
    }
});

You can test it on JsFiddle : https://jsfiddle.net/z8491etu/

Any way to go around this ? I’m not sure how to let the browser know that the “dismiss” method is indeed called after a user action. Thanks !

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Corioucommented, Jul 5, 2016

Hey @acupajoe nice trick indeed ! That’s awesome, thank you for taking the time !

2reactions
acupofjosecommented, Jul 4, 2016

@Coriou: Based on this question: https://stackoverflow.com/questions/9514698/bypass-popup-blocker-on-window-open-when-jquery-event-preventdefault-is-set

It’s because of the asynchronous nature of promises. You can do a semi-hackish manual bind though if you really want to get this working [Tested working on chrome 51.0.2 | firefox 47.0.1 | IE 11 (with polyfill)] https://jsfiddle.net/t7hfffch/2/

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Avoid browser popup blockers - Stack Overflow
The general rule is that popup blockers will engage if window.open or similar is invoked from javascript that is not invoked by direct...
Read more >
How to avoid browser pop-up blockers on window.open - Yaplex
The better way to altogether avoid pop-up blocking in browsers is to open a new window as a result of a user action,...
Read more >
Popups and window methods - The Modern JavaScript Tutorial
A popup can be opened by the open(url, name, params) call. It returns the reference to the newly opened window. Browsers block open...
Read more >
Avoid pop-up blocker settings when opening new Window.
It looks like you open a window, close it, and then open another one. This is likely what is triggering the popup blocker....
Read more >
How to Avoid Browser Popup Blockers with JavaScript Code?
To avoid the permission popup for opening the popup, we should use avoid calling window.open in a function that returns a promise or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found