关于javascript:在iframe中使用window.open的唯一弹出窗口

Unique popup window using window.open in an iframe

我有一个带窗口打开的iFrame(urluniqueIDwindowparams)。
iFrame位于单页应用程序中,并托管在另一台服务器中。
每次刷新或更改页面时,都返回IFrame页面,然后尝试启动相同的window.open。它不刷新已打开的窗口,而是创建同一窗口的实例。检查每个弹出窗口的window.name后,它会返回与其window name相同的uniqueID。如果将url设置为空白,则其行为符合预期。但是,当设置了URL时,它将创建窗口的新实例。

  • 销毁后,iFrame window.open的行为是否如此?
  • 我尝试在本地运行iFrame源,即使刷新后,打开的窗口也可以正常运行。
  • 我在IE,Firefox和Chrome中尝试过,但返回的行为相同。

更新:

Mike通过为iFrame源添加了沙箱属性,成功解决了Webkit浏览器的问题。现在,window.open方法可以按预期工作,并且不会在同一window.name的同一窗口中创建新实例。

但是,Mike仍然对Firefox没有运气。如果有人可以为此解决问题,将不胜感激。

以下视频中具有带框盒装属性的iFrame的Webkit浏览器行为。

See that the parent, even when refreshed still can detect that there is an already opened popup of the same name.
https://youtu.be/z5_xXRjY8Ow

以下视频中具有带盒装属性的iFrame的Firefox行为。

When the parent window is refreshed, the browser could not detect the already opened popup and creates another instance of the pop up with the same window.name.
https://youtu.be/uHaDveW1Sd0

有没有解决方法可以使Firefox像Webkit浏览器一样运行?

更新:

迈克发现在window.open中使用空白网址在firefox中表现正常。
但是仍然如何解决这个问题。

更新:

这是约翰尼! er Mike是另一个测试用例。
尝试使用Webkit浏览器和Firefox。
打开弹出窗口后,刷新页面,然后打开另一个弹出窗口
webkit浏览器将只有一个实例,但是firefox将创建一个新实例。
console.log(window.name)在打开的弹出窗口上,您将获得" Mike"作为窗口名称
https://bug1295839.bmoattachments.org/attachment.cgi?id=8782242


我在您的代码中看到了几个问题:

  • 您正在调用嵌套在另一个函数内的函数以关闭窗口,该函数永远不会被触发,因为此时无法调用父函数。

    1
    2
    3
    4
    5
    6
    function openWin(){
         /* code */
    }
    function closeWin(){
         /* code */
    }
  • 为了确保浏览器兼容以关闭窗口,我强烈建议在父窗口的生成的弹出窗口中设置并调用一个可注入函数

  • 注入函数的示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    myWindow.document.write(
           "<p>
    This is 'myWindow'
    </p>"

            +""
            +"function closeWindow()\\{" // sample injected function
            +"window.close();"
            +"\\}<\\/script>"
        );

    完整示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <!DOCTYPE html>
    <html>
    <body>

     <button onclick="openWin()">Open"myWindow"</button>
    <button onclick="closeWin()">Close"myWindow"</button>



    var myWindow;
    function openWin() {
        myWindow = window.open("","myWindow","width=200,height=100");
        myWindow.document.write(
           "<p>
    This is 'myWindow'
    </p>"

            +""
            +"function closeWindow()\\{" // sample injected function
            +"window.close();"
            +"\\}<\\/script>"
        );    
    }
    function closeWin() {
            if(!myWindow.closeWindow) {
                setTimeout(closeWin,1000); // repeat if function not found
            } else {
                myWindow.closeWindow();
            }
    }

    </body>
    </html>


    好像是一个镀铬的虫子

    https://bugzilla.mozilla.org/show_bug.cgi?id=1295839
    https://bugs.chromium.org/p/chromium/issues/detail?id=640620