JavaScript(Iframe、window.open、window.showModalDialog)父窗口與子窗口之間的操做

1、Iframe 篇html

 公共部分web

 

//父對象獲得子窗口的值
//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.父窗口對子窗口操做windows

 

刷新:
      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();
              
    取值:
alert(window.parent.document.getElementById("IframeDiv").innerHTML);     
 
賦值:
window.parent.document.getElementById("IframeDiv").innerHTML="我是從子窗口IFRAME傳過來的值";
 
關閉:
window.parent.opener=null;//若是不加這句,會提示關閉詢問窗口;
window.parent.close();

 

2、window.open app

1.父窗口對子窗口操做dom

打開:
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,在地址欄輸入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傳過來的值";     

 

3、模態窗口篇spa

1.父窗口操做子窗口firefox

父窗口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.模態窗口操做父窗口code

 

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");
              }     
}

 

 

 

 

 

 

轉載於http://www.cnblogs.com/haojianwei/archive/2010/03/02/1676707.html

相關文章
相關標籤/搜索