Home > Web Front-end > JS Tutorial > Code to dynamically adjust iframe height using javascript_javascript skills

Code to dynamically adjust iframe height using javascript_javascript skills

WBOY
Release: 2016-05-16 19:15:13
Original
995 people have browsed it

When you use an iframe on a page, generally speaking, you don't want the iframe to display ugly scroll bars, so that the content in the iframe and the content of the main page are integrated. At this time you will set the scrolling="no" attribute. But in this way, if the content in the iframe changes and the height changes with the change of the content, your iframe will appear too long, resulting in a large blank space underneath, or on the contrary, the height of the iframe will be too small. Some content will be blocked. Here I provide a javascript script compatible with IE/NS/Firefox to dynamically adjust the height of the iframe. If you need to adjust the width, the principle is the same and will not be detailed in this article.

First, your main page must contain the following javascript code:


Then add code where iframe is used on the main page: width=200 height=100>
<script> <BR>var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1] <BR>//extra height in px to add to iframe in FireFox 1.0+ browsers <BR>var FFextraHeight=getFFVersion>=0.1? 16 : 0 <br><br>function dyniframesize(iframename) { <BR>var pTar = null; <BR>if (document.getElementById){ <BR>pTar = document.getElementById(iframename); <BR>} <BR>else{ <BR>eval('pTar = ' + iframename + ';'); <BR>} <BR>if (pTar && !window.opera){ <BR>//begin resizing iframe <BR>pTar.style.display="block" <br><br>if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){ <BR>//ns6 syntax <BR>pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; <BR>} <BR>else if (pTar.Document && pTar.Document.body.scrollHeight){ <BR>//ie5+ syntax <BR>pTar.height = pTar.Document.body.scrollHeight; <BR>} <BR>} <BR>} <BR></script>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template