Same-domain, sub-page height does not increase dynamically
This situation is the simplest, directly through the script to get the actual height of the word page, modify the height of the IFRAME element. But there are two points to note:
If the page has absolute positioning or not clear floating elements, the situation is somewhat complex, different browser processing results are different, even including the WebKit kernel browser, specifically see this demo. So you can either do a browser check, or use Math.max to compute a maximum value, or you can think of another way.
IFrame contains pages may be very large, need a very long load time, for this direct calculation of height, it is likely that the page has not finished downloading, height calculation will have problems. So it is best to compute the altitude in the onload event of the IFRAME. Also note here is that IE must use the Microsoft Event model Obj.attachevent to bind the onload event. Other browsers can also directly obj.onload = function () {}.
| The code is as follows |
Copy Code |
|
(function () {
var _resetiframe = function () {
var frame = document.getElementById ("Frame_content_parent")
try {
var framecontent = frame.contentWindow.document,
Bodyheight = Math.max
(FrameContent.body.scrollHeight,
FrameContent.documentElement.scrollHeight);
if (bodyheight!= frame.height) {
Frame.height = Bodyheight;
}
}
catch (ex) {
frame.height = 1800;
}
}
if (Frame.addeventlistener) {
Frame.addeventlistener
("Load", function () {setinterval (_resetiframe,200);},false);
}else{
Frame.attachevent ("onload", function () {setinterval (_resetiframe,200);});
}
})(); |
The same domain, sub page height will increase dynamically, the script may be completely invalid
In the second example, considering the error of the script, but in case the script does not execute at all, the content in the IFRAME will not be displayed because of the height of the iframe. To this end we usually set a sufficient height, in order to control the front end, I think it is more appropriate to write in the CSS file, you need to change the CSS only to change the line. Here I set up the selector{height:1800px; Note that the style of writing in the style table, not directly with Node.style[property] to take, for the Microsoft model, to use Node.currentstyle[property] (digression: The tragic IE model does not support CSS pseudo-Class), for the model , use window.getComputedStyle (Node,null) [property] to fetch. I am here to facilitate the use of Yui directly.
Another problem here is that each browser handles differently when the height of the IFRAME is greater than the height of its containing page. For example, under Firefox, you must calculate the height of the body element, and the height of the HTML element is equal to the height of the IFRAME, however, when it happens that this page has absolute positioning, not clear floating elements, and can not be taken through the body elements, it is obvious that the first method of less disadvantages Please see this demo specifically.
From the above demo can be seen, in addition to IE browser, other browsers calculate the height of the iframe, that is, CSS set #frame_content_parent{height:1800px; IE calculates the actual height of the page referenced by the IFRAME.
| The code is as follows |
Copy Code |
|
#frame_content_parent {height:1800px;}
(function () {
var $ = YAHOO.util.Dom,
frame = $.get ("frame_content_parent");
function Resetiframe () {
var framecontent = frame.contentWindow.document,
Bodyheight = Math.max
(FrameContent.documentElement.scrollHeight,
FrameContent.body.scrollHeight);
if (bodyheight!= $.getstyle (frame, "height")) {
$.setstyle (frame, "height", bodyheight + "px");
}
}
if (frame) {
$.setstyle (frame, "height", "Auto");
SetInterval (resetiframe,300);
}
})(); |
Look at one more example
| The code is as follows |
Copy Code |
|
<script type= "Text/javascript" >
* * IFRAME automatically adapt to the page **//
Enter a list of the names of the IFRAME that you want to automatically adjust height based on the height of the page
Separate the IDs of each iframe with commas. For example: ["myframe1", "myframe2"], you can have only one form, no commas.
Define the ID of the IFRAME
var iframeids=["Test"]
If the user's browser does not support IFrame to hide the iframe and yes to hide, no means no hidden
var iframehide= "Yes"
function Dyniframesize ()
{
var dyniframe=new Array ()
for (i=0; i<iframeids.length; i++)
{
if (document.getElementById)
{
Automatically adjust the iframe height
Dyniframe[dyniframe.length] = document.getElementById (iframeids);
if (dyniframe &&!window.opera)
{
dyniframe.style.display= "Block"
if (dyniframe.contentdocument && dyniframe.contentDocument.body.offsetHeight)//If the user's browser is Netscape
Dyniframe.height = Dyniframe.contentDocument.body.offsetHeight;
else if (dyniframe. Document && Dyniframe. Document.body.scrollHeight)//If the user's browser is IE
Dyniframe.height = Dyniframe. Document.body.scrollHeight;
}
}
To handle the display of browsers that do not support IFRAME based on the parameters set
if ((document.all | | document.getelementbyid) && iframehide== "No")
{
var tempobj=document.all? Document.all[iframeids]: document.getElementById (iframeids)
tempobj.style.display= "Block"
}
}
}
if (Window.addeventlistener)
Window.addeventlistener ("Load", Dyniframesize, False)
else if (window.attachevent)
Window.attachevent ("onload", dyniframesize)
Else
Window.onload=dyniframesize
</script> |
Automatic adaptation of IFRAME to high cross domain
Here is a way to provide an IFRAME agent, simply to say the principle. Suppose there are 3 pages, is the main page a.html, Word page b.html, proxy page c.html. Where A and B are cross-domain, and A and C are the same domain. Their relationship: A contains b,b containing C. Obviously A and B, and B and C, because Cross-domain cannot communicate with each other, and a and C are the same domains that can communicate with each other. To do so we think of the C page to tell a page, b page in the end how much high. Because B and C are cross-domain and cannot communicate with each other, so want to be in the C page, Direct Window.parent.document.body.scrollHeight This is not feasible, so we can only let B page to calculate their own height, and then some way to tell the C page, and then the C page to tell a page. One way to do this is to generate an IFRAME node on page b and then set its SRC attribute, attaching an argument to the address, which is the height of the B-page calculation, Then C page can get the address in this address bar through window.location, extract the height value, through window.top find a page, set a page of the iframe height. The basic principle is this, look at the code:
DEMO
| The code is as follows |
Copy Code |
|
b page Script
Task: Compute its actual height, then generate an iframe node, assigning a height as part of the address of proxy page C to the SRC attribute
(function () {
var agent_iframe = document.createelement ("iframe"),
B_height = Math.max (document.documentelement.scrollheight,document.body.scrollheight);
AGENT_IFRAME.SRC = "/js/2010/iframe_height/agent_iframe_once.html#" + b_height;
Document.body.appendChild (Agent_iframe);
Agent_iframe.style.display = "None";
})();
C Page Script
Task: Gets the height value of the request address and assigns it to the height of the IFRAME on page a
Window.top.document.getElementById ("Frame_content_parent"). Height = parseint (window.location.hash.substring (1), 10); |
Cross-domain, Word page height dynamic change
Here combined with the 2nd, the first 42 methods, my idea is in the B page through a timer, and constantly calculate the height of the B page, one but change, immediately modify the IFRAME label SRC attribute, and C page also has the timer constantly monitor src changes, change the height of the Aiframe label. Note that only the value of the anchor point (such as "#1234") after the SRC attribute is modified, and the page is not refreshed and will not be requested again, which is why the timer is added to the C page.
DEMO
| The code is as follows |
Copy Code |
|
b page Script
(function () {
var getheight = function () {
Return Math.max (document.documentelement.scrollheight,document.body.scrollheight);
};
var preheight = GetHeight (),
Agent_iframe;
var createiframe = function (height) {
Agent_iframe = document.createelement ("iframe");
Agent_iframe.style.height = "0";
Agent_iframe.style.width = "0";
Agent_iframe.style.border = "None";
AGENT_IFRAME.SRC = "/js/2010/iframe_height/agent_iframe.html#" + height;
Document.body.appendChild (Agent_iframe);
}
Createiframe (Preheight);
var checkheight = function () {
var currentheight = GetHeight ();
if (currentheight!= preheight) {
AGENT_IFRAME.SRC = "/js/2010/iframe_height/agent_iframe.html#" + currentheight;
Preheight = Currentheight;
}
SetTimeout (checkheight,500);
}
SetTimeout (checkheight,500);
})();
//C page Script
(function () {
var preheight = parseint (window.location.hash.substring (1), 10),
Ifrmae = window.top.document.getElementById ("Frame_content_parent" );
ifrmae.height = preheight;
setinterval (function () {
var newheight = parseint (window.location.hash.substring (1), 10);
if (newheight!== preheight) {
ifrmae.height = newheight;
preheight = newheight;
}
},500);
}) ();
|
Another option here is to have the IFRAME request again every time, so the C page does not need a timer, but if the 2 calculation height Repeat, will cause the SRC attribute of the same value, so that the browser is very likely not to request the page again, then the C page script will not run. To fix this problem is simple, just add a random number to each computed src attribute. Like/js/2010/iframe_height/agent_iframe.html?temp=123123423712937#1563.
| The code is as follows |
Copy Code |
|
b page Key Script
AGENT_IFRAME.SRC = "/js/2010/iframe_height/agent_iframe.html?a=" + math.random () + "#" + currentheight;
C Page Script
Window.top.document.getElementById ("Frame_content_parent"). Height = parseint (window.location.hash.substring (1), 10); |
The above are all on the Internet, the following to their own a lot of adaptive height for you to take out
Bulk iframe Adaptive:
Work encountered IFrame with the content of the adaptive height of the problem, previously seen on the internet to solve similar problems, so the search, found a more complete compatible with the browser solution, save yourself write.
Although you do not have to write, the idea is to understand, basically is to obtain the IFRAME attribute src specified the content of the contained document height, and then used to set the height of the iframe itself, in the IFRAME on the page load on the page all need to adapt to the height of the iframe automatically set, Save time and effort, if the page to determine all the IFRAME need to adapt to the height, directly to obtain an IFRAME array to the code, even ID do not have to write their own, complete the program to finish. (Code posted:)
| The code is as follows |
Copy Code |
<script language= "JavaScript" >
Enter a list of the names of the IFRAME that you want to automatically adjust height based on the height of the page
Separate the IDs of each iframe with commas. For example: ["myframe1", "myframe2"], you can have only one form, no commas.
Define the ID of the IFRAME
var iframeids=["Test"];
If the user's browser does not support IFrame to hide the iframe and yes to hide, no means no hidden
var iframehide= "yes";
function Dyniframesize ()
{
var dyniframe=new Array ()
for (i=0; i<iframeids.length; i++)
{
if (document.getElementById)
{
Automatically adjust the iframe height
Dyniframe[dyniframe.length] = document.getElementById (Iframeids[i]);
if (Dyniframe[i] &&!window.opera)
{
dyniframe[i].style.display= "Block";
if (dyniframe[i].contentdocument && dyniframe[i].contentdocument.body.offsetheight)//If the user's browser is Netscape
Dyniframe[i].height = Dyniframe[i].contentdocument.body.offsetheight;
else if (dyniframe[i). Document && Dyniframe[i]. Document.body.scrollHeight)//If the user's browser is IE
Dyniframe[i].height = Dyniframe[i]. Document.body.scrollHeight;
}
}
To handle the display of browsers that do not support IFRAME based on the parameters set
if ((document.all | | document.getelementbyid) && iframehide== "No")
{
var tempobj=document.all? Document.all[iframeids[i]]: document.getElementById (iframeids[i]);
tempobj.style.display= "Block";
}
}
}
if (Window.addeventlistener)
Window.addeventlistener ("Load", dyniframesize, false);
else if (window.attachevent)
Window.attachevent ("onload", dyniframesize);
Else
Window.onload=dyniframesize;
</script> |
Someone on the internet has improved the method to solve the problem of automatic adjustment of the iframe height when the content of the IFRAME contains a high dynamic change, the principle is to scan the IFRAME on the page to keep the content height of the document and change the height of the IFRAME, this method seems to solve the problem, However, the page speed and system resource consumption is not difficult to say, the sense of the method is somewhat paranoid, there should be a better solution.