PDA

View Full Version : Auto resize iframe - multibrowser



imbehind
03-11-2007, 06:03 PM
Needed auto resized iframe for my own content, and after few hours this is a working result:

Here is the code for the iframe with id="ifrm"


function autoIframe(){
var x = 0;

///////////// reset iframe size for Opera //////////////
if(navigator.userAgent.indexOf("Opera")!=-1){
this.document.body.scrollHeight = this.document.body.offsetHeight
}
/////////////////////IE, Opera///////////////////////////////
var y = this.document.body.scrollHeight;
while (x < y){
x+=1;
}
parent.document.getElementById("ifrm").style.height = x

///////////////////// Firefox //////////////////////////////
if(navigator.userAgent.indexOf("Firefox")!=-1){
parent.ifrm.frameElement.height = this.document.body.scrollHeight+5;
}
}

body tag of the iframe content should look like this:


<body onload="autoIframe()">

Tested with Opera 9.0, IE 7.0, Firefox 2.0

Cheers!