当前位置:Gxlcms > JavaScript > javascript firefox 自动加载iframe 自动调整高宽示例

javascript firefox 自动加载iframe 自动调整高宽示例

时间:2021-07-01 10:21:17 帮助过:16人阅读

iframe 自动获取onload高宽
代码如下:

function AutoResize(iframe)
{
//firefox
if(iframe.contentWindow)
{
iframe.height = iframe.contentWindow.document.documentElement.scrollHeight;
iframe.width = iframe.contentWindow.document.documentElement.scrollWidth;

}
//IE
else if(iframe.contentDocument) {

iframe.height = iframe.contentDocument.width;
iframe.width = iframe.contentDocument.height;
}
}

iframe 自动加载:
代码如下:

var tdObj = document.getElementById('ifrtd');
tdObj.innerHTML = ' QQ动态加载中 ... ';

var iframe = document.createElement("iframe");
iframe.src = 'http://www.zbphp.com/';

if (iframe.attachEvent){

//iframe.attachEvent("onload",AutoResize.call(iframe)); #报错
iframe.attachEvent("onload", function(){
AutoResize(iframe);
});
} else {

//iframe.onload = AutoResize.call(iframe);#报错不支持
iframe.onload = function(){
AutoResize(iframe);
};
}

tdObj.innerHTML = '';
tdObj.appendChild(iframe);

其实那个iframe.onload这里想写成iframe.onload = AutoResize.call(iframe);很遗憾,报错,不支持。

一直不知道javascript 如何进行函数调用。比如遇到 iframe.onload = function(){} 调用函数且有参数的时候,这样的情况只能这样写,而不能像其他程序那样直接传参。

以前看过有 apply() call(),但试过都是不支持的。为什么?

人气教程排行