【问题标题】:window.opener alternativeswindow.opener 替代品
【发布时间】:2011-02-03 11:50:34
【问题描述】:

我正在打开一个模态弹出窗口。然后我使用window.opener 访问父窗口文本框和其他属性。它在 Firefox 中运行良好,但在 IE8 中运行良好。它给出了错误'window.opener is null'。如何在两个浏览器中都可以访问子窗口中的父窗口属性。

【问题讨论】:

  • 你试过window.parent.opener吗?
  • 我尝试了 window.parent.opener 但我无法访问父文档对象。我将 parent.document 作为参数传递并在弹出窗口中访问 parent.document 作为 window.dialogArguments.parentDocumentObj 其中 parentDocumentObj 是包含文档的变量的名称。

标签: javascript window.opener


【解决方案1】:

有两种方法可以解决这个问题: 注意:如果使用了“showModalDialog”,则IE不支持“window.opener”。

1) 用“window.open”代替“window.showModalDialog

2) 如果您想使用“window.showModalDialog”,请执行以下操作:

<script language="javascript" type="text/javascript">
    function YourFunction()
    {
        var opener = null;

        if (window.dialogArguments) // Internet Explorer supports window.dialogArguments
        { 
            opener = window.dialogArguments;
        } 
        else // Firefox, Safari, Google Chrome and Opera supports window.opener
        {        
            if (window.opener) 
            {
                opener = window.opener;
            }
        }       
        // write you code and refer "opener"
        window.close();
    }
</script>

【讨论】:

    【解决方案2】:

    您可以将参数传递给 showModalDialog 函数。只需将窗口对象作为参数传递。

    window.showModalDialog(theURL, window);
    

    你可以使用 dialogArguments 从模态窗口访问参数。见:http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx

    var openerWindow = window.dialogArguments;
    

    【讨论】:

    【解决方案3】:

    禁用 Internet Explorer 的“保护模式”,它会阻止访问此对象。

    具体步骤如下:

    1. 按 Alt+T 显示“工具”菜单
    2. 点击“互联网选项”
    3. 选择“安全”标签
    4. 确保所选区域包含您的站点。对于 Intranet 站点,它通常是“本地 Intranet”区域。
    5. 取消勾选“启用保护模式”
    6. 关闭所有 IE 选项卡和窗口,然后重新打开。

    现在您应该可以访问 window.opener 对象了。

    【讨论】:

      【解决方案4】:

      我将采取的方法如下:

      1. 使用现有的 JavaScript UI 库,因为您不是第一个想要这样做的人,失败了
      2. 创建一个名为OpenWindow 的函数,让浏览器嗅探window.opener 方法

      例如:

      if(window.opener == undefined) {
         //probably not Firefox...
      }
      

      如果找到它然后使用它,否则它会测试 IE 变体并使用它。然后它会检查 Safari 的版本等...

      【讨论】:

        【解决方案5】:

        作为跨浏览器的替代方案,您可以在打开新窗口时为其赋予自定义属性:

        var popup = window.open(...);
        popup.isPopup = true;
        

        然后,在被引用的页面中:

        if (window.isPopup) {
          // Do something
        }
        else {
          // Not in a popup
        }
        

        【讨论】:

        • 如果用户重新加载新窗口,该属性将不再出现在窗口中
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        • 2011-06-10
        • 2014-04-18
        • 2012-04-08
        • 2012-07-09
        • 2011-03-31
        • 2023-03-12
        相关资源
        最近更新 更多