window.returnValue和 window.showModalDialog()和window.close()的使用方法

returnValue是javascript中html的window对象的属性,目的是返回窗口值,当用window.showModalDialog函数打开一个IE的模式窗口(模式窗口知道吧,就是打开后不能操作父窗口,只能等模式窗口关闭时才能操作)时,用于返回窗口的值。

具体在项目中使用,有图有真相!(360浏览器测试成功!)

window.returnValue和 window.showModalDialog()和window.close()的使用方法_第1张图片

代码截图

window.returnValue和 window.showModalDialog()和window.close()的使用方法_第2张图片

window.returnValue和 window.showModalDialog()和window.close()的使用方法_第3张图片

代码

function getKjSubject2(id,name){
	alert("nihao");
	var returnValues ;
	var url = "";
	alert("2");
	var returnValues = window.showModalDialog(url,"dialogWidth=200px;dialogHeight=100px");
	alert("得到");
	alert(returnValues);
	if(returnValues != null){
		document.getElementById(id).value = returnValues[0] ;  
	 	document.getElementById(name).value = returnValues[1];
	}
}


function doSelect(){
		alert("2");
		var obj=new Array(2);
		var pid = document.getElementById("pid").value;
		var pname = document.getElementById("pname").value;
		alert(pid);
		obj[0]=pid;
		obj[1]=pname;
		window.parent.returnValue=obj;
		//window.returnValue=obj;
		window.top.close();
		//window.close();	 returnValues
	}
总结:这样一来可以实现从模式窗口向父窗口传递值的作用,这个returnValue除了可以是布尔值,整型值等以外还可以是个js数组,用来传递大量数据。

在项目运行的时候,功能不可用,返回的值为null,另外,窗口也不会关闭!

window.parent.returnValue=obj;
//window.returnValue=obj;
window.top.close();
//window.close();

解决方法:

百度了一些资料,最终解决了!

参考资料如下:

http://suo.iteye.com/blog/508572  

http://bbs.csdn.net/topics/310058773


一般的窗口关闭的JS如下写法:
    window.close()
但是呢,chrome,firefox等中有时候会不起作用。
改为下面的写法:
    window.open("about:blank","_self").close()   
   或者
   window.open("","_self").close()   

如果是frame的时候如下写法:
   一般:window.top.close()
   改善:window.open("about:blank","_top").close()   或者 window.open("","_top").close()

其他比如window.parent.close()也是可以用类似的方法。

如果关闭按钮既可能是单独的画面,也可能是frame的一部分的时候,可以用下面的写法对应。
function closeWin() {
   try {
       window.opener = window;
       var win = window.open("","_self");
       win.close();
       //frame的时候
       top.close();
   } catch (e) {
   }
}

下面是网上比较受欢迎的文章:
function logout(){
        if(confirm("确定要退出吗?")){
                 var browserName=navigator.appName;
                 if (browserName=="Netscape"){
                       window.open('', '_self', '');
                       window.close();
                 }
                 if (browserName=="Microsoft Internet Explorer") { 
                       window.parent.opener = "whocares"; 
                       window.parent.close(); 
                 }
        }
}

FireFox需要设置:
在Firefox地址栏里输入 about:config
在配置列表中找到 dom.allow_scripts_to_close_windows
点右键的选切换把上面的false修改为true即可。
注:默认是false,是为了防止脚本乱关窗口

你可能感兴趣的:(js)