returnValue的理解

来源:互联网 发布:java打开jdk api 编辑:程序博客网 时间:2024/06/06 09:28

father.html

 

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="javascript">

function showmodal(){
var ret = window.showModalDialog("child.html",null,"dialogWidth:500px;dialogHeight:550px;help:no;status:no");
    if (ret){alert('子窗口返回真!');
    }else{
         alert('子窗口返回假!');
   }
}
</script>
</HEAD>

<BODY>
<INPUT id=button1 type=button value=Button name=button1 onclick="showmodal();">
</BODY>
</HTML>


child.html

 

<html>  <head> 

<title></title>
<script type="text/javascript"> 
function trans(tag){    
 if (tag==0){        
 window.returnValue=false;  //解释:当前窗口的返回值为false 
 } else{       
 window.returnValue =true;  //解释:当前窗口的返回值为true
 }    
 window.close();

</script> 
</head>  <body> 
<input id="button1" type="button" value="返回真" name="button1" onclick="trans(1)">
<input id="button2" type="button" value="返回假" name="button2" onclick="trans(0)">  
</body>  </html> 

 

 

returnValue方法有两种作用.看上面的语法.这两种写法代表不同的作用.我们首先来介绍:event.returnValue=false 这种写法的作用是取消事件源头的默认行为.也可以理解为浏览器的默认行为.那什么是浏览器的默认行为呢.比如说:在你的网页中有一个连接.你点击了这个链接以后,就会打开连接的这个页面.或者你单击鼠标右键会打开菜单,这就是浏览器的默认行为.利用event.returnValue=false可以阻止这个连接.让鼠标右键失效.在特定的情况下你也许会用到.
下面介绍window.returnValue="str":这种写法代表返回某些值.尤其是在窗口之间传弟字符串或者其他的类型的数据.
注意:returnValue只支持IE浏览器,与其功能类似的W3C标准的指令是preventDefault
我在下面会重点给出一个event.returnValue的例子.

 

 

event.returnValue=boolr || text = window.returnValue="str"
text:返回你想得到的字符串内容

event:事件句柄.代表事件发生的状态.键盘鼠标都可以捕获
boolr:布尔值.真或假.可以为true或false;
str:想要返回的字符串内容.