Cross-origin IFRAME highly adaptive (compatible with IE/FF/OP/chrome)

Source: Internet
Author: User

Using JavaScript to control the height of IFRAME elements is the key to IFRAME highly adaptive. At the same time, because JavaScript controls permissions under different domain names, it leads to the same domain and cross-domain situations.

Because client JS uses the same-source security policy of the browser, if a nested Page wants to obtain and modify the DOM attribute of the parent page in the Cross-origin scenario, the permission is insufficient, and an error is prompted: permission denied to access property 'document '. This is because the same-source policy prohibits client scripts from linking to any other host or accessing data from any other host, except for hosts loaded with scripts. This means that the JavaScript code accessing a Web Service is usually useful only when it also resides on the same server where the Web Service is located.

In cross-origin scenarios, the problem we encounter is that the parent window cannot obtain the height of the nested page, in addition, the nested page cannot modify the attributes of the DOM node of the parent window through JS residing on its server. Therefore, we need a medium to obtain the height of the nested page and modify the height of the IFRAME node on the main interface.

Ideas:The existing main interface is in Domain A, nested page B is in Domain B, and nested page B is nested with another intermediary page a under Domain. When the user opens the browser mail.html, load B, trigger the onload event of B to get its own height, load B to A, and assign the height value as the parameter to the location object of. In this way, a can obtain the height of B through location. Hash. (Location is the built-in object for managing the address bar in Javascript. For example, location. href is used to manage the URL of the page. You can use location. href = URL to directly redirect the page url. Location. Hash can be used to obtain or set the tag value of the page. For example, http: // domain/# admin's location. Hash = "# admin ". Use this attribute value to do something very meaningful .). Because a and main pages are in the same domain, you can modify the DOM node attribute of main to set the IFRAME label height.

Key code:

IFRAME homepage:Main.html

<iframe id="iframeB"  name="iframeB" src="www.b.com/B.html" width="100%" height="auto" scrolling="no" frameborder="0"></iframe>

IFRAME nested page: B .html

<IFRAME id = "iframea" name = "iframea" src = "" width = "0" Height = "0" style = "display: none; "> </iframe> <SCRIPT type =" text/JavaScript "> function sethash () {hashh = document.doc umentelement. scrollheight; // obtain the urlc = "www.a.com/a.html"; // set the srcdocument of iframea. getelementbyid ("iframea "). src = urlc + "#" + hashh; // pass the height as a parameter} window. onload = sethash; </SCRIPT>

Mediation page: a.html

<SCRIPT> function pseth () {var iobj = parent.parent.doc ument. getelementbyid ('iframeb'); // A and main are in the same domain, so you can access the node iobloud = parent. parent. frames ["iframeb"]. frames ["iframea"]. location. hash; // access your location object to obtain the hash value iobj. style. height = iobloud. split ("#") [1] + "PX"; // operation Dom} pseth (); </SCRIPT>

In the case of the same domain, you can directly obtain its own height from the nested page B and operate the DOM attribute of its parent window main.

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.