Javascript window.opener 替代品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4885765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:36:28  来源:igfitidea点击:

window.opener alternatives

javascriptwindow.opener

提问by dmay

I am opening a modal popup window. Then I access a parent window textbox and other attributes using window.opener. It is working fine in firefox but not in IE8. It gives error 'window.opener is null'. How can I access parent window attributes in child window which works in both browsers.

我正在打开一个模态弹出窗口。然后我使用window.opener. 它在 Firefox 中工作正常,但在 IE8 中却没有。它给出了错误'window.opener is null'。如何在两个浏览器中都可以使用的子窗口中访问父窗口属性。

采纳答案by Kaitnieks

You can pass arguments to showModalDialog function. Simply pass window object as an argument.

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

window.showModalDialog(theURL, window);

Yo can access the arguments from the modal window using dialogArguments. See: http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx

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

var openerWindow = window.dialogArguments;

回答by Krishn Y

There are two ways to solve the problem: Note: "window.opener" is not supported by IE if "showModalDialog" is been used.

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

1) Instead of "window.showModalDialog" use "window.open"

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

2) If you want to use "window.showModalDialog" then do the following:

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>

回答by Aidan

Disable Internet Explorer's "Protected Mode", which prevents access to this object.

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

The steps for this are:

执行此操作的步骤是:

  1. Press Alt+T to show the Tools menu
  2. Click "Internet options"
  3. Select the "Security" tab
  4. Make sure zone selected contains your site. For an intranet site it would typically be "Local intranet" zone.
  5. Untick "Enable Protected Mode"
  6. Close all IE tabs and windows and re-open.
  1. 按 Alt+T 显示工具菜单
  2. 点击“互联网选项”
  3. 选择“安全”选项卡
  4. 确保选择的区域包含您的站点。对于 Intranet 站点,它通常是“本地 Intranet”区域。
  5. 取消勾选“启用保护模式”
  6. 关闭所有 IE 选项卡和窗口并重新打开。

Now you should be able to access the window.opener object.

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

回答by Bünyamin Sar?gül

As a cross-browser alternative, you can give a custom attribute to the new window while you are opening it:

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

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

Then, in the referred page:

然后,在引用的页面中:

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

回答by Xhalent

The approach I would take is the following:

我将采取的方法如下:

  1. Use an existing JavaScript UI library because you are not the first person to ever want to do this, failing that
  2. Create a function called OpenWindow, that browser sniffs for the window.openermethod
  1. 使用现有的 JavaScript UI 库,因为你不是第一个想要这样做的人,如果失败了
  2. 创建一个名为 的函数OpenWindow,浏览器会嗅探该window.opener方法

For example:

例如:

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

and if it finds it then uses it, else it tests for the IE variant and uses it. And then it checks Safari's version, etc...

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