欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iframe 自适应高度JS代码,兼容FF ,IE

程序员文章站 2022-05-29 07:56:28
...

代码:

 

 

<iframe src="showmagzine.htm" id="magzineframe" name="magzineframe" align="center" frameborder=no width="100%" height=100% scrolling="no"  frameborder="1" bgcolor="#FFFFFF" onload="javascript:{dyniframesize('magzineframe');}">
	</iframe>

 

js 代码:

<script>
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]   
//extra height in px to add to iframe in FireFox 1.0+ browsers   
var FFextraHeight=getFFVersion>=0.1? 16 : 0    
function dyniframesize(iframename) {

  var pTar = null;   
  if (document.getElementById){     
    pTar = document.getElementById(iframename);   
  }else{   
    eval('pTar = ' + iframename + ';');   
  }   
  if (pTar && !window.opera){   
    //begin resizing iframe     
    pTar.style.display="block"  
    if (pTar.contentDocument && pTar.contentDocument.documentElement.scrollHeight){   
 
	      pTar.height =0;
		pTar.height=pTar.contentDocument.documentElement.scrollHeight;    
     

    }else if (pTar.Document && pTar.Document.body.scrollHeight){   
      //ie5+ syntax   
      pTar.height = pTar.Document.body.scrollHeight;   
       
      
    }   
  }     
} 


</script>