JavaScript cross-domain summarization window.name implementation of cross-domain data transfer _javascript techniques

Source: Internet
Author: User

I practice a bit, really good use. The specific implementation method is recorded as follows

There are three pages:

A.com/app.html: Application page.
A.com/proxy.html: Agent file, is generally an HTML file without any content, need and apply the page under the same domain.
B.com/data.html: The page where the application page needs to get the data, can be called the data page.

The basic steps to achieve this are as follows:

Create an IFRAME in the Application page (a.com/app.html) and point its SRC to the data page (b.com/data.html).
The data page attaches the data to the window.name of the IFRAME, and the data.html code is as follows:

  <script type= "Text/javascript" >
    window.name = ' I was there! ';  Here is the data to be transferred, the size is generally 2m,ie and Firefox can be large to 32M or so
                     //data format can be customized, such as JSON, string
  </script>

In the Application page (a.com/app.html) to monitor the onload event of the IFRAME, this event set this iframe src point to the local domain agent files (agent files and application pages in the same domain, so can communicate with each other). app.html part of the code is as follows:

<script type= "Text/javascript" >
    var state = 0, 
    iframe = document.createelement (' iframe '),
    LOADFN = function () {
      if (state = = 1) {
        var data = Iframe.contentWindow.name;  Read
        alert (data);  Eject ' I was there! '
      } else if (state = = 0) {state
        = 1;
        Iframe.contentWindow.location = "http://a.com/proxy.html";  Set Agent file
      } 
    };
    IFRAME.SRC = ' http://b.com/data.html ';
    if (iframe.attachevent) {
      iframe.attachevent (' onload ', LOADFN);
    } else {
      iframe.onload = LOADFN;
    }
    Document.body.appendChild (IFRAME);
  </script>

After the data is retrieved, the IFRAME is destroyed and the memory is freed, which is guaranteed to be secure (not accessed by the other domain frame JS).

<script type= "Text/javascript" >
    iframe.contentWindow.document.write (');
    Iframe.contentWindow.close ();
    Document.body.removeChild (IFRAME);
  </script>

The

concludes that: the SRC attribute of an IFRAME is shifted from Outland to the local domain, and cross-domain data is passed from Outland to the local domain by the window.name of the IFRAME. This cleverly bypasses the browser's Cross-domain access restrictions, but it is also a safe operation.

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.