http://www.cnblogs.com/haojianwei/archive/2010/03/02/1676707.html
来源:互联网 发布:数据分析的道与术 ppt 编辑:程序博客网 时间:2024/05/29 06:41
总结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.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");
}
}
- http://www.cnblogs.com/haojianwei/archive/2010/03/02/1676707.html
- http://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html
- http://www.cnblogs.com/cnajian/archive/2011/03/02/1968641.html
- http://www.cnblogs.com/yungboy/archive/2010/06/03/1750902.html
- Delphi 文件操作 http://www.cnblogs.com/nothing_cj/archive/2010/09/03/1817256.html
- PHP获取随机数 http://www.cnblogs.com/hailexuexi/archive/2010/09/03/1817442.html
- CMake 使用方法 http://www.cnblogs.com/lyq105/archive/2010/12/03/1895067.html
- http://www.cnblogs.com/and/archive/2010/03/24/1693970.html
- VS2010版本介绍(转自:http://www.cnblogs.com/Leo_wl/archive/2010/06/02/1750035.html)
- LoadRunner总结http://www.cnblogs.com/preftest/archive/2010/08/02/1790569.html
- 经典SQL语句大全:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html
- http://www.cnblogs.com/xiachufeng/archive/2010/08/02/1790411.html
- HTML解析原理http://www.cnblogs.com/NetSos/archive/2010/11/29/1891194.html
- http详解(http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html)
- HTTP协议详解,转自http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html
- HTTP协议的chunked编码 http://www.cnblogs.com/zhaozhan/archive/2010/08/24/1807639.html
- .net常用的类库网络转载(http://www.cnblogs.com/and/archive/2010/03/24/1693970.html)
- SQLiteDatabase类——转载(http://www.cnblogs.com/tt_mc/archive/2010/03/09/1681540.html)
- 个人多线程程序设计经验总结
- 学校网站常出现的几种问题
- C++ string::size_type 类型
- hdu 2795
- 数据库索引的使用
- http://www.cnblogs.com/haojianwei/archive/2010/03/02/1676707.html
- hdu 2844 Coins 多重背包基础题
- hdu2533 N皇后问题
- 经验交流七个方法可以帮助站长判断关键词的难度系数
- HDU 1005 疏忽了一个问题!
- 《算法竞赛-训练指南》第一章-1.13_LA 3635
- Android Resource介绍和使用
- 学习Machine Leaning In Action(二):kNN
- 做好网站内容重要性