悉野小楼

导航

chrome中showModalDialog解决方案

调用myShowModalDialog

function myShowModalDialog(url, width, height, callback)
{
	if(window.showModalDialog)
	{
		if(callback)
		{
			var rlt = showModalDialog(url, '', 'resizable:no;scroll:no;status:no;center:yes;help:no;dialogWidth:' + width + ' px;dialogHeight:' + height + ' px');
			if(rlt)
				return callback(rlt);
			else
			{
				callback(window.returnValue);
			}
		}
		else
			showModalDialog(url, '', 'resizable:no;scroll:no;status:no;center:yes;help:no;dialogWidth:' + width + ' px;dialogHeight:' + height + ' px');
	}
	else
	{
		if(callback)
			window.showModalDialogCallback = callback;
		var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
		var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
		var winOption = "top="+top+",left="+left+",height="+height+",width="+width+",resizable=no,scrollbars=no,status=no,toolbar=no,location=no,directories=no,menubar=no,help=no";
    	window.open(url,window, winOption);
	}
}

  

 

test.html中脚本函数

<script type='text/javascript'>//在点击网页中确定按钮调用SetReturnValue
function SetReturnValue()
{
	var rlt = "我想返回的数值";
	window.returnValue = rlt;//ie之类浏览器
	if(opener != null && opener != 'undefined')
	{
		window.opener.returnValue = rlt; //chrome有些版本有问题, 所以在子窗口同时修改了父窗口的ReturnValue(能执行showModalDialog的chrome)
		if(window.opener.showModalDialogCallback)
			window.opener.showModalDialogCallback(rlt);
	}
	window.close();
}
</script>

  

调用方式

myShowModalDialog("http://test.com/test.html", 500, 4000, function(resv){
         alert(resv);//原窗口关闭处理流程
});

  

posted on 2015-04-07 15:21  悉野  阅读(3840)  评论(0编辑  收藏  举报