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

总结JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

一、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();
              
    取值:
alert(window.parent.document.getElementById("IframeDiv").innerHTML);     
 
赋值:
window.parent.document.getElementById("IframeDiv").innerHTML="我是从子窗口IFRAME传过来的值";
 
关闭:
window.parent.opener=null;//如果不加这句,会提示关闭询问窗口;
window.parent.close();

?

二、window.open