javascript - 使用 Chrome v37+ 时,window.opener.someGlobalJavaScriptVariable 始终未定义

标签 javascript asp.net undefined window.open ascx

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

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

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

(我正在 Chrome 版本 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>

enter image description here enter image description here

最佳答案

您无法将参数传递给 window.open

重点是,你在 JavaScript 中定义一个变量,然后通过 window.opener.yourVariable 引用它

阅读这篇文章,最后一篇文章...

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

关于javascript - 使用 Chrome v37+ 时,window.opener.someGlobalJavaScriptVariable 始终未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478304/

相关文章:

javascript - Mongoose 不会在生产 (Heroku) 上填充 (.populate()),但可以在本地工作

JavaScript 更改页面上的链接?

javascript - 如何在 HTML 中使用内联 JavaScript?

javascript - ASPX JavaScript通过id获取元素值

c++ - 从另一个文件调用 C++ 函数时出现 "Undefined Symbols"

javascript 未捕获类型错误 : Cannot read property 'firstChild' of null

asp.net - Web.config 中使用附加 .mdf 数据库的数据库连接字符串不起作用

asp.net - 即使执行成功,ExecuteNonQuery 返回 -1

javascript - 无法在函数内的对象上调用 $.data

python - 使用python运行nlp脚本时如何解决“"name ' Score' is not Defined”的问题?