window.open和window.showModalDialog使用

来源:互联网 发布:阿里云app学生专区 编辑:程序博客网 时间:2024/04/26 00:26
 最近在搞物联网项目,有一个模块:空调控制和电视机控制,因此使用到window.open和window.showModalDialog,想想以后不会经常用,干脆整理下来,供自己以后查看,不会忘记。也给大家分享一下!

        以下面写出自己认为有用的操作和代码。

        SetValue("Iframe1","IframeDiv");      

window.parent.opener=null;//如果不加这句,会提示关闭询问窗口;

window.parent.close();

一、window.open

1.父窗口对子窗口操作

打开窗口同时设定窗口的大小:

var win=null;

win=window.open("Open.html","win","width=200,height=200");

 A.控制窗口的大小

最大化:

//窗口最大化

function SonMaximize()

{

       if(win&&win.open&&!win.closed)

       {

              win.moveTo(-4,-4);

              win.resizeTo(screen.availWidth+8,screen.availHeight+8);

       }else{

              alert('还没有打开窗口或已经关闭');

       }

}

 

最小化:

//窗口最小化

function SonMinimize()

{

       if(win&&win.open&&!win.closed)

       {

              win.resizeTo(0,0);

              win.moveTo(0,window.screen.width);

       }else{

       alert('还没有打开窗口或已经关闭');

       }    

}

关闭:

//关闭窗口

function CloseSon()

{

       if(win&&win.open&&!win.closed)

       {

              win.opener=null;

              win.close()

       }else{

              alert('还没有打开窗口或已关闭') ;

       }

}

刷新:

//刷新

function RefreshSon()

{

       if(win&&win.open&&!win.closed)

       {

              win.location.reload();

              win.focus();

       }else{

              alert('窗口还没有打开或已关闭');

       }

}

查看窗口大小:

function ViewSonSize()

{

       if(win&&win.open&&!win.closed)

       {

              alert(win.document.body.clientWidth+'*'+win.document.body.clientHeight);

              win.focus();

       }else

       {

              alert(' 还没有打开窗口或者已关闭');

       }    

}

 

取值:

alert(window.document.getElementById("OpenDiv").innerHTML);

 

赋值:

win.document.getElementById("OpenDiv").innerHTML="我是从父窗口中传过来的值";

 

2.子窗口操作窗口

 

刷新:

window.opener.location.reload();

       //下面这种方法也可以

       //window.parent.location.href=window.parent.location.href; 

关闭本窗口:

//关闭本窗口

function CloseWindow()

{     //window.opener.opener=null;

       window.close();

}

 

关闭父窗口:

//关闭父窗口

function CloseParent()

{     //火狐下不起作用,如果要想起作用。用下面的方法

    //firefox,在地址栏输入about:config      

       //找到dom.allow_scripts_to_close_windows这项并改为true

              var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

              if(IsIE){//如果是IE            

                     window.opener.opener=null;

                     window.opener.close();

                     window.close();     

              }else{

                     alert("火狐不能直接关闭;需要以下设置1.firefox,在地址栏输入about:config;2.找到dom.allow_scripts_to_close_windows这项并改为true");

              }

}

 

取值:

alert(window.opener.document.getElementById("OpenDiv").innerHTML);     

赋值:

window.opener.document.getElementById("OpenDiv").innerHTML="我是从子窗口Open传过来的值";           

二、模态窗口篇

1.父窗口操作子窗口

父窗口JS代码:

var parValue="现在显示了父窗口中的变量值";

var hao="郝建卫";

function ShowDailog(PageHref,Title,Height,Width)

{

       //--------------left位置

       //screen.availHeight声明了显示浏览器的屏幕的可用宽度

       var dleft =(screen.availHeight-Height)/2;

       //--------------top位置

       var dtop =(screen.availWidth-Width)/2;

       //---------------

 

Var sRet = window.showModalDialog(PageHref,window,Title,"scrollbars=yes;resizable=no;help=no;status=no;center=yes;dialogTop=25;dialogLeft="+ dleft +";dialogTop="+ dtop +";dialogHeight="+Height+"px;dialogWidth="+Width+"px;");

       //--------return

       if (sRet =="refresh")//这种是利用返回值来刷新父页面

       {

              window.Test="true";

              window.location.reload();            

              alert(window.Test);

       }

}

function test()

{

       alert("模态窗口成功调用父窗口的方法");

}

2.模态窗口操作父窗口

var parentWin=window.dialogArguments; 

 

刷新:

       parentWin.location.reload(); 

 

取值:

alert(parentWin.document.getElementById("ShowModalDialogDiv").innerHTML)   //获取父窗口中的对象

 alert("我是从父窗口中得到的变量>>>"+parentWin.parValue);       //获取父窗口中的变量

 

调用父窗口JS方法:

parentWin.test();    //调用父窗口中的方法

 

赋值:

parentWin.document.getElementById("ShowModalDialogDiv").innerHTML="我是从子窗口ShowModalDialog传过来的值";      

 

关闭本窗口:

//关闭本窗口

function CloseWindow()

{

       window.parent.close();

}

 

关闭父窗口:

//关闭父窗口

function CloseModal()

{    

       var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

              if(IsIE){//如果是IE            

                     window.parent.parent.close();

                     //parentWin.opener=null;如果把上面的换成这行,不能关闭父窗口,

                     parentWin.close();

                     //window.parent.parent.parent.parent.close();这个只能关闭模态窗口本身目前只在IE6下测试

              }else{

                     alert("火狐不能直接关闭;需要以下设置1.firefox,在地址栏输入about:config;2.找到dom.allow_scripts_to_close_windows这项并改为true");

              }    

}