`
123003473
  • 浏览: 1040143 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

window.parent和window.opener区别 及iframe自适应高度及js offsetheight和scrollheight的区别

 
阅读更多
下面一段代码是关于window.parent和window.opener区别 来讲的,我们如果要用到iframe的值传到另一框架就要用到window.opener.document.getElementById(name).value = uvalue;这种形式哦。
window.parent能获取一个框架的父窗口或父框架。顶层窗口的parent引用的是它本身。
可以用这一点特性来判断这个窗口是否是顶层窗口。如:
Code
function IsTopWindow( win )
{
    if( win.parent == win ) return true;
    else return false;
}
window.opener引用的是window.open打开的页面的父页面。

opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的opener,在B页面通过opener对象可以访问A页面。 
parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent。 
在JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:
a.html中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过window.opener(省略写为opener)来引用a.html,包括a.html的document等对象,操作a.html的内容。假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会出现“对象为空或者不存在”的JS错误。
window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以写为:
window.opener.document.getElementById("name").value = "输入的数据";


【转载地址】http://www.111cn.net/wy/99/f046d20386b748218a96b0a3bfc45f3b.htm







function setPIH()
{
//** iframe自动适应页面 **//
//定义iframe的ID
var iframeids=["ifr"]
//alert(iframeids.length);
//如果用户的浏览器不支持iframe是否将iframe隐藏 yes 表示隐藏,no表示不隐藏
var iframehide="yes"
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++)
{
if (parent.document.getElementById)
{
//自动调整iframe高度
dyniframe[dyniframe.length] = parent.document.getElementById(iframeids[i]);
if (dyniframe[i] && !parent.window.opera)
{
dyniframe[i].style.display="block"
if (dyniframe[i].contentDocument && dyniframe[i].contentDocument.body.offsetHeight) //如果用户的浏览器是NetScape
dyniframe[i].height = dyniframe[i].contentDocument.body.offsetHeight +10;
else if (dyniframe[i].Document && dyniframe[i].Document.body.scrollHeight) //如果用户的浏览器是IE
dyniframe[i].height = dyniframe[i].Document.body.scrollHeight +19;
}
//alert(dyniframe[i].height );
}
//根据设定的参数来处理不支持iframe的浏览器的显示问题
if ((parent.document.all || parent.document.getElementById) && iframehide=="no")
{
var tempobj=parent.document.all? parent.document.all[iframeids[i]] : parent.document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}
今天原来FF和IE用的都是高度加5,经测试,ie有body上边距,FF也有,网上有一文章说ie是10,FF是8,也就是说两者差2,但我用PS测量出来的结果是两者差了8-9像素,获取到的高度比实际的火狐是差了10,IE是19,于是在上面加上了,如果这俩不单独处理IE的iframe如果选中文字会有滚动,而FF直接是不显示超过高度的部分,
我可能解释的不清楚,但不大好说明白。。关于js offsetheight和scrollheight的区别如下:
JS 关于浏览器之间 scrollHeight offsetHeight (比如 iframe自适应高度时...)
2009-06-06 19:35

关于clientHeight、offsetHeight、scrollHeight

window.screen.availWidth     返回当前屏幕宽度(空白空间) 
window.screen.availHeight     返回当前屏幕高度(空白空间) 
window.screen.width     返回当前屏幕宽度(分辨率值) 
window.screen.height     返回当前屏幕高度(分辨率值) 
window.document.body.offsetHeight;     返回当前网页高度 
window.document.body.offsetWidth;     返回当前网页宽度 

我们这里说说四种浏览器对 document.body 的 clientHeight、offsetHeight 和 scrollHeight 的解释。

这四种浏览器分别为IE(Internet Explorer)、NS(Netscape)、Opera、FF(FireFox)。

clientHeight
大家对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。

offsetHeight
IE、Opera 认为 offsetHeight = clientHeight + 滚动条 + 边框。
NS、FF 认为 offsetHeight 是网页内容实际高度,可以小于 clientHeight。

scrollHeight
IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
NS、FF 认为 scrollHeight 是网页内容高度,不过最小值是 clientHeight。

简单地说
clientHeight 就是透过浏览器看内容的这个区域高度。
NS、FF 认为 offsetHeight 和 scrollHeight 都是网页内容高度,只不过当网页内容高度小于等于 clientHeight 时,scrollHeight 的值是 clientHeight,而 offsetHeight 可以小于 clientHeight。
IE、Opera 认为 offsetHeight 是可视区域 clientHeight 滚动条加边框。scrollHeight 则是网页内容实际高度。

同理
clientWidth、offsetWidth 和 scrollWidth 的解释与上面相同,只是把高度换成宽度即可。

=======================================================================

clientHeight与offsetHeight的区别

许多文章已经介绍了clientHeight和offsetHeight的区别,就是clientHeight的值不包括scrollbar的高 度,而offsetHeight的值包括了scrollbar的高度。然而,clientHeight和offsetHeight的值到底由什么组成的 呢?如何计算这两个数的值?

1. clientHeight和offsetHeight的值由什么决定?

假如我们有以下的DIV,主要显示的文字为"This is the main body of DIV"。

如上图所示,clientHeight的值由DIV内容的实际高度和CSS中的padding值决定,而offsetHeight的值由DIV内容 的实际高度,CSS中的padding值,scrollbar的高度和DIV的border值决定;至于CSS中的margin值,则不会影响 clientHeight和offsetHeight的值。

2. CSS中的Height值对clientHeight和offsetHeight有什么影响?

首先,我们看一下CSS中Height定义的是什么的高度。如在本文最后部分“APPENDIX示例代码”(注:以下称为“示例代码”) 中,innerDIVClass的Height值设定为50px,在IE下计算出来的值如下所示。也就是说,在IE里面,CSS中的Height值定义了 DIV包括padding在内的高度(即offsetHeight的值);在Firefox里面,CSS中的Height值只定义的DIV实际内容的高 度,padding并没有包括在这个值里面(70 = 50 + 10 * 2)。

in IE:
innerDiv.clientHeight: 46
innerDiv.offsetHeight: 50
outerDiv.clientHeight: 0
outerDiv.offsetHeight: 264

in Firfox:
innerDiv.clientHeight: 70
innerDiv.offsetHeight: 74
outerDiv.clientHeight: 348
outerDiv.offsetHeight: 362


在上面的示例中,也许你会很奇怪,为什么在IE里面outerDiv.clientHeight的值为0。那是因为示例代码中,没有定义 outerDIVClass的Height值,这时,在IE里面,则clientHeight的值是无法计算的。同样,在示例代码中,如果将 innerDIVClass中的Height值去年,则innerDIV.clientHeight的值也为0。(注:在Firefox下不存在这种情 况)。

如果CSS中Height值小于DIV要显示内容的高度的时候呢(当CSS中没有定义overflow的行为时)?在IE里面,整个 clientHeight(或者offsetHeight)的值并没有影响,DIV会自动被撑大;而在Firefox里面,DIV是不会被撑开的。如在示 例代码中,将innerDivClass的Height值设为0,则计算结果如下所示。IE里面的DIV被撑开,其clientHeight值等于内容的 高度与padding*2的和;而Firefox里面,文字将溢出DIV的边界,其clientHeight值正好是padding值的两倍。

In IE:
innerDiv.clientHeight: 38
innerDiv.offsetHeight: 42
outerDiv.clientHeight: 0
outerDiv.offsetHeight: 256

In Firefox:
innerDiv.clientHeight: 20
innerDiv.offsetHeight: 24
outerDiv.clientHeight: 298
outerDiv.offsetHeight: 312


APPENDIX 示例代码

<html>
<head>
<style type="text/css">......
.innerDivClass
{...}{...}{...}{
color: red;
margin: 37px;
padding: 10px;
border: 2px solid #000000;
height: 50px;


}
.outerDivClass
{...}{...}{...}{
padding: 100px;
margin: 200px;
border: 7px solid #000000;
}
</style>

<script>......
function checkClientHeight()
......{
var innerDiv = document.getElementById("innerDiv");
var outerDiv = document.getElementById("outerDiv");

       result.innerHTML = "innerDiv.clientHeight: " + innerDiv.clientHeight + "<br />";
result.innerHTML += "innerDiv.offsetHeight: " + innerDiv.offsetHeight + "<br />";
result.innerHTML += "outerDiv.clientHeight: " + outerDiv.clientHeight + "<br />";
result.innerHTML += "outerDiv.offsetHeight: " + outerDiv.offsetHeight + "<br />";
}
</script>
</head>
<body>
<div id="outerDiv" class="outerDivClass">
<div class="innerDivClass" id="innerDiv">
Hello world.        
</div>
</div>
<p></p>
<div id="result">
</div>
<input type="button" onclick="checkClientHeight()" text="Click Me" Value="Click Me" />
</body>
</html>
分享到:
评论

相关推荐

    showModalDialog open弹出子窗口操作parent、opener父窗口及跨域处理

    1&gt; window.showModalDialog()采用JS原理实现,同时父窗口不可操作,window.open()采用新创建一个窗口,同时父窗口可操作; 2&gt; 父窗口与子窗口传递值的方式也有所不同,在子窗口中操作父窗口也语法也不同,分别为var...

    Javascript中封装window.open解决不兼容问题

    对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 var openWindow = function(url, options) { var str = ""; ...

    jsp 刷新父页面

    用iframe、弹出子页面刷新父页面 iframe parent.location.reload(); 弹出子页面 window.opener.location.reload(); 子窗口刷新父窗口 self.window.opener.locaction.reload(); 刷新一open()方法打开的窗口 ...

    字符串 window.open() window.opener window.name window对象等的总结

    2个页面,加了注释,很清晰。

    javascript window.opener的用法分析

    window.opener 返回的是创建当前窗口的那个窗口的引用

    解析jquery获取父窗口的元素

    对应javascript版本为window.parent.document.getElementByIdx_x(“父窗口元素ID”);取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent...

    parent和opener的区别

    parent和opener的区别 parent和opener的区别

    JS window对象的top、parent、opener含义介绍

    例如:A页面通过window.open()方法弹出了B页面,在B页面中就可以通过opener来引用A页面,这样就可以通过这个对象来对A页面进行操作。 3.parentparent用于在iframe,frame中生成的子页面中访问父页面的对象。例如:A...

    解决window.opener=null;window.close(),只支持IE6不支持IE7,IE8的问题

    window.opener=null;window.close(),只支持IE6不支持IE7的问题 打开新窗口并且关闭本窗口不弹出要关闭窗口前的提示function openWin(){window.open(‘login.jsp’,”,’fullscreen=yes,menubar=no,resizable=no’);...

    通过window.opener控制父窗体

    可以看一看啊 博文链接:https://bageer707.iteye.com/blog/74458

    window.opener用法和用途实例介绍

    window.opener,是通过window.open打开子窗体的父窗体的引用。 比如在父窗体parentForm里面,通过window.open(“subForm.html”),那么在subform.html中window.opener就代表parentForm。既然在子窗体中能够拿到父窗体...

    JS window.opener返回父页面的应用

    JS代码: window.open(); 而当支付成功后,需要关闭支付平台支付成功界面,并在客户端加载客户端支付成功页面,JS代码: window.opener.location.href=url;window.close();

    javascript关于open.window子页面执行完成后刷新父页面的问题分析

    本文实例分析了javascript关于open.window子页面执行完成后刷新父页面的方法。分享给大家供大家参考。具体分析如下: 主页面: &lt;input id=btnAdd type=button onclick=openWin(); value=添加 /&gt; 在js中有如下...

    用window.open,opener实现网页间通信

    如果网页 A 可以发送信息到网页 B,反之也然,而不必动用请求/应答模式,该是一件多么惬意的事儿。可以轻松地实现聊天不是吗?

    javascript提示类

    某页面中button按钮事件写代码如下: ... Response.Write("&lt;script&gt;window.open('" + Str1 + "','_blank');window.showModalDialog('close.htm');...&lt;body onload="window.parent.opener=null;window.close();"&gt; &lt;/html&gt;

    JS子父窗口互相操作取值赋值的方法介绍

    对应javascript版本为window.parent.document.getElementByIdx_x(“父窗口元素ID”); 取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent...

    firefox下frameset取不到值的解决方法

     js操作frame详细解说,window.opener和window.parent的区别 frame框架里的页面要改其他同框架下的页面或父框架的页面就用parent window.opener引用的是window.open打开的页面的父页面。 window.frames对象可以引用...

Global site tag (gtag.js) - Google Analytics