IFRAME full cross-origin highly adaptive solution

Source: Internet
Author: User
ArticleDirectory
    • Solution description:
    • Code:

When you see this demand, it's dark, and you can solve another knowledge point. Haha, after a day's struggle, I had some gains, and through the guidance of Feifei, there were still breakthroughs in cross-origin problem solving (not through hash ).

Check the demo. compatible with IE, ff, and safari.

Solution description:

Requirement: a page (taobao.com) needs to be embedded into B page (alibaba.com). Because the height of B page cannot be determined, high adaptability is required.

Solution: Simply put, create an iframe c in the same domain as a on the B page to allow communication between C and A to pass the height of B.

So how can we pass it over? The previous method was to change the location. Hash of parent. Parent (that is, a) in C, but changing the hash will generate a browser history record. The user experience of clicking the backward button is not good. We tried to directly retrieve frames [B]. Frames [C]. Location. Hash on page a, so we can get it.

The rest is simple. Specify iframe B in a to get the hash of C after loading, and then set the height of iframe B through hash.

Details Code :

Page A: (based on Yui)

/*** Run the function after the IFRAME is loaded ** @ Param {element} el * @ Param {function} func */var oniframeload = function (El, func) {var cb = function () {try {func. call (this);} catch (e) {}} if (TB. bom. isie) {el. onreadystatechange = function () {If (El. readystate = 'complete') {setTimeout (CB, 0); El. onreadystatechange = NULL ;}} else {el. onload = function () {setTimeout (CB, 0); El. onload = NULL ;}};/*** highly adaptive cross-origin IFRAME encapsulation ** @ Param {string} name */var crossdomainiframe = function (name) {var IFRAME = Yahoo. util. dom. get (name); var xclient = 'xclient'; oniframeload (IFRAME, function () {try {var H = frames [name]. frames [xclient]. location. hash. substring (1); If (H = '') {var func = arguments. callee; setTimeout (function () {func () ;}, 20); Return ;}iframe. style. height = H + 'px ';} catch (e) {}}) ;}; // execute crossdomainiframe ('frame _ content '); 

Page B:

  (function () {function addloadevent (func) {var oldon Load = Window. onload; If (typeof window. onload! = 'Function') {window. onload = func;} else {window. onload = function () {oldonload (); func () ;}} function adjust () {var H = document.doc umentelement. scrollheight | document. body. scrollheight; try {If (document. getelementbyid ('xclient') {var divel = document. getelementbyid ('xclient '). parentnode; console. log (document. getelementbyid ('xclient '). parentnode); divel. parentnode. removechild (divel);} var El = document. createelement ('div '); El. innerhtml = ''; document. body. appendchild (EL);} catch (e) {}} addloadevent (adjust) ;}) ();  

Page C: empty page. There is a file to avoid 404.

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.