【发布时间】:2021-07-12 19:36:52
【问题描述】:
Firefox 正在阻止我使用 window.open 并阻止它,就好像它是一个不需要的弹出窗口一样。
我有一个带有按钮的用户界面。
当按钮被按下时,我调用一个服务来获取一些数据,然后将该调用的结果用作我 window.open() 到的 url 的输入。
伪代码看起来像:
onClick = (selectedAccount, $event){
this.serviceBroker.getUrl(selectedAccount).then(data => window.open(data.url, '_blank', 'noreferrer'));
}
上面的代码将在 Firefox 中显示一条消息popup blocked would you like to allow this popup?
我假设这是因为对 window.open 的调用被传递到没有用户操作上下文的服务中。
如果我将代码更改为:
onClick = selectedAccount, $event){
window.open(<hardcoded_url>,'_blank', 'noreferrer');
}
然后新标签打开,我没有收到不需要的popup blocked
我尝试将函数的上下文与 $event 绑定为 promise 的解析调用,但没有帮助
let test = data => window.open(data.url, '_blank', 'noreferrer');
onClick = (selectedAccount, $event){
this.serviceBroker.getUrl(selectedAccount).then(test.bind(this));
}
这仅在 Firefox 中发生,并且在 IE、Chrome 或 Edge 中不会阻止弹出窗口。
【问题讨论】:
标签: javascript angular firefox