window.showModalDialog瀏覽器不支持後使用後使用window.open代碼問題
由於window.showModalDialog會執行完子頁面後纔會執行母頁面事件,而window.open 卻不會他是同時執行的。
因爲我需要的是執行子頁面後返回到母頁面的參數然後判斷是否有值根據值執行後事件
這個問題困擾了我好幾天,查了很多資料和測試,終於寫出了自己想要的代碼,
在此貼出來以供參考
母頁面ok1.aspx"
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function show_child() {
var child = window.open("ok2.aspx", "child", "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
/* if(!child.closed)
{
if(!window .close())
{
var textValue = frm.txt.value; parent.frm0.txt0.value = textValue;
}
else
{
window .close();
child.close();
}
}*/
}
}
</script>
</head>
<body>
<a href="javascript:show_child();">打開子窗口</a>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="javascript:show_child()"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" name="Button2" runat="server" onclick="Button2_Click" Text="Button" />
</body>
</html>
子頁面ok2.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function choseItem() {
var v = "";
var check_item = document.frm.item;
for (i = 0; i < check_item.length; i++) {
if (check_item[i].checked) {
v += "," + check_item[i].value;
}
document.frm.txt.value = v.replace(/^,{1}/, "");
}
}
function foo() {
// window.opener.location.reload(); //刷新父頁面
window.close();
window.opener.document.getElementById("TextBox1").value = document.getElementById("txt").value
window.opener.document.getElementById("Button2").click(); //賦值後回調母頁面事件判斷
}
</script>
</head>
<body>
<form name=frm>
<input type=checkbox name=item value=1 onclick="choseItem();">a
<input type=checkbox name=item value=2 onclick="choseItem();">b
<input type=checkbox name=item value=3 onclick="choseItem();">c
<input type=checkbox name=item value=4 onclick="choseItem();">d
<input type=text name="txt" id="txt">
</form>
<input type=button value="關閉" onclick="foo();">
</body>
</html>
回調ok1.aspxcs C#事件代碼
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length > 0)
{
Response.Write("我有數了");
}
else
{
Response.Write("我是空的");
}
}