JavaScript uses Location.hash to transfer data across domains

Source: Internet
Author: User
Tags hash

Previously written on the use of window.name to transfer data across the domain, the principle of the Location.hash method is similar to the use of Location.hash changes will not refresh the feature, by referencing the data page in Outland to modify the main page of the Location.hash to achieve the purpose of transmitting data.

Also three pages: 111cn.net/main.html (Home page), 111cn.net/proxy.html (Agent page), b.com/data.html (Data page in Outland)

1. main.html code for the home page of this domain

var iframe = document.createelement ("iframe");
IFRAME.SRC = "http://www.b.com/data.html";
if (window. VBArray) {//if it is IE
Iframe.onreadystatechange = function () {
if (iframe.readystate = = ' complete ') {
alert (Location.hash);
}
};
} else {
Iframe.onload = function () {
alert (Location.hash);
};
}
Document.body.appendChild (IFRAME);

2. The code for data.html in Outland is as follows

var data = "some datas";
try {
Parent.location.hash = data;
catch (e) {
var iframe = document.createelement ("iframe");
IFRAME.SRC = "http://www.111cn.net/proxy.html#" + data;
Document.body.appendChild (IFRAME);
}

3. The proxy page for this domain proxy.html the following code

Because all two pages are of the same domain, you can set the hash value of the main.html

Parent.parent.location.hash = Self.location.hash;

and Window.name cross-domain transmission of data is the same disadvantage, the transmission of data is a string type, if you want to pass the JSON data, the first to be converted to a string in data.html, and then in main.html in the conversion back, and the data are exposed in the URL, but also a limit is the length of the URL E can only be 2048 characters, which cannot pass too much data.

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.