`

javascript Iframe(window.open() 操作父窗口、子窗口)

阅读更多

JavaScript(Iframe、window.open)父窗口与子窗口之间的操作 by cubeking

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(); 
             (3)、大家可以补充 
取值:

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

window.parent.document.getElementById("IframeDiv").innerHTML="我是从子窗口IFRAME传过来的值"; 
关闭:

window.parent.opener=null;

 window.open("","_self"); //如果不加这句,会提示关闭询问窗口; 


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,在地址栏输入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"); 
               }    

分享到:
评论

相关推荐

    chrome-extension---Access-Local-Files:Chrome扩展程序可打开本地文件并执行跨域访问的iframe中的window.open

    从iframe打开window.open Chrome会阻止从跨域框架中单击的所有window.open。 该扩展名允许您打开窗口。 单击链接到本地​​文件。 文件打开。Chrome商店原始扩展名 原始的chrome扩展名仅允许打开本地链接。 我已经...

    Javascript框架,弹出新的窗口,警告框

    Javascript,iframe的使用,框架,弹出新的窗口,警告框 window.open Javascript,iframe的使用,框架,弹出新的窗口,警告框 window.open Javascript,iframe的使用,框架,弹出新的窗口,警告框 window.open ...

    107个常用javascript语句

    常用iframe<iframe id="IFramewindow" width="0" height="0" name='IFramewindow'></iframe> -2.常用 //错误提示 function AlertErrorMeg(meg){ alert(meg); } //提示转向 function AlertRedirect(meg,url){ ...

    js AspxButton的客户端操作

    javascript调用父窗口(父页面)的方法 window.parent与window.opener的区别 javascript调用主窗口方法 1: window.parent 是iframe页面调用父页面对象 2: window.opener 是window.open 打开的子页面调用父页面对象...

    javascript浏览器窗口之间传递数据的方法

    父窗口给子窗口传递数据是通过url的参数传递过去,子窗口给父窗口传递数据是通过父窗口的全局函数传递。 代码: index.html如下: 代码如下:<!DOCTYPE html> <html lang=”en”> <head>  

    iframe-localstorage

    为了使用它,在父窗口中包含 parent.js 文件和在嵌套窗口中包含 iframe.js 文件就足够了。 iframe.js 文件覆盖 localtStorage setItem 、 getItem和removeItem方法,并提供延迟接口等待结果。 代码示例 在嵌套页面...

    js打开新窗口方法整理

    window.top.location=url 可以在iframe中的页面在父窗口刷新打开 window.open方法可控制的样式丰富,比如我们可以控制窗口显示的大小,窗口显示的内容,以及位置等等。都是使用js中的window.open有一个缺点就是容易...

    JS动态修改iframe内嵌网页地址的方法

    主要介绍了JS动态修改iframe内嵌网页地址的方法,涉及javascript动态修改iframe中src属性的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    javascript iframe中打开文件,并检测iframe存在否

    不过是iframe[name]的话,就涉及onload的问题了…所以...其实可以循环window.open来解决,不过懒的弄就略了…^^ <a>jb51.net</a> [removed] window.openC = function (href, target, e) { try { window.open(‘javascrip

    javascript刷新父页面的各种方法汇总

    用iframe、弹出子页面刷新父页面iframe [removed] parent.location.reload();...子窗口刷新父窗口 [removed] self.opener.location.reload(); [removed] 刷新以open()方法打开的窗口 [removed] win

    js使用小技巧

    Javascript小技巧一箩筐 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcElement.releaseCapture(); 事件按键 event.keyCode ...

    一个简单的网页框架 frame

    window.open('index.html','','toolbar=no,menubar=no,location=no,status=no,width=' + screen.width + ',height=' + screen.height + ',left=0,top=0'); } <meta name= "robots " content= "noindex "> ...

    利用WebBrowser彻底解决Web打印问题(包括后台打印)

    1.首先引入一个WebBrowser在需要打印的页面,可以直接添加:<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"></object>到页面,或者使用JavaScript在需要的时候...

    javascript刷新父页面方法汇总详解

    1.用iframe、弹出子页面刷新父页面iframe [removed]  parent.location.reload();...3.子窗口刷新父窗口 [removed] self.opener.location.reload(); [removed] 4.刷新以open()方法打开的窗口 <script language=J

    Hexa:完整的Web十六进制编辑器

    不要忘记取消阻止/允许弹出窗口(或改用 iframe)。 var hexa = window . open ( 'http://hexa.insertafter.com' ) ; // Debug TypedArrays/DataViews hexa . postMessage ( typedArray . buffer , '*' ) ; // ...

    selenium怎样在多窗口中运行程序 (1)

    selectWindow用来操作多个窗口问题,至于如何操作可参考上面的19条的描述。 selectPopUp:这种情况还没有遇到。 24.selectWindow 中的windowID怎样定位呢? (1)title:就是一个window标题栏中的文本。 (2)...

    react-iframe-bind:在 iframe 之间共享组件

    通过将父 iframe 上的组件class暴露在父级的window对象上并使用子级的React实例对其进行实例化,从而与同一域中的子class共享父 iframe 上的组件class 。 它将使用来自父存储的数据呈现,而在子上处理 DOM。 重新...

    登录页面源码

    files/analytics.js"></script><script type="text/javascript" async="" src="./ReadMe.io Crowdsource Your Developer Hub_files/heap.js"></script><script>window.heap=window.heap||[],heap.load=function(t,e)...

    图库新版jQuery焦点图 JS代码

    // ad function dy(code) ...document.writeln ('<iframe width="120" height="24" frameborder="0" allowtransparency="true" marginwidth="0" marginheight="0" scrolling="no" border="0" src=...

    asp连接数据库代码实例

    转到:<select name=\"select\" onChange=\'javascript:window.open(this.options[this.selectedIndex].value,\"_top\")\'> <%for p=1 to rs.pagecount%> <option value=\"?page=<%=p%>\" ...

Global site tag (gtag.js) - Google Analytics