简体   繁体   English

window.open 在用户操作期间被阻止为弹出窗口

[英]window.open blocked as popup during user action

Firefox is blocking my use of window.open and blocking it as if it's an unwanted popup. Firefox 阻止了我对 window.open 的使用并将其阻止为不需要的弹出窗口。

I have a ui with a button.我有一个带按钮的用户界面。

When the button is pressed I make a call to a service to fetch some data and then use the result of that call as input to the url I window.open() to.当按钮被按下时,我调用一个服务来获取一些数据,然后使用该调用的结果作为我 window.open() 到的 url 的输入。

The psudocode looks something like:伪代码如下所示:

onClick = (selectedAccount, $event){
  this.serviceBroker.getUrl(selectedAccount).then(data => window.open(data.url, '_blank', 'noreferrer'));
}

The above code will display a message in Firefox saying popup blocked would you like to allow this popup?上面的代码将在 Firefox 中显示一条消息,说popup blocked would you like to allow this popup?
I'm assuming this is happening because the call to window.open is being passed into a service that doesn't have the user action context.我假设发生这种情况是因为对 window.open 的调用被传递到没有用户操作上下文的服务中。

If I change the code to be:如果我将代码更改为:

onClick = selectedAccount, $event){
  window.open(<hardcoded_url>,'_blank', 'noreferrer');
}

Then the new tab opens and I do not receive the unwanted popup blocked然后新标签打开,我没有收到popup blocked的不需要的popup blocked

I've tried binding the context of function with the $event as the resolve call of the promise, but it didn't help我已经尝试将函数的上下文与 $event 绑定作为承诺的解析调用,但它没有帮助

let test = data => window.open(data.url, '_blank', 'noreferrer');
onClick = (selectedAccount, $event){
  this.serviceBroker.getUrl(selectedAccount).then(test.bind(this));
}

This is only occurring in Firefox and the popup is not blocked in IE, Chrome, or Edge.这仅在 Firefox 中发生,在 IE、Chrome 或 Edge 中不会阻止弹出窗口。

All browsers support window.open() to show popups, but there are some rules.所有浏览器都支持 window.open() 来显示弹出窗口,但有一些规则。 The window should be open as a result of a user action , for example, button click.窗口应该作为用户操作的结果打开,例如,单击按钮。 Still, if you add async call inside of the button click event and try to open the popup window inside of then/callback , it is no longer considered as a user action, and such popup will be blocked .尽管如此,如果您在按钮单击事件内部添加异步调用并尝试在then/callback内部打开弹出窗口,不再将其视为用户操作,并且此类弹出窗口将被阻止

const popUp = (url) => {
       const isOpen = window.open(url, '_blank')
    
        if(isOpen==null){
           return true;
        }

       return false;
   };

 this.serviceBroker.getUrl(selectedAccount).then(data => popUp(data.url));

I tried to keep the async call on a button click, still, it was blocked.我试图在单击按钮时保持异步调用,但它仍然被阻止。

https://codesandbox.io/s/zen-field-1srdo?file=/src/Moondc.jshttps://codesandbox.io/s/zen-field-1srdo?file=/src/Moondc.js

The best workaround would be to do the open at first then fill it with the content once the async action completes.最好的解决方法是首先打开,然后在异步操作完成后用内容填充它。

let popUp = window.open(url, '_blank');

this.serviceBroker.getUrl(selectedAccount).then(data => {
            popUp.document.write(data.url);
            popUp.focus();
});

or simply,或者干脆,

let popUp= window.open(...);

 this.serviceBroker.getUrl(selectedAccount).then(data => {
          popUp.location = data.url;
    });

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

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