Javascript native and jquery libraries implement iframe adaptive height and width

  • 2020-03-30 03:33:40
  • OfStack

Javascript native and jquery libraries implement iframe adaptive content height and width - jquery code is recommended!


‍<iframe src="index.php" id="mainiframe" name="mainiframe" width="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>

The code based on Jquery library is easy to implement:


<script language="javascript" type="text/javascript"> 
$(document).ready(function(){ 
$("#mainframe").load(function(){ 
$(this).height(0); //Used to control the height initialization of the IFRAME on each refresh
var height = $(this).contents().height() + 10; 
$(this).height( height < 500 ? 500 : height ); 
}); 
}); 
</script>

The & # 8205; JS native code based implementation:


<script language="javascript"> 
if (window.parent.length>0){window.parent.document.all.mainframe.style.height=document.body.scrollHeight;} 
</script>

Just the file you are called on by the iframe. / body> Then add the above paragraph!
This also controls the height of the iframe to increase automatically with more content


<iframe name="web" width="100%" frameborder=0 height="100%" src="index.php" id="web" onload="this.height=web.document.body.scrollHeight+20" ></iframe>

The jquery library implements an iframe adaptive content height and width


Related articles: