window.open() triggers popup blocker
See original GitHub issueI 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:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Hey @acupajoe nice trick indeed ! That’s awesome, thank you for taking the time !
@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/