简体   繁体   中英

window.open() is blocked by browser when is called from promise

I have code like this:

window.open('https://api.instagram.com/oauth/authorize/',
'_blank',
'width=700,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0,modal=yes');

This works fine when is called from any place of code, but when I use it in promise (see below), it is always blocked by browser. Any suggestions?

action().success(function (r) {
  // window.open(...);
}

Promises are from angular.

            var newTab = $window.open('', '_blank');
        promise
            .then(function () {
                var url = '...';
                newTab.location.href = url;
            });

The solution I use to this problem is to

  1. immediately open the window and keep a reference (when it's legal, that is in the event handler)
  2. launch the asynchronous operation
  3. then, in the promise, use the window you opened and fill it (you may use win.location )

The promise fires in response to you getting the HTTP response back from the Ajax request. That isn't a user triggered event, so popups are blocked. Use the window the user gives you instead of creating a new one.

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