简体   繁体   English

window.open() 在从 promise 调用时被浏览器阻止

[英]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 )然后,在承诺中,使用您打开的窗口并填充它(您可以使用win.location

The promise fires in response to you getting the HTTP response back from the Ajax request.当您从 Ajax 请求中获取 HTTP 响应时,promise 会触发。 That isn't a user triggered event, so popups are blocked.这不是用户触发的事件,因此会阻止弹出窗口。 Use the window the user gives you instead of creating a new one.使用用户给你的窗口,而不是创建一个新窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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