IFRAME cross-origin adaptive Height Solution

Source: Internet
Author: User

IFRAME needs to be used to nest the child pages of another project, but a scroll bar is not required. That is to say, the height of IFRAME must be adaptive according to the height of the nested page.

 

Because of cross-origin, parent and child pages cannot communicate.

 

The first thought was the window. Name method that was recently used.

 

Code snippet:

 

// This method cannot solve the issue that the link in the subpage cannot adapt to the height. <br/> function autoheightiframenavigate (iframeid, URL) {<br/> VaR _ frame = $ ('#' + iframeid); <br/> _frame.css ('visibility ', 'ddy'); <br/> _ frame. one ('load', function () {<br/> $ (this ). one ('load', function () {<br/> var MSG = This. contentWindow. name; // obtain the value. The value is the height. <br/> This. contentWindow. location = URL; // redirect to the target page again <br/> try {<br/> var Height = eval (MSG ); // obtain and set the height. <br/> _frame.css ('height', height + 'px'hangzhou.css ('visibility ', 'visable'); <br/>} catch (E) {<br/> _frame.css ('height', '800px'}.css ('visibility ', 'visable'); <br/>}< br/>}); <br/> This. contentWindow. location = "about: blank"; <br/> }). ATTR ('src', URL); <br/>}

 

Modified from here

 

Principle: when the child IFRAME page is onloaded, the height is calculated and written in window. in name, modify the SRC of IFRAME on the parent page to load the local proxy page and get the height. Then, set the height and change the SRC of IFRAME to the original address (this is quite awkward !!)

 

This method is flawed:

1. IFRAME will be loaded twice (the second will be cached by the browser)

2. It cannot solve the high self-adaptability after the IFRAME page contains pages (there is a solution, but it is more awkward)

 

 

The perfect solution was accidentally found out yesterday.

 

The key is window. Top.

 

The principle is not hard: after cross-origin sub-page B is loaded, calculate the height, dynamically load proxy Page C (same domain as parent page A), and pass the height to C, c. Call the top-level page (that is, a) and return parameters.

 

After simple modification, the following is a demo:

 

Page

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "/> <br/> <title> test </title> <br/> <SCRIPT src =" http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "mce_src =" http://ajax.googleapis.com/ajax/ Libs/jquery/1.4.2/jquery. min. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> <input type =" button "value =" B1 "onclick = "change (1 ); "/> <br/> <input type =" button "value =" B2 "onclick =" Change (2 ); "/> <br/> <IFRAME id =" _ frame "src =" # "mce_src =" # "style =" height: 600px; "frameborder =" 0 "marginheight =" 0 "marginwidth =" 0 "scrolling =" no "> </iframe> <br/> <SCRIPT type =" text/JavaScript "> <! -- <Br/> Function Change (I) {<br/> $ ('# _ frame '). ATTR ('src', 'HTTP: // mao.pconline.com.cn: 8080/iframeheight/frameb'{ I }'.html '); <br/>}< br/> function adaptiframeheight (h) {<br/> condition ('{_frame'{.css ('height', H + 'px '); <br/>}</P> <p> // --> </SCRIPT> <br/> </body> <br/> </ptml>

 

Subpage B1

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "/> <br/> <title> </title> <br/> <SCRIPT src =" http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "mce_src =" http://ajax.googleapis.com/ajax/li BS/jquery/1.4.2/jquery. min. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> <SCRIPT type = "text/JavaScript"> <! -- <Br/> $ (document ). ready (function () {<br/> var agent_iframe = document. createelement ("iframe"); <br/> agent_iframe.src = "http://mao.pclady.com.cn: 8080/iframeheight/framec.html #" + $ ("body "). ATTR ('rollheight'); <br/> document. body. appendchild (agent_iframe); <br/> agent_iframe.style.display = "NONE"; <br/> }); </P> <p> // --> </SCRIPT> <br/> </body> <br/> </ptml> 

 

Subpage B2

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "/> <br/> <title> </title> <br/> <SCRIPT src =" http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "mce_src =" http://ajax.googleapis.com/ajax/li BS/jquery/1.4.2/jquery. min. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> 1 <br/> <SCRIPT type = "text/JavaScript"> <! -- <Br/> $ (document ). ready (function () {<br/> var agent_iframe = document. createelement ("iframe"); <br/> agent_iframe.src = "http://mao.pclady.com.cn: 8080/iframeheight/framec.html #" + $ ("body "). ATTR ('rollheight'); <br/> document. body. appendchild (agent_iframe); <br/> agent_iframe.style.display = "NONE"; <br/> }); </P> <p> // --> </SCRIPT> <br/> </body> <br/> </ptml> 

 

Proxy Page C

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "/> <br/> <title> </title> <br/> <SCRIPT src =" http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "mce_src =" http://ajax.googleapis.com/ajax/li BS/jquery/1.4.2/jquery. min. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> <SCRIPT type =" text/JavaScript "> <! -- <Br/> window. top. adaptiframeheight (parseint (window. location. hash. substring (1), 10); </P> <p> // --> </SCRIPT> <br/> </body> <br/> </ptml>

 

That is to say, application a needs to prepare the container page (framea) and proxy page (framec), and application B needs to calculate the height after the page is loaded, then dynamically load the proxy page of application a and pass the height parameter.

 

(Supplement: in fact, through the existing technology, nginx jump can easily achieve same-domain access between applications)

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.