
HTML的frame框架自适应高度的6个方法:
1、可以给它添加一个默认的CSS的min-height值,然后同时使用JavaScript改变高度。常用的兼容代码有:
// documentdomain = "caibaojiancom";function setIframeHeight(iframe) {
if (iframe) {
var iframeWin = iframecontentWindow || iframecontentDocumentparentWindow;
if (iframeWindocumentbody) {
iframeheight = iframeWindocumentdocumentElementscrollHeight || iframeWindocumentbodyscrollHeight;
}
}
};
windowonload = function () {
setIframeHeight(documentgetElementById('external-frame'));
};
(如果在同个顶级域名下,不同子域名之间互通信息,设置documentdomain="域名com"
2、只要修改以上的iframe的ID即可了。或者你可以直接在iframe里面写代码,我们一般为了不污染HTML代码,建议使用上面的代码。
<iframe src="backtophtml" frameborder="0" scrolling="no" id="external-frame" onload="setIframeHeight(this)"></iframe>3、多个iframe的情况下
<script language="javascript">//输入你希望根据页面高度自动调整高度的iframe的名称的列表
//用逗号把每个iframe的ID分隔 例如: ["myframe1", "myframe2"],可以只有一个窗体,则不用逗号。
//定义iframe的ID
var iframeids=["test"];
//如果用户的浏览器不支持iframe是否将iframe隐藏 yes 表示隐藏,no表示不隐藏
var iframehide="yes";
function dyniframesize()
{
var dyniframe=new Array()
for (i=0; i<iframeidslength; i++)
{
if (documentgetElementById)
{
//自动调整iframe高度
dyniframe[dyniframelength] = documentgetElementById(iframeids[i]);
if (dyniframe[i] && !windowopera)
{
dyniframe[i]styledisplay="block";
if (dyniframe[i]contentDocument && dyniframe[i]contentDocumentbodyoffsetHeight) //如果用户的浏览器是NetScape
dyniframe[i]height = dyniframe[i]contentDocumentbodyoffsetHeight;
else if (dyniframe[i]Document && dyniframe[i]DocumentbodyscrollHeight) //如果用户的浏览器是IE
dyniframe[i]height = dyniframe[i]DocumentbodyscrollHeight;
}
}
//根据设定的参数来处理不支持iframe的浏览器的显示问题
if ((documentall || documentgetElementById) && iframehide=="no")
{
var tempobj=documentall documentall[iframeids[i]] : documentgetElementById(iframeids[i]);
tempobjstyledisplay="block";
}
}
}
if (windowaddEventListener)
windowaddEventListener("load", dyniframesize, false);
else if (windowattachEvent)
windowattachEvent("onload", dyniframesize);
else
windowonload=dyniframesize;
</script>
4、打开调试运行窗口可以看到运行。·
跨域下的iframe自适应高度
跨域的时候,由于js的同源策略,父页面内的js不能获取到iframe页面的高度。需要一个页面来做代理。
方法如下:假设>
ahtml中包含iframe:
<iframe src=";5、在chtml中加入如下代码:
<iframe id="c_iframe" height="0" width="0" src=";6、最后,agenthtml中放入一段js:
<script type="text/javascript">var b_iframe = windowparentparentdocumentgetElementById("Iframe");
var hash_url = windowlocationhash;
if(hash_urlindexOf("#")>=0){
var hash_width = hash_urlsplit("#")[1]split("|")[0]+"px";
var hash_height = hash_urlsplit("#")[1]split("|")[1]+"px";
b_iframestylewidth = hash_width;
b_iframestyleheight = hash_height;
}
</script>
agenthtml从URL中获得宽度值和高度值,并设置iframe的高度和宽度(因为agenthtml在>
1js控制iframe
<div id="dd">132</div>
<script>
var s = '<iframe src="javascript:documentopen();documentwrite(\'<script>alert(1);<\/script><div>test</div>\');documentclose();"></iframe>';
documentgetElementById("dd")innerHTML = s
</script>
<iframe id="tst" name="tst" ></iframe>
<script>
alert(2);
var cw = windowdocumentgetElementById('tst')contentWindow;
cwdocumentopen();
cwdocumentwrite('<script>alert("test")<' + '/script>');
cwdocumentwrite('<div>test</div>');
cwdocumentclose();
</script>
IE6,IE7,FF2下测试通过
2自适应高度
在id为"ifr"的iframe内容里写上js代码:
<script>
function resize(){
parentdocumentgetElementById('ifr')styleheight = documentbodyscrollHeight>300documentbodyscrollHeight:300+"px";
}
windowonload=resize;
windowonresize = resize;
</script>
就能控制id为"ifr"高度至少为300px
要是回答的内容有问题,或认为不妥,请发送百度消息给我,消息内容加上本页网址哦。。
·
采用JavaScript来控制iframe元素的高度是iframe高度自适应的关键,同时由于JavaScript对不同域名下权限的控制,引发出同域、跨域两种情况。
由于客户端js使用浏览器的同源安全策略,跨域情况下,被嵌套页面如果想要获取和修改父页面的DOM属性会出现权限不足的情况,提示错误:Permission denied to access property 'document'。这是因为除了包含脚本的文档载入的主机外,同源策略禁止客户端脚本链接到其他任何主机或者访问其他任何主机的数据。这意味着访问一个web服务的javascript代码通常只有在它也驻留在Web服务本身所在的同一个服务器的时候才有用。
所以在跨域情况下,我们遇到的问题就是:父窗口无法获得被嵌套页面的高度,而且被嵌套页面也无法通过驻留在其服务器上的js修改父窗口Dom节点的属性。所以我们需要一个媒介,来获得被嵌套页面的高度同时又能修改主界面iframe节点的高度。
思路:现有主界面main在域a下,被嵌套页面B在域b下,被嵌套页面B又嵌套一个在域a下的中介页面A。当用户打开浏览器访问mailhtml的时候载入B,触发B的onload事件获取其自身高度,然后B载入A,并将高度值作为参数赋值给A的location对象。这样A就可以通过locationhash获得B的高度。(location是javascript里边管理地址栏的内置对象,比如locationhref就管理页面的url,用locationhref=url就可以直接将页面重定向url。而locationhash则可以用来获取或设置页面的标签值。比如>
关键代码:
iframe主页面:mainhtml
<iframe id="iframeB" name="iframeB" src=">
iframe嵌套页面:Bhtml
<iframe id="iframeA" name="iframeA" src="" width="0" height="0" style="display:none;" ></iframe>
<script type="text/javascript">
function sethash(){
hashH = documentdocumentElementscrollHeight; //获取自身高度
urlC = ">
中介页面:Ahtml
<script>
function pseth() {
var iObj = parentparentdocumentgetElementByIdx_x_x_x('iframeB');//A和main同域,所以可以访问节点
iObjH = parentparentframes["iframeB"]frames["iframeA"]locationhash;//访问自己的location对象获取hash值
iObjstyleheight = iObjHsplit("#")[1]+"px";// *** 作dom
}
pseth();
</script>
同域情况下就不用多说了,直接在被嵌套的页面B中获取其自身高度并 *** 作其父窗口main的dom属性即可。
//windowonresize表示窗口触发时间的时候执行
//两个函数,用闭包包裹起来()()
windowonload=function(){
(windowonresize=function(){
//获取可见宽度
var width=documentdocumentElementclientWidth-180;//180为左边的div的大小
//获取可见高度
var height=documentdocumentElementclientHeight-80;//80为头部的div的大小可自己修改
//如果有宽度就给值,
if(width>=0){
documentgetElementById('main')stylewidth=width+'px';
}
if(height>=0){
documentgetElementById('sidebar')styleheight=height+'px';
documentgetElementById('main')styleheight=height+'px';
}
})()};
扩展资料:
//设置iframe的高度
function resizeFrameHeight() {
$('tab_iframe')css('height', documentdocumentElementclientHeight - 118);
$('md-tab-content')css('left', '0');
}
windowonresize = function () {
resizeFrameHeight();
initScrollShow();
initScrollState();
}
var winWidth = 0;
var winHeight = 0;
function findDimensions() // 函数:获取尺寸
{
// 获取窗口宽度
if (windowinnerWidth)
winWidth = windowinnerWidth;
else if ((documentbody) && (documentbodyclientWidth))
winWidth = documentbodyclientWidth;
// 获取窗口高度
if (windowinnerHeight)
winHeight = windowinnerHeight;
else if ((documentbody) && (documentbodyclientHeight))
winHeight = documentbodyclientHeight;
// 通过深入 Document 内部对 body 进行检测,获取窗口大小
if (documentdocumentElement && documentdocumentElementclientHeight && documentdocumentElementclientWidth) {
winHeight = documentdocumentElementclientHeight;
winWidth = documentdocumentElementclientWidth;
}
// 结果
var body = documentdocumentElementscrollHeight;
var height = winHeight - 140;//窗体高度减去固定的高度
documentgetElementById("frmain")styleheight = height + "px";//给iframe定义高度,会适应屏幕的大小,并跟随浏览器的最小化与最大化改变。
}
function switchSysBar() {
var src = documentall("frameshow")stylebackgroundImage;
var bl = documentall('frmTitle')styledisplay;
if (bl == "") {
documentall("frameshow")stylebackground = "url(img/rightpng) no-repeat center center";
documentall('frmTitle')styledisplay = "none"
}
else {
documentall("frameshow")stylebackground = "url(img/leftpng) no-repeat center center";
documentall('frmTitle')styledisplay = ""
}
}
findDimensions();
// 调用函数,获取数值
windowonresize = findDimensions;
//将此js脚本放到页面的最下边。
你是在哪个位置获取iframe中内容的高度的呢?如果是在父页面的话你的语句没有问题,但必须要在iframe中内容加载完成后再获取,你可以用一个按钮来ALERT一下,或者在onload事件里ALERT一下来显示看看获取有没有问题
<IFRAME onload="alert(thiscontentWindowdocumentbodyscrollHeight)“ src="">></IFRAME>
我看了一下代码,在Chrome下获取frame里面的document确实是用
documentgetElementById('frameId')contentDocument;
这里报错的话,可能是因为Chrome没有获取到frame对象,你可以检查一下
简单的说跨域和同域
同域就是访问的文件和请求的发起文件都在同一域名下,即同一台服务器上,地址可以不加>
以上就是关于HTML的frame框架怎么自适应高度全部的内容,包括:HTML的frame框架怎么自适应高度、如何获取页面iframe元素内容的高、宽、js写入的iframe自适应高度问题(急)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)