Search code examples
javascriptasp.netundefinedwindow.openascx

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


I am facing an issue with window.open and window.opener.someGlobalJavaScriptVariable.

I have two *.ascx pages that are parent and its child page. They are located in the same folder.

The parent page will call window.open to show the child page under a window. And the problem is that the window.opener.someGlobalJavaScriptVariable is always underfined in child page. I cannot recieve my expected arguments which I have sent from the parent page.

(I am developing these code on Chrome ver 37+ so that I cannot use window.showModalDialog in parent page and window.dialogArguments in child page.)

Please help me out of this problem. I really dont know the reason why I got it. It spends a day of mine.

Here is parent page code:

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);
}

And the child page code (childPage.ascx):

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

[UPDATE01][Updating new information after applying a suggestion of WhiteHat]

I tried applying the solution as WhiteHat suggested and I still got the problem. My expected argument is still undefined in child page.

Please see the code of two pages to get more details.

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


Solution

  • You can't pass arguments to window.open

    The point is, you define a variable in JavaScript, then just reference it by window.opener.yourVariable

    Read this, last post...

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