總結JavaScript(Iframe、window.open、window.showModalDialog)父視窗與子視窗之間的操作

來源:互聯網
上載者:User
一、Iframe 篇

//父物件得到子視窗的值

//ObjectID是視窗標識,ContentID是元素ID

function GetValue(ObjectID,ContentID)

{

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

                     if(IsIE)

                     {//如果是IE         

                            alert(document.frames(ObjectID).document.getElementById(ContentID).innerHTML);                              

                     }

                     else

                     {//如果是FF

                             alert(document.getElementById(ObjectID).contentDocument.getElementById(ContentID).innerHTML);

                                   //FF下不支援innerText; 下面是解決方案                     

                                   //if(document.all){

                                   //  alert(document.getElementById('div1').innerText);

                                   //} else{

                                   //  alert(document.getElementById('div1').textContent);

                                   //}

                     }    

}

 

//父物件向子視窗賦值

//ObjectID是視窗標識,ContentID是元素ID

function SetValue(ObjectID,ContentID)

{

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

              if(IsIE)

              {//如果是IE         

                     document.frames(ObjectID).document.getElementById(ContentID).innerHTML="我是IE下通過父視窗賦值過來的";                            

              }

              else

              {//如果是FF

                      document.getElementById(ObjectID).contentDocument.getElementById(ContentID).innerHTML="我是FF下通過父視窗賦值過來的";                  

              }    

}

//&&&&&&&&&&&&&&&&&&&&公用方法結束&&&&&&&&&&&&&&&

 

 

      1.父視窗對子視窗操作

 

重新整理:

      document.getElementById("IframeID").src=document.getElementById("IframeID").src+"?_="+Math.random();

上面這種方法有時需要對“src”屬性處理一下。

 

取值:

//父視窗取子視窗的值

       GetValue("Iframe1","IframeDiv");

 

賦值:

//父視窗設定視窗元素的值;

       SetValue("Iframe1","IframeDiv");      

 

   2.子視窗操作父視窗

 

              重新整理:

           (1)、window.parent.location.href=window.parent.location.href;  

           (2)、window.parent.location.reload();

              (3)、大家可以補充

 

    取值:

alert(window.parent.document.getElementById("IframeDiv").innerHTML);    

 

賦值:

window.parent.document.getElementById("IframeDiv").innerHTML="我是從子視窗IFRAME傳過來的值";

 

關閉:

window.parent.opener=null;//如果不加這句,會提示關閉詢問視窗;

window.parent.close();

二、window.open

1.父視窗對子視窗操作

開啟:

var win=null;

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

 

最大化:

//視窗最大化

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下不起作用,如果要想起作用。用下面的方法

    //開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("Firefox不能直接關閉;需要以下設定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("Firefox不能直接關閉;需要以下設定1.開firefox,在地址欄輸入about:config;2.找到dom.allow_scripts_to_close_windows這項並改為true");

              }    

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.