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.