IFrame自适应高与contentWindow
<script type="text/javascript">
function autoFixIfm()
{
var yocityAd= document.getElementById('yocityAd');
yocityAd.height=yocityAd.contentWindow.document.body.scrollHeight+5;
yocityAd.width=yocityAd.contentWindow.document.body.scrollWidth+5;
}
</script>
<iframe id="yocityAd" name="yocityAd" src="Iframe/Index.aspx" width="100%" height="500" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" onload="autoFixIfm()"></iframe>
一定要加contentWindow, 不则是当前的document.body.scrollHeight
contentWindow Property Internet Development Index
--------------------------------------------------------------------------------
Retrieves the window object of the specified frame or iframe.
Syntax
HTML N/A
Scripting [ oWindow = ] document.all.object.contentWindow
Possible Values
oWindow Object that receives the window object.
The property is read-only. The property has no default value.
Remarks
This property is useful if you do not know the id of the frame or iframe you are accessing through a collection.
=============================================
其子窗口中加入以下代码即可
autoFixIframe: function(){
try
{
if(window!=parent)
{
var commentIfrm = parent.document.getElementById("yocityAd");
if(commentIfrm.contentWindow==window)
{
var h1=0, h2=0, d=document, dd=d.documentElement;
commentIfrm.parentNode.style.height = commentIfrm.offsetHeight +"px";
commentIfrm.style.height = "10px";
if(dd && dd.scrollHeight) h1=dd.scrollHeight;
if(d.body) h2=d.body.scrollHeight;
var h=Math.max(h1, h2);
if(document.all){h += 4;}
if(window.opera){h += 1;}
commentIfrm.style.height = commentIfrm.parentNode.style.height = h +"px";
}
}
}
catch (ex){}
}
if(window.attachEvent)
{
window.attachEvent("onload", iframeAutoFit);
}
else if(window.addEventListener)
{
window.addEventListener('load', iframeAutoFit, false);
}
分享到:
相关推荐
iframe高度自适应,多浏览器兼容 已测试过在ie6,7,8,firefox8,chrom15中通过
try{ var bHeight = iframe.contentWindow.document.body.scrollHeight; var dHeight = iframe.contentWindow.document.documentElement.scrollHeight; var height = Math.max(bHeight, dHeight); ifr
简述frame contentwindow的区别,实用方便很不错
iframe.document出现拒绝访问。 threw an exception of type 'System.UnauthorizedAccessException' dynamic {System.UnauthorizedAccessException}
本文介绍了layer弹出子iframe层父子页面传值的实现方法,分享给大家,具体如下: 父页面获取子页面元素 格式: $("#iframeID").contents().find("#eleID") 示例代码: father.html <!DOCTYPE html> <...
前端项目-iframe-resizer,保持相同和跨域iframe的大小与其内容一致,并支持窗口/内容大小调整和多个iframe。
如图中的 this.iframeWin = this.$refs.iframe.contentWindow 【拿到iframe的window对象】 3.vue如何向iframe内传送信息 可以通过H5新属性 postmessage 注释:【postMessage是有点类似于UDP协议,就像短信,是异步的...
找到了下面这个js 代码如下: function SetCwinHeight(obj) { var cwin=obj; if (document.getElementById) { if (cwin && !window.opera) { if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight) ...
NULL 博文链接:https://guoliangheyuqin.iteye.com/blog/1664562
方法是使用iframe window的scroll方法: 1、获取iframe的窗口对象 var iwin = document.getElementById(‘iframe1’).contentWindow; 2 、获取iframe的窗口document对象 var doc = iwin.document; 3、调用iframe ...
jQuery获取iframe的window对象 var win = $(‘#ifr’)[0].contentWindow; JS原生方法获取iframe的window对象 document.getElementById(“ifr”).contentWindow; 可见 $(‘#ifr’)[0].contentWindow 和 document....
在父页面获取iframe子页面的元素 代码如下: 代码如下: $(“#objid”, document.iframes(‘iframe’).document) 或 代码如下: $(document.getElementById(‘iframeId’).contentWindow.document.body).html() $...
1、动态加载js,css文件(用原生js和jquery) iframe结构: frame0(父) frame2(子) frame3(子) frame2中触发事件,动态的向frame3中 加载js、css文件和 dom元素? *同级之间可以调用,可以 通过 子-父-子 的方式...
设置 iframe 的 src 为 ‘about:blank’ 之后,不置为“about:...复制代码代码如下: function clearIframe(id){ var el = document.getElementById(id), iframe = el.contentWindow; if(el){ el.src = ‘about:blan
本文实例分析了jQuery取得iframe中元素的常用方法。分享给大家供大家参考,具体如下: jquery取得iframe中元素的几种...$(document.getElementById('iframeId').contentWindow.document.body).html() 显示iframe中body
生活永远是一个大染缸,一块白布...在子级iframe设置 父级 iframe ,或 孙级 iframe 高度。 function showIframeH(){ var parentWin = parent.document.getElementById("test"); if(!parentWin) return false; var
messenger.addTarget(iframe1.contentWindow, 'iframe1'); messenger.addTarget(iframe2.contentWindow, 'iframe2'); // 给子页面发消息 messenger.targets['iframe1'].send('发给子页面1的消息'); // 或者给所有子...
获取iframe的document对象的目的就是进行dom的操作 $(function() { var result = $('#myframe').prop('contentWindow').document; console.log(result); }); 这样输出来的结果是:document对象
我们经常会有父页面与iframe页面的操作,比如 <iframe id = “iframe”></iframe> 这个iframe里面的内容是js写的。如以下代码 var iframe = document.getElementById(iframe), doc = iframe....