我遇到了window.open和window.opener.someGlobalJavaScriptVariable的问题。
我有两个* .ascx页面,它们是父页面及其子页面。它们位于同一文件夹中。
父页面将调用window.open以在窗口下显示子页面。 问题是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>
答案 0 :(得分:1)
您无法将参数传递给window.open
关键是,你在JavaScript中定义一个变量,然后通过window.opener.yourVariable
引用它。阅读本文,最后一篇文章......
http://forums.asp.net/t/1267365.aspx?window+open+and+window+dialogArguments+in+javascript