JavaScript implements iframe automatic height adjustment and cross-origin of different primary domain names, javascriptiframe

Source: Internet
Author: User

JavaScript implements iframe automatic height adjustment and cross-origin of different primary domain names, javascriptiframe

As we all know, JavaScript has a same-origin policy, that is, different nested iframe of the primary domain name does not allow Js communication.

For example, if I have a website that is embedded with its webpage. Then I can use iframe to reference the address of a third-party website.

However, the iframe height is fixed and cannot be well integrated with a third-party website. For example, if a third-party website uses the waterfall stream plug-in and needs to scroll to load the automatically calculated height, cross-origin is the first question: the iframe primary domain name has different cross-domain methods. If an iframe reference B .com is put in website A.com B .com A, Js in B .com cannot access A.com. Javascript cross-origin must be the same origin, that is, the same primary domain name. Is that swollen?

We can introduce an iframe in B .com and call it "C". This iframe loads a webpage in A.com. In this way, a webpage in the iframe in B .com can communicate with A.com. Next, as long as B communicates with C, C communicates with A to complete B-> A communication. In this way, C is notified when B's height changes, and C notifies A to adjust the iframe height.
Communication between B and C can be achieved through the url address. B .com iframe is set to hidden. C can receive the communication when the src address is changed.

No need to talk about code.

A.com/index.html


B .com/index.html

<Html> 


C proxy File

<Script> var search = window. location. search, getSearchParam = function (search, key) {var mc = search. match (new RegExp (key + "= ([^ \ &] *)"), ret = ""; mc & mc. length & (ret = mc [0]. replace (key + "=", ""); return ret;}, // parameter h = getSearchParam (search, "h"), ifmID = getSearchParam (search, "ifmID"), cbn = getSearchParam (search, "cbn"), // width and height mh = getSearchParam (search, "mh") | h, isFunction = function (fn ) {Return !! Fn &&! Fn. nodeName & fn. constructor! = String & fn. constructor! = RegExp & fn. constructor! = Array & (/function/I ). test (fn + "") ;}; try {if (parent & parent. parent) {ifm = parent.parent.doc ument. getElementById (ifmID); ifm & mh & (ifm. height = mh); fn = parent. parent [cbn]; isFunction (fn) & fn. call (); ifm = null ;}} catch (ex) {console. log (ex) ;}</script>

Articles you may be interested in:
  • Summary of several methods for partial refresh using iframe in JavaScript
  • Js Method for checking whether iframe is loaded
  • Js implementation to prevent iframe
  • How to use JavaScript to control homepage redirection in iframe
  • How to obtain the longdesc attribute in iframe using JS
  • Perfect Method for judging whether iframe is loaded in js
  • Real iframe highly adaptive implemented by js (compatible with IE, FF, and Opera)
  • How to obtain and operate elements in iframe in JS
  • Example of calling js functions on the iframe parent page
  • In-depth introduction to js iframe cross-origin access (same main domain/non-main 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.