日期:2014-05-16  浏览次数:20399 次

js中window.showModalDialog各浏览器居中和传参实例兼以及一些兼容性问题

 

浏览器居中以及传参实例

window.showModelDialog可设置center参数为yes,保证其在子窗口在父窗口居中。

但是该参数只对IE浏览器有效,对火狐无效,只有通过计算模态窗口的居中位置。

 

解决办法

function openShowModalDialog(url,param,whparam,e){
 
 // 传递至子窗口的参数
 var paramObj = param || { };
 
 // 模态窗口高度和宽度
 var whparamObj = whparam || { width: 500, height: 500 };
 
 // 相对于浏览器的居中位置
 var bleft = ($(window).width() - whparamObj.width) / 2;
 var btop = ($(window).height() - whparamObj.height) / 2;
 
 // 根据鼠标点击位置算出绝对位置
 var tleft = e.screenX - e.clientX;
 var ttop = e.screenY - e.clientY;
 
 // 最终模态窗口的位置
 var left = bleft + tleft;
 var top = btop + ttop;
 
 // 参数
 var p = "help:no;status:no;center:yes;";
     p += 'dialogWidth:'+(whparamObj.width)+'px;';
     p += 'dialogHeight:'+(whparamObj.height)+'px;';
     p += 'dialogLeft:' + left + 'px;';
     p += 'dialogTop:' + top + 'px;';
 

  return showModalDialog(url,paramObj,p);
}

 


下面是一个使用该方法和传参的实例
 
Father.html

<head>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#btn").click(function (e) {
                var stuObj = { name: "xy", age: 22 };
                var whObj = { width: 300, height: 200 };
                var returnValue = openShowModalDialog("Son.html", stuObj, whObj, e);
                if (returnValue) {
                    alert("传回来的name:" + returnValue.name);
                }
            });
        });
    </script>
</head>

<body>
    <input id="btn" type="button" value="模态测试按钮" />
</body>


Son.html
<head>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
           
            // 接