JavaScript processing Iframe adaptive height (same as or under different domain names)

Source: Internet
Author: User

1. Handling Iframe adaptive height under the same domain name Copy codeThe Code is as follows: <iframe onload = "Javascript: SetIFrameHeight (this)" src = ".. /Home/B "id =" win "name =" win "width =" 100% "height =" 1 "> </iframe>

Of course, here I use Asp. Net MVC. Here src is set as the routing structure.Copy codeThe Code is 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>

When an iframe nested page is loaded, the onload event is used to obtain the height of the page nested in iframe, and then the height is assigned to Iframe.
2. Iframe highly adaptive during cross-Origin Copy codeThe Code is as follows: when the home page and the nested iframe are different domain names, it is a little troublesome and you need to avoid cross-domain restrictions of JavaScript.
Principle: Fill (domain name is http://www.ccvita.com embedded iframe.html (domain name is: http://www.phpq.net fill (Domain Name: http://www.ccvita.com pull transmission height, through the criptset main.html iframe height. Ultimately achieve the expected goal.

Iframemain page main.html
[Code]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head> <title> iframe homepage </title> <Body>
<Div style = "border: 1px solid # ccc; padding: 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>
</Html>

Iframeembedded set page iframe.htmlCopy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head> <title> page nested by iframe </title> <Body>
Text <br/>/>
Text <br/>/>
Text <br/>/>
Text <br/>/>
<Iframe id = "iframeC" name = "iframeC" src = "" width = "0" height = "0" style = "display: none;"> </iframe>
<Script type = "text/javascript">
Function sethash (){
HashH = document.doc umentElement. scrollHeight;
UrlC = "agent.html ";
Document. getElementByIdx ("iframeC"). src = urlC + "#" + hashH;
}
Window. onload = sethash;
</Script>
</Body>
</Html>

Iframemediation page agent.htmlCopy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head> <title> iframe mediation page </title> <Body>
<Script>
Function pseth (){
Var iObj = parent.parent.doc ument. getElementByIdx ('frame _ content ');
Iobloud = parent. parent. frames ["frame_content"]. frames ["iframeC"]. location. hash;
IObj. style. height = iobloud. split ("#") [1] + "px ";
}
Pseth ();
</Script>
</Body>
</Html>

In the code, kimi may indicate incomplete paths
Main.html and agent.html must be in the same domain
Iframe.html is in another domain

Related Article

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.