Resolve cross-domain IFRAME adaptive height (from: http://www.lzlu.com/blog? P = 692)

Source: Internet
Author: User

About the IFRAME adaptive height of the discussion can first look at the reputation of UED blog http://ued.koubei.com /? P = 1217

For general principles, see:

Can look at the example directly: http://lzlu.com/lab/loader/

The code below is displayed directly.
The following is the core code loader. js.

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 /** * Cross-origin IFRAME adaptive Height Solution * Author: changyin@taobao.com  * Copyright (c) 2011, Taobao Inc. All rights reserved. */ var Loader = new function(){    var doc = document,body = doc.body,        self = this,         // Obtain parameters in the URL        getRequest = function(name) {            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"),                r = window.location.search.substr(1).match(reg);            return (r!=null)?  unescape(r[2]) : null;        },         // Get the configuration. The script priority is higher than the URL parameter.        getConfig = function(){            var scripts = doc.getElementsByTagName('script'),                script = scripts[scripts.length - 1];            return function(param){                var p = script.getAttribute(param);                return p? p : getRequest(param);            };        }(),         // Proxy height        proxyheight = 0,         // The ID of the frame on the top page        frameid = getConfig("data-frameid"),         // Listener real-time update height Interval        timer = getConfig("data-timer"),         // Obtain the proxy URL        getProxyuUrl = getConfig("data-proxy"),         // Create the IFRAME of the proxy        proxyframe = function(){            var el = doc.createElement("iframe");            el.style.display = "none";            el.name="proxy";            return el;        }();     // Reset the height    this.resize = function(){        proxyheight = body.offsetHeight;        proxyframe.src =  getProxyuUrl + "?data-frameid=" + frameid+ "&data-frameheight=" + (proxyheight+40);    }     this.init = function(){        var init = function(){            body.appendChild(proxyframe);            self.resize();            // Whether to update            if(!isNaN(timer)){                timer = timer<500?500:timer;                window.setInterval(function(){                    if(body.offsetHeight != proxyheight){                        self.resize();                    }                },timer);            };        };        if(doc.addEventListener){            window.addEventListener("load",init,false);        }else{            window.attachEvent("onload",init);        }        // If the JS framework is introduced, we recommend that you change the above sentence, for example, kissy. Ready (function () {Init ();});    }}; Loader.init();

---------
Proxy file proxy.html

?
1234567891011121314151617181920212223242526 <!DOCTYPE html><html><head><title>proxy</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><script>(function() {    var getRequest = function(name) {            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"),                r = window.location.search.substr(1).match(reg);            return (r!=null)?  unescape(r[2]) : null;        },        height = getRequest("data-frameheight");    try {        var el = window.top.document.getElementById(getRequest("data-frameid"));        if (!el) return;        el.style.height = height + 'px';    }    catch (e) {        //console.log(e);    }})();</script></body></html>

---------

Usage:
1. Introduce loader. js to the IFRAME to be included on the home page

2. input parameters. There are two methods for passing parameters:

1. When the IFRAME is referenced on the home page, the parameter is passed after the URL of the IFRAME to be referenced.
For example, <IFRAME id = "testframe" src1_a.html? Data-frameid=testframe&data-auto=200&data-proxy?proxy.html "> </iframe>

2. input when the IFRAME page contains loader. js
Example: <SCRIPT src = "loader. js" data-frameid = "testframe" data-timer = "2000" data-proxy = “proxy.html "> </SCRIPT>

If you are not busy, follow the example http://lzlu.com/lab/loader/to write it. Then proxy.htmlmust be in the same domain as the most specific page.

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.