posts - 46, comments - 75, trackbacks - 5, articles - 1

[Javascript]showModalDialog函数的使用

Posted on 2005-04-20 17:10 xujh 阅读(4024) 评论(5)  编辑 收藏

本范例可以实现弹出一个模态窗口,并演示了两种接收和传递参数的方法,同时可以接受模态窗口返回的多个变量
 
showModalDialog帮助可以参阅微软MSDN http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/showmodaldialog.asp
AA.HTM

--------------------------------------------------------------------------------------------------------------------------
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>主界面</title>
</head>

<body>
<form id="getForm">    回传值:<input type="text" id="getData" readOnly> <input type="text" id="getData1" readOnly>

</form>
<input type="button" value="打开新窗口1" onClick="openWin1()">
<input type="button" value="打开新窗口2" onClick="openWin2()">
<script language="JavaScript">   
function openWin1()   
{       
  var srcFile = "BB.htm";    //新窗口的文档名称       
  var winFeatures = "dialogHeight:300px; dialogLeft:200px;";       
  var obj = getForm;  //将form作为对象传递给新窗口              
  window.showModalDialog(srcFile, obj, winFeatures);   
}
function openWin2()   
{       
  var srcFile = "CC.htm";    //新窗口的文档名称       
  var winFeatures = "dialogHeight:300px; dialogLeft:200px;";       
  var obj = getForm.getData.value;  //将form作为对象传递给新窗口              
  var str=window.showModalDialog(srcFile, obj, winFeatures);   
  if(str!=null)
     document.getForm.getData.value = str;
}

</script>

</body>

</html>

BB.HTM
重要提示:你弹出窗口中调用的是ASPX文件时,必须在Page_Load事件中加入以下代码
Response.Expires = 0; Response.Cache.SetNoStore(); Response.AppendHeader("Pragma", "no-cache");
否则,每次弹出的页面都是上一次的Cache,不会改变。

------------------------------------------------------------------------------------------------------------------------
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新窗口BB</title>
</head>

<base target="_self"> <!--这句话非常重要,只要加上才能保证在弹出窗口中调用服务端代码而不会再弹出一个新窗口-->
<body>
<form id="sendForm">    请输入资料:
<input type="text" id="writeData">   
<input type="text" id="writeData1">
<input type="button" value="送回父窗口" onClick="send(sendForm.writeData.value,sendForm.writeData1.value);">
</form>
<script language="JavaScript">   
function send(val,val1)   
{       
    //取得父窗口传过来的对象       
    var myObj = window.dialogArguments;       
    //赋值
    myObj.getData.value = val;
    myObj.getData1.value = val1;
    //关闭窗口       
    window.close();   
}
</script>

</body>

</html>

CC.HTM
------------------------------------------------------------------------------------------------------------------------
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新窗口CC</title>
</head>

<base target="_self"> <!--这句话非常重要,只要加上才能保证在弹出窗口中调用服务端代码而不会再弹出一个新窗口-->
<body>
<form id="sendForm">    请输入资料:
<input type="text" id="writeData">   
<input type="button" value="送回父窗口" onClick="send(sendForm.writeData.value);">
</form>
<script language="JavaScript">   
function send(val)   
{        
    window.returnValue=val
    window.close();   
}
</script>

</body>

</html>


Feedback

#1楼    回复  引用    

2005-11-12 16:52 by skywolf [未注册用户]
在jsp中怎么写啊???????
不一样的啊 !

#2楼    回复  引用    

2005-11-12 17:26 by skywolf [未注册用户]
是 啊!我也遇到这样问题啊!

#3楼    回复  引用  查看    

2006-06-14 11:44 by 追梦客      
谢谢您的提示,
<base target="_self"> <!--这句话非常重要,只要加上才能保证在弹出窗口中调用服务端代码而不会再弹出一个新窗口-->


#4楼    回复  引用  查看    

2008-07-23 01:43 by 傲然林      
mark.

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
博客园首页

新闻频道

社区

小组

博问

网摘

闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-04-29 18:10 编辑过
成果网帮您增加网站收入


相关链接: