JavaScript uses Window.name to deliver data across domains

Source: Internet
Author: User


Name is a global property of window, including ie6,7, which is supported by almost all browsers.

Using Window.name to transfer data across domains is a feature of its use: Window.name has been saved even though the refresh, url jump

The implementation principle is very simple, the homepage uses the IFRAME to load the Outland data page, the Outland page sets this iframe the Window.name value. After loading, the IFRAME jumps back to the blank page of the domain, this time the main page can use Iframe.contentWindow.name to take the value.

Three pages: 111cn.net/main.html (Home page), 111cn.net/blank.html (blank page), b.com/data.html (Data page in Outland)

1. The data.html code for the data page in Outland is very simple, pass the data to Window.name, code as follows

<script type= "Text/javascript" >
Window.name = ' Holle world ';
</script>
2. The homepage of this domain main.html, listens to the onload event of the IFRAME, after loading the data page of the Outland, points to the local blank.html, then obtains the data, the code is as follows:

var state = 0;
var iframe = document.createelement ("iframe");
IFRAME.SRC = "http://www.b.com/data.html";
if (window. VBArray) {
Iframe.onreadystatechange = function () {
if (iframe.readystate = = ' Compltete ') {
if (state = = 1) {
var data = Iframe.contentWindow.name; You can get the data at this point.
} else {
state = 1;
IFRAME.SRC = "http://www.111cn.net/blank.html";
}
}
}
} else {
Iframe.onload = function () {
if (state = = 1) {
var data = Iframe.contentWindow.name; You can get the data at this point.
} else {
state = 1;
IFRAME.SRC = "http://www.111cn.net/blank.html";
}
}
}
Document.body.appendChild (IFRAME);
The last thing to note is that Window.name can only store data in string format, and if, window.name=1, it is automatically converted to the string "1"

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.