showModalDialog 传值及刷新。弹出子页面,回传给父页面

来源:互联网 发布:电视软件安装 编辑:程序博客网 时间:2024/05/29 15:25
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<TITLE>New Document </TITLE>
        
<META content="EditPlus" name="Generator">
        
<META content="" name="Author">
        
<META content="" name="Keywords">
        
<META content="" name="Description">
        
<script language="javascript"> 
        
<!-- 
        
function openChild(){
        
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px"); 
        
if(k != null
        document.getElementById(
"txt11").value = k; 
        } 
        
//--> 
        </script>
    
</HEAD>
    
<BODY>
        
<FONT face="宋体"></FONT>
        
<br>
        传递到父窗口的值:
<input id="txt9" type="text" value="3333333333333" name="txt9"><br>
        返回的值:
<input id="txt11" type="text" name="txt11"><br>
        子窗口设置的值:
<input id="txt10" type="text" name="txt10"><br>
        
<input id="Button1" onclick="openChild()" type="button" value="openChild" name="Button1">
    
</BODY>
</HTML>


child.html
---------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<TITLE>New Document </TITLE>
        
<META content="EditPlus" name="Generator">
        
<META content="" name="Author">
        
<META content="" name="Keywords">
        
<META content="" name="Description">
        
<meta http-equiv="Expires" content="0">
        
<meta http-equiv="Cache-Control" content="no-cache">
        
<meta http-equiv="Pragma" content="no-cache">
    
</HEAD>
    
<BODY>
        
<FONT face="宋体"></FONT>
        
<br>
        父窗口传递来的值:
<input id="txt0" type="text" name="txt0"><br>
        输入要设置父窗口的值:
<input id="txt1" type="text" name="txt1"><input id="Button1" onclick="setFather()" type="button" value="设置父窗口的值" name="Button1"><br>
        输入返回的值:
<input id="txt2" type="text" name="txt2"><input id="Button2" onclick="retrunValue()" type="button" value="关闭切返回值" name="Button2">
        
<input id="Button3" onclick="" type="button" value="关闭刷新父窗口" name="Button3">
        
<script language="javascript"> 
        
<!-- 
        
var k=window.dialogArguments; 
        
//获得父窗口传递来的值 
        if(k!=null
        { 
        document.getElementById(
"txt0").value = k.document.getElementById("txt9").value; 
        } 
        
//设置父窗口的值 
        function setFather() 
        { 
        k.document.getElementById(
"txt10").value = document.getElementById("txt1").value 
        } 
        
//设置返回到父窗口的值 
        function retrunValue() 
        { 
        
var s = document.getElementById("txt2").value; 
        window.returnValue
=s; 
        window.close(); 
        } 
        
//--> 
        </script>
    
</BODY>
</HTML>


----------------------------
说明:
由于showModalDialog缓存严重,下面是在子窗口取消客户端缓存的设置.也可以在服务器端取消缓存,参考:
http://adandelion.cnblogs.com/articles/252137.html
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">



(二)下面是关闭刷新父窗口的例子

farther.html
---------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<TITLE>New Document </TITLE>
        
<META NAME="Generator" CONTENT="EditPlus">
        
<META NAME="Author" CONTENT="">
        
<META NAME="Keywords" CONTENT="">
        
<META NAME="Description" CONTENT="">
        
<script language="javascript"> 
        
<!-- 
        
function openChild() 
        { 

        
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px"); 
        
if(k == 1)//判断是否刷新 
        { 
        alert('刷新'); 
        window.location.reload(); 
        } 
        } 
        
//--> 
        </script>
    
</HEAD>
    
<BODY>
        
<br>
        传递到父窗口的值:
<input id="txt9" type="text" value="3333333333333" NAME="txt9"><br>
        
<input type="button" value="openChild" onclick="openChild()" ID="Button1" NAME="Button1">
    
</BODY>
</HTML>


child.html
----------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<TITLE>New Document </TITLE>
        
<META content="EditPlus" name="Generator">
        
<META content="" name="Author">
        
<META content="" name="Keywords">
        
<META content="" name="Description">
        
<meta http-equiv="Expires" content="0">
        
<meta http-equiv="Cache-Control" content="no-cache">
        
<meta http-equiv="Pragma" content="no-cache">
    
</HEAD>
    
<BODY>
        
<FONT face="宋体"></FONT>
        
<br>
        父窗口传递来的值:
<input id="txt0" type="text" name="txt0"><br>
        
<input id="Button1" onclick="winClose(1)" type="button" value="关闭刷新父窗口" name="Button1">
        
<input id="Button2" onclick="winClose(0)" type="button" value="关闭不刷新父窗口" name="Button2">
        
<script language="javascript"> 
        
<!-- 
        
var k=window.dialogArguments; 
        
//获得父窗口传递来的值 
        if(k!=null
        { 
        document.getElementById(
"txt0").value = k.document.getElementById("txt9").value; 
        } 

        
//关闭窗口返回是否刷新的参数. 
        function winClose(isRefrash) 
        { 
        window.returnValue
=isRefrash; 
        window.close(); 
        } 
        
//--> 
        </script>
    
</BODY>
</HTML>

--------------------------
说明
1.下面是取消客户端缓存的:
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
也可以在服务器端取消缓存,参考:
http://adandelion.cnblogs.com/articles/252137.html

2.向父窗口传递阐述在ASP.NET中也可以是用aaa.aspx?id=1的方式传递.

3.不刷新父窗口的话在父窗口中直接这样一来设置可以.
<script>
window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
</script>
4.在子窗口中若要提交页面的话要加入:,这样就不会打开新窗口了.
<head>
<base target="_self">
</HEAD>








JS中showModalDialog 详细使用


基本介绍:
          showModalDialog()         (IE 4+ 支持)
          showModelessDialog()      (IE 5+ 支持)
          window.showModalDialog()                  方法用来创建一个显示HTML内容的模态对话框。
          window.showModelessDialog()             方法用来创建一个显示HTML内容的非模态对话框。
使用方法:
          vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
          vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures])
参数说明:
         sURL          --  必选参数,类型:字符串。用来指定对话框要显示的文档的URL。
         vArguments    -- 可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过
 

                          window.dialogArguments来取得传递进来的参数。
         sFeatures     -- 可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。
----------------
1.    dialogHeight:    对话框高度,不小于100px
2.    dialogWidth:    对话框宽度。
3.    dialogLeft:     离屏幕左的距离。
4.    dialogTop:     离屏幕上的距离。
5.    center:          { yes | no | 1 | 0 } :              是否居中,默认yes,但仍可以指定高度和宽度。
6.    help:             {yes | no | 1 | 0 }:                是否显示帮助按钮,默认yes。
7.    resizable:       {yes | no | 1 | 0 } [IE5+]:     是否可被改变大小。默认no。
8.    status:          {yes | no | 1 | 0 } [IE5+]:      是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
9.    scroll:            { yes | no | 1 | 0 | on | off }:是否显示滚动条。默认为yes。

下面几个属性是用在HTA中的,在一般的网页中一般不使用。
10.    dialogHide:{ yes | no | 1 | 0 | on | off }:在
打印或者打印预览时对话框是否隐藏。默认为no。
11.    edge:{ sunken | raised }:指明对话框的边框样式。默认为raised。
12.    unadorned:{ yes | no | 1 | 0 | on | off }:默认为no。

参数传递:
1. 要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大为4096个字符。也可以传递对象,例如:
-------------------------------
parent.htm
<script>
          var obj = new Object();
          obj.name="51js";
          window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px");
</script>
modal.htm
<script>
          var obj = window.dialogArguments
          alert("您传递的参数为:" + obj.name)
</script>
-------------------------------
2.可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如:
------------------------------
parent.htm
<script>
          str =window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px");
          alert(str);
</script>
modal.htm
<script>
          window.returnValue="http://homepage.yesky.com";
</script>

常见技巧:

一、怎样才让在showModalDialog和showModelessDialog的超连接不弹出新窗口?
  在被打开的网页里加上<base target="_self">就可以了。这句话一般是放在<head>之间的。

二、怎样才刷新showModalDialog和showModelessDialog里的内容?
  在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能弹出菜单。这个只能依靠

javascript了,以下是相关代码:

<body onkeydown="if (event.keyCode==116){reload.click()}">
<a id="reload" href="http://ayue05.blog.163.com/blog/filename.htm" style="display:none">reload...</a>

  将filename.htm替换成网页的名字然后将它放到你打开的网页里,按F5就可以刷新了,注意,这个要

配合<base target="_self">使用,不然你按下F5会弹出新窗口的。

三、如何用javascript关掉showModalDialog(或showModelessDialog)打开的窗口。
  <input type="button" value="关闭" onclick="window.close()">
  也要配合<base target="_self">,不然会打开一个新的IE窗口,然后再关掉的。

四、Math.random与showModalDialog。

   当你设置的弹出网页固定时(如上面的"modal.htm"页面),ie很可能到临时文件区,下载上次产生的该页面(openPage.html),而没有重新加载,

   对于动态加载的页面来说,这样往往产生误会,如没有及时更新数据,也就更不利于开发者测试。所以,你可以采用如下方式:

      var strPage = “/medal.htm?random="+Math.random();

   这样每次产生的strPage是不一样的,原因也就不言自明了。

0 0