【问题标题】:Javascript window.open is blocked by IE popup blockerJavascript window.open 被 IE 弹出窗口阻止程序阻止
【发布时间】:2009-05-20 16:29:57
【问题描述】:

谁能帮忙,我有一个被阻止的弹出窗口。这是一个弹出窗口,因为有人点击了我网站上的打印图片。

当弹出窗口通过onclick 出现时,我认为 IE 不应该阻止这些?

有人可以帮忙吗?如果启用了弹出窗口阻止程序,child1 变量始终返回为 NULL...

也许问题在于onclick 事件随后将控制权传递给一个新函数,该函数加载一个 html 文件并执行child.document.write

这是我的简单代码..

var width = 800;
var height = 600;
var left = parseInt((screen.availWidth / 2) - (width / 2));
var top = parseInt((screen.availHeight / 2) - (height / 2));
var windowFeatures = "width=" + width + ",height=" + height 
   + ",menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,left=" 
   + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;      

child1 = window.open("about:blank", "subWind", windowFeatures);

【问题讨论】:

    标签: javascript popup


    【解决方案1】:

    问题在于,如果您导航到当前主机中的某个位置,open 只会返回对窗口的引用。您正在导航到不在您的主机中的 about:blank。

    尝试将空白.htm 文件添加到您的站点并打开它。我仍然不确定 document.write 是否被允许该文档不会被打开以进行写入,但是您也许可以操作现有空白文档的 DOM。

    【讨论】:

    • 是的,谢谢你就是这样,基本上我需要从我当前的网络加载一个文件而不是空白......所以我创建了一个空的 html 文件..加载它并使用 windows.write写我的html ..谢谢你,感谢其他人的cmets
    【解决方案2】:

    当设置为Medium 过滤级别时,Internet Explorer 弹出窗口阻止程序不会阻止由 JavaScript 打开的窗口(如果它们是由用户操作启动的)。以下在 IE 6、7 和 8 中运行良好:

    <script type="text/javascript">
    function openWin() {
        var width = 800;
        var height = 600;
        var left = Math.floor((screen.availWidth - width) / 2);
        var top = Math.floor((screen.availHeight - height) / 2);
        var windowFeatures = "width=" + width + ",height=" + height +
                ",menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes," +
                "left=" + left + ",top=" + top +
                "screenX=" + left + ",screenY=" + top;
        child1 = window.open("about:blank", "subWind", windowFeatures);
        writeTo(child1);
    }
    function writeTo(w) {
        w.document.write('Test');
    }
    </script>
    <a href="#" onclick="openWin();return false;">Test</a>
    

    请注意,在新打开的窗口中使用document.write 在某些网络浏览器中不起作用。另请注意,这可能会在其他浏览器中触发弹出窗口阻止程序,即使它在 Internet Explorer 中显示。

    我已经看到从 onclick 事件调用 JavaScript 在某些情况下会触发弹出窗口阻止程序。这似乎与 far window.open() 来自 onclick 事件的方式有关。在调用window.open() 之前调用函数的函数级别太多。没有看到您的确切实现,我无法告诉您这是否是您遇到的问题。

    【讨论】:

      【解决方案3】:

      它会阻止它,因为它不是具有 target="_blank" 的锚。您正在以编程方式创建弹出窗口。

      在您提供的代码示例之后执行此操作。

      if (!child1) {
            alert('You have a popup blocker enabled. Please allow popups for www.yourSite.com');
      }
      

      【讨论】:

        【解决方案4】:

        请尝试代码

        var newWin = window.open(...);
        if (newWin && newWin.top) {
            // popup has opened
        } else {
            // popup has been blocked
        }
        

        【讨论】:

          【解决方案5】:

          我建议你用 target="_blank" 而不是 window.open() 制作一个虚拟 [form]。

          我希望它有效。

          问候。

          PD:我想将您的网站添加到“受信任的网站”不是一种选择,对吧?

          【讨论】:

            【解决方案6】:

            我在 10 月 11 日有个主意:

            let win = window.open();
            setTimeout(function(){
                win.customParam = 'xxxx';
            });
            

            在win中,您可以在窗口加载后通过计时器或循环获取customParam:

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-11-28
              • 2019-05-27
              • 2011-10-01
              • 2015-05-05
              相关资源
              最近更新 更多