Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set Iframe height by JavaScript

Shulou Source: shulou.com Published: 2022-06-01 10:31:00 09月16日 Update

Most people do not understand the knowledge points of this article "JavaScript how to set Iframe height", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "JavaScript how to set Iframe height" article.

Sometimes you may need to embed Iframe in a web page, and the control of Iframe can not be fixed, so you can automatically set the height according to the content in Iframe. Add an onload event to the Iframe tag of HTML, which calculates the setting height when the Iframe page is loaded.

Function SetIFrameHeight (iFrameId) {

If (iFrameId = "") {return;}

Var Sys = {}

Var ua = navigator.userAgent.toLowerCase ()

Var s

(s = ua.match (/ msie ([\ d.] +) /)? Sys.ie = s [1]: 0

Var pTar = null

If (document.getElementById) {

PTar = document.getElementById (iFrameId)

} else {

Eval ('pTar=' + iFrameId +';')

}

PTar.style.display = "block"

If (Sys.ie) {

If (Sys.ie = = '9.0') {

PTar.height = pTar.contentWindow.document.body.offsetHeight + 15 + "px"

PTar.width = pTar.contentWindow.document.body.scrollWidth + "px"

} else if (Sys.ie = = '8.0') {

PTar.height = pTar.Document.body.offsetHeight + 25 + "px"

PTar.width = pTar.Document.body.scrollWidth + "px"

} else {

PTar.height = pTar.Document.body.scrollHeight + 25 + "px"

PTar.width = pTar.Document.body.scrollWidth + "px"

}

}

}

The parameter is the id input of the Iframe, and it is no problem to pass the IE8 test temporarily.

The above is about the content of this article on "how to set the Iframe height of JavaScript". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Content height that is articles knowledge articles events values parameters most more sometimes tags steps knowledge points web pages industries information channels questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft MariaDB MySQL Linux