JavaScript processing iframe Adaptive height (with or under different domain names) _javascript skills

Source: Internet
Author: User
1. The processing of the adaptive height of iframe under the same domain name
Copy Code code as follows:

<iframe onload= "Javascript:setiframeheight (This)" src= ". /home/b "id=" Win "Name=" Win "width=" 100% "height=" 1 "> </iframe>

Of course here I'm using asp.net MVC where SRC is set to the routing structure
Copy Code code as follows:

<script type= "Text/javascript" >
function Setiframeheight (obj) {
var win = obj;
if (document.getElementById) {
If (Win &&!window.opera) {
if (win.contentdocument && win.contentDocument.body.offsetHeight)
Win.height = Win.contentDocument.body.offsetHeight;
else if (win. Document && win. Document.body.scrollHeight)
Win.height = win. Document.body.scrollHeight;
}
}
}
</script>

Use is the iframe nested pages loaded, use the OnLoad event to get the height of the Web page nested in the IFRAME, and then assign value to the height of the IFRAME can be.
2. Cross-domain iframe height adaptive
Copy Code code as follows:

When the home page and the nested IFRAME are different domain names, it's a bit cumbersome to avoid JavaScript cross-domain restrictions.
Principle: The existing IFRAME homepage main.html, is the iframe nested page iframe.html, the IFRAME intermediary page agent.html three, through main.html (domain name for http:// www.ccvita.com) nested iframe.html (domain name: http:// Www.phpq.net), when the user browses the execution iframe.html the JavaScript code setting IFRAMEC's SCR address to add the IFRAME page height, agent.html (domain name: http:// www.ccvita.com) Gets the height of the pass, setting the height of the iframe in the main.html through JavaScript. Ultimately achieve the desired goals.

IFRAME Home Face main.html
[Code]
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<body>
<div style= "border:1px solid #ccc;p adding:10px;" >
<iframe id= "frame_content" name= "frame_content" src= "iframe.html" width= "100%" height= "0" scrolling= "no" Frameborder= "0" ></iframe>
</div>
<br/> Tail <br/>
</body>

IFrame nested page iframe.html
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<body>
Text <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/ >
Text <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/ >
Text <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/ >
Text <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/ >
<iframe id= "Iframec" name= "Iframec" src= "" width= "0" height= "0" style= "display:none;" ></iframe>
<script type= "Text/javascript" >
function Sethash () {
Hashh = Document.documentElement.scrollHeight;
URLC = "agent.html";
Document.getelementbyidx ("Iframec"). src=urlc+ "#" +HASHH;
}
Window.onload=sethash;
</script>
</body>

IFRAME Intermediary page agent.html
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<body>
<script>
function Pseth () {
var iobj = parent.parent.document.getElementByIdx (' frame_content ');
IOBJH = parent.parent.frames["Frame_content"].frames["Iframec"].location.hash;
IObj.style.height = Iobjh.split ("#") [1]+ "px";
}
Pseth ();
</script>
</body>

In the code, the Kimi possible path representation is not complete
Main.html and agent.html must be in the same domain.
and iframe.html in another domain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.