window.open、window.open,window.showModalDialog和window.showModelessDialog 的区别

前端使用easyUi控件,在写新增,编辑弹出框时,因为数据较多页面比较复杂。easyUi自带的模态框,window/dialog需要在父页面中创建div,导致父页面代码很多。然后想到用window.open() window.showModalDialog('xxiconAdd.jsp','_blank','height=500, width=950, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no'); 不能做到遮罩,可能会出现多个窗体出现误操作。 然后找到了window.showModalDialog可实现需求。 一下查找资料做了比较:

window.open、window.showModalDialog和window.showModelessDialog 的区别

博客分类: 模态窗口 

一、前言 要打开一个可以载入页面的子窗口有三种方法,分别是window.open、window.showModalDialog和window.showModelessDialog。 open方法就是打开一个页面,可以说同用url链接打开一个页面一样,不推荐使用,因为很多浏览器会拦截。 这里推荐使用的是window.showModalDialog和window.showModelessDialog,下面介绍二者的异同和用法。

二、 showModalDialog和showModelessDialog的区别 showModalDialog:被打开后就会始终保持输入焦点,除非对话框被关闭,否则用户无法切换到父窗口,类似alert的运行效果。 showModelessDialog:被打开后,用户可以随机切换输入焦点。对主窗口没有任何影响,最多是被挡住一下而以。

三、怎样才让在showModalDialog和showModelessDialog里的超连接不弹出新窗口 在默认情况下,showModalDialog和showModelessDialog窗口中的链接都会导致打开一个新的窗口,但这不是我们需要的。 解决这个问题的方法是在被showModalDialog和showModelessDialog窗口调用的页面添加<base target="_self" /> 如下: <title>被打开的页面</title> <base target="_self" />

四.、showModalDialog和showModelessDialog不使用缓存 showModalDialog和showModelessDialog在第一次打开页面时会默认缓存该页面,如果再次打开相同URL的页面的话,他们会直接调用缓存中的页面,而不是从服务器返回,要不使用缓存可进行如下配置:

<title>被打开的页面</title> <meta http-equiv="pragram" content="no-cache"> //禁止浏览器从本地缓存中调阅页面,网页不保存在缓存中,每次访问都刷新页面。 <meta http-equiv="cache-control" content="no-cache, must-revalidate"> //同上面意思差不多,必须重新加载页面 <meta http-equiv="expires" content="0"> //网页在缓存中的过期时间为0,一旦网页过期,必须从服务器上重新订 上面的配置不一定有效果,所以不推荐使用,最好的办法是在URL后加上一个时间戳,如下: url = url + “&time=” + new Date();

五、如何刷新showModalDialog和showModelessDialog里的内容 在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能弹出菜单。这个只能依靠javascript了,以下是相关代码:

<body οnkeydοwn="if (event.keyCode==116){reload.click()}"> <a id="reload" href="filename.htm" style="display:none">reload...</a> 将filename.htm替换成网页的名字然后将它放到你打开的网页里,按F5就可以刷新了,注意,这个要配合<base target="_self">使用,不然你按下F5会弹出新窗口的。 由于在刷新上处理起来非常不方便,所以使用ajax结合showModalDialog和showModelessDialog使用是非常适合的,建议结合使用。

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

七、 showModalDialog和showModelessDialog数据传递技巧(例子用的是showModalDialog函数,showModelessDialog函数的用法一样)

  1. 父窗体向打开的窗体传递数据一般使用url参数传递
  2. 打开的窗体,即子窗体向父窗体进行数据传递有两种方法 (1) 第一种称为“函数法”,同调用一个函数并返回值一样 可以通过在被调用的页面(子页面)使用window.returnValue来设置返回值,返回值可以是任何值或对象,调用页面(父页面)直接获取返回值即可。 //父窗体js,直接通过函数获取返回值

Html代码 收藏代码

function openModalWindow(){  
          var returnValue = window.showModalDialog("sonPage.aspx");  
          alert(returnValue);  
      }  



//子窗体js,通过window.returnvalue来设置返回值

Html代码 收藏代码

function setReturnFatherPageValue(){  
       window.returnValue = true;  
    }  

(2) 第二种称为“引用法”,通过传递父窗体的引用,我们可以操作父窗体上的所有东西 要使用引用法就必须在打开子窗体时将父窗体作为一个参数传递给子窗体,而在子窗体可以通过window.dialogArguments获取到传递过来的父窗体的引用。 //父窗体js,将整个父window作为参数传递给子窗体

Html代码 收藏代码

function openModalWindow(){  
          window.showModalDialog("sonPage.aspx", window);  
      }  



  //子窗体js,通过window.dialogArguments可以访问父window中的所有元素,它在这里代表了父window对象

Html代码 收藏代码

function openModalWindow(){  
          var txt = window.dialogArguments.document.getElementByIdx("txt");  
          var lab = window.dialogArguments.document.getElementByIdx("lab");  
          txt.value = "sonPageChangedValue";  
          lab.value = "isTheSame";  
      }  

八、 控制弹出窗体的样式

  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。
    

举例如下: window.showModalDialog("sonPage.aspx", "", "dialogHeight=350px;dialogwidth=410px;dialogLeft=0;dialogTop=25;help=no;resizable=no;status=no;scrollbars=no;"); 或 window.showModalDialog("sonPage.aspx", window, "dialogHeight=350px;dialogwidth=500px;help=no;scrollbars=no;"); 都可

九、 窗口高度自适应,这个需要在每个弹出框加载的页面放置,比较麻烦,而且不完善,使用时请调试好 Html代码 收藏代码

<script type="text/javascript">  
  function resetDialogHeight(){  
      if(window.dialogArguments == null){  
          return; //忽略非模态窗口    
      }  
             var ua = navigator.userAgent;  
      var height = document.body.offsetHeight;  
      if(ua.lastIndexOf("MSIE 6.0") != -1){  
        if(ua.lastIndexOf("Windows NT 5.1") != -1){    //alert("xp.ie6.0");  
            window.dialogHeight=(height+102)+"px";    
        }  
        else if(ua.lastIndexOf("Windows NT 5.0") != -1){    //alert("w2k.ie6.0");    
           window.dialogHeight=(height+49)+"px";  
        }  
      }  
      else{  
        window.dialogHeight=height+"px";  
      }  
    }  
</script>  

然后如下设置即可: Html代码 收藏代码

<body onload="resetDialogHeight()"> 

转载于:https://my.oschina.net/maoguangdong/blog/776671

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值