Elegant and elegant JavaScript cross-origin Problem Solution

Source: Internet
Author: User

The solution to the Cross-origin JavaScript problem is already in the previous section.ArticleFor details, see http://blog.csdn.net/sfdev/archive/2009/02/13/3887006.aspx;

In addition to the three solutions mentioned in this article, Huang fangrong's "Web Data Interaction art" at this year's Baidu salon sharing conference mentioned a very elegant and brilliant solution! If you don't talk much about it, go directly to the solution schematic:

The problems to be solved in this figure are described as follows:
Any DOM object or JS function in callback, because of cross-origin, but we often need to do something about parameter return. What should we do? The above implementation method can solve this problem well;

The key elegance of the solution is that although the browser will prohibit objects in the JS cross-origin access page, there is no restriction on hierarchical link reference of IFRAME, that is, parent is still available; this solution uses the layer-2 embedded IFRAME and the page and parent in the level-2 IFRAME. parent pages are in the same domain name relationship, so as to avoid cross-domain issues and implement data transmission between two pages,In essence, callback for Js in the parent page is implemented using parent. Parent!

Let's take a practical case:
Function Description:
Renewal page?

solution :
1. Declare a JS function process (height, width) on the index.htm page ); used to set the IFRAME height and width on the page.
Why? Height=xx&width=yy. this page does not have any content, but is used to transmit the data of the width and height after the sub-index.htm page is loaded. js in the page calls the parent directly after obtaining the parameters in the request. parent. process (height, width); finish setting the width and height of the parent page.
see the following sequence chart:

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.