【问题标题】:Prevent blocked popup to be opened by Firefox阻止被阻止的弹出窗口被 Firefox 打开
【发布时间】:2019-03-26 14:08:03
【问题描述】:

对于 web 应用,我可以选择打开一个弹出窗口 (w = window.open) 为最终用户启用多屏。

当弹窗在没有用户输入的情况下被 js 打开时(加载之前的用户配置),Firefox 会按预期阻止它。

我可以用w == null 检测到它,如果用户不想要它们,我可以处理它以拥有一个没有弹出窗口的功能性应用程序。

但是,如果用户单击“允许此站点打开弹出窗口”,则 Firefox 会打开一个包含当前页面的弹出窗口。

是否有可能阻止 Firefox 打开被阻止的弹出窗口?

编辑:MWE

<html>
<body> 
<script type="text/javascript">
    function bug () {
        let w = window.open('', '', 'width=800, height=600');
        if (w == null) {
            console.log("blocked");
            return;
        }
        let p = w.document.createElement('div');
        p.className += "layout";
        let c = w.document.createElement('div');
        c.className += "content";
        c.style.height = '100%';
        c.innerHTML = "Hello world !";
        w.document.body.appendChild(p);
        p.appendChild(c);
    }

    document.body.innerHTML = "Hello fellow human beings";
    bug();
</script>
</body>
</html>

Edit2 :当前页面和弹出窗口的混合在我这边,主要问题仍然存在。

谢谢

【问题讨论】:

  • 请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于SO的相关主题;如果您遇到困难,请发布您的尝试minimal reproducible example,并注明输入和预期输出。
  • 您可能会以某种方式使用 window.open 来获得该结果。请发布您的代码。

标签: javascript firefox popup


【解决方案1】:

如果用户选择在弹出窗口被阻止后从被阻止的工具栏中打开弹出窗口,它将加载您提供给window.open 的 URL。你还没有给出一个,所以它会默认到同一页。

首先,通常的解决方案是让弹出窗口加载不同的页面,而不是让它加载相同的页面,然后尝试修改它的 DOM。然后在事后打开被阻止的弹出窗口仍然会加载具有不同内容的正确页面。

第二个解决方案更接近您的要求,将弹出 URL 设置为一个页面,该页面将立即关闭。在不被阻止的情况下,您可以在该页面加载关闭之前停止加载该页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 2014-03-14
    相关资源
    最近更新 更多