window.opener.someGlobalJavaScriptVariable 在使用 Chrome v37+ 时始终未定义

window.opener.someGlobalJavaScriptVariable is always undefined when using Chrome v37+

我遇到了 window.open 和 window.opener.someGlobalJavaScriptVariable 的问题。

我有两个 *.ascx 页面,分别是父页面和子页面。它们位于同一文件夹中。

父页面会调用window.open来显示window下的子页面。 问题是 window.opener.someGlobalJavaScriptVariable 在子页面中总是定义不足。我无法收到我从父页面发送的预期参数。

(我在 Chrome ver 37+ 上开发这些代码,这样我就不能在父页面中使用 window.showModalDialog 和在子页面中使用 window.dialogArguments。)

请帮我解决这个问题。我真的不知道我得到它的原因。它花了我一天的时间。

父页面代码如下:

function parentFunction(fileCode)
{
    var args = new Array();
    args["fileCode"] = fileCode;
    var url = "childPage.aspx";
    var WinSettings = "center:yes; resizable:yes; dialogWidth:450px; dialogHeight:350px;scrollbars=yes;"
    var w;
    var h;
    var resizable = "no";
    var scroll = "no";
    var status = "no";

    // get the modal specs
    var mdattrs = WinSettings.split(";");
    for (i = 0; i < mdattrs.length; i++) {
        var mdattr = mdattrs[i].split(":");

        var n = mdattr[0];
        var v = mdattr[1];
        if (n) { n = n.trim().toLowerCase(); }
        if (v) { v = v.trim().toLowerCase(); }

        if (n == "dialogheight") {
            h = v.replace("px", "");
        } else if (n == "dialogwidth") {
            w = v.replace("px", "");
        } else if (n == "resizable") {
            resizable = v;
        } else if (n == "scroll") {
            scroll = v;
        } else if (n == "status") {
            status = v;
        }
    }

    var left = window.screenX + (window.outerWidth / 2) - (w / 2);
    var top = window.screenY + (window.outerHeight / 2) - (h / 2);
    args = window.open(url, args, 'toolbar=no, location=no, directories=no, status=' + status + ', menubar=no, scrollbars=' + scroll + ', resizable=' + resizable + ', copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}

和子页面代码(childPage.ascx):

function childFunction() {
    var args = new Array();
    args = window.opener.someGlobalJavaScriptVariable;
    //args is always undefined at this step
}

[UPDATE01][应用 WhiteHat 的建议后更新新信息]

我尝试按照 WhiteHat 的建议应用解决方案,但问题仍然存在。我预期的参数在子页面中仍未定义。

详情请看两页代码

ParentPage.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ParentPage.aspx.vb" Inherits="WebApplication1.ParentPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" runat="server">
    <div>
         <input type="button" onclick="parentFunction('A0');" value="Show Child Page" />
    </div>
    </form>
    <script type="text/javascript">
        function parentFunction(fileCode) {
            var args = new Array();
            args["fileCode"] = fileCode;
            var url = "childPage.aspx";
            var WinSettings = "center:yes; resizable:yes; dialogWidth:450px; dialogHeight:350px;scrollbars=yes;"
            var w;
            var h;
            var resizable = "no";
            var scroll = "no";
            var status = "no";

            // get the modal specs
            var mdattrs = WinSettings.split(";");
            for (i = 0; i < mdattrs.length; i++) {
                var mdattr = mdattrs[i].split(":");

                var n = mdattr[0];
                var v = mdattr[1];
                if (n) { n = n.trim().toLowerCase(); }
                if (v) { v = v.trim().toLowerCase(); }

                if (n == "dialogheight") {
                    h = v.replace("px", "");
                } else if (n == "dialogwidth") {
                    w = v.replace("px", "");
                } else if (n == "resizable") {
                    resizable = v;
                } else if (n == "scroll") {
                    scroll = v;
                } else if (n == "status") {
                    status = v;
                }
            }

            var left = window.screenX + (window.outerWidth / 2) - (w / 2);
            var top = window.screenY + (window.outerHeight / 2) - (h / 2);
            args = window.open(url, args, 'toolbar=no, location=no, directories=no, status=' + status + ', menubar=no, scrollbars=' + scroll + ', resizable=' + resizable + ', copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            var myargs = args;
            var mynum = 777;
        }
    </script>
</body>
</html>

ChildPage.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ChildPage.aspx.vb" Inherits="WebApplication1.ChildPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" method="post" runat="server">
         <input type="button" onclick="closeWindow();" value="Show args from Parent Page" />
    </form>

    <script type="text/javascript">

        function closeWindow()
        {
            alert("window.opener.myargs =" + window.opener.myargs);
            alert("window.opener.mynum =" + window.opener.mynum);
        }

    </script>
</body>
</html>

您不能将参数传递给 window.open

关键是,你在JavaScript中定义了一个变量,然后通过window.opener.yourVariable

引用它

读这个,最后 post...

http://forums.asp.net/t/1267365.aspx?window+open+and+window+dialogArguments+in+javascript