JavaScript uses Window.name to troubleshoot cross-domain issues 1th/2 page _javascript tips

Source: Internet
Author: User
Window.name transmission technology, was originally Thomas FrankIt was invented to address some of the disadvantages of cookies (4 Kb per domain limit, data can only be strings, set and get the complexity of cookie syntax, and so on) (see Text in detail: "Session Variables without cookies"), and later Kris ZypOn the basis of this method, the Window.name transmission is strengthened and introduced into the Dojo( Dojox.io.windowNameTo resolve cross-domain data transfer issues.
The basic principle and steps of Window.name transmission technology are:
Name is a property of a global/window object in the browser environment, and the property value of name remains unchanged when a new page is loaded in the frame. By loading a resource in the IFRAME, the target page sets the Name property of the frame. This Name property value can be fetched to access the information sent by the Web service. However, the Name property is accessible only to the frame of the same domain name. This means that in order to access the Name property, when the Remote Web service page is loaded, the frame must be navigated back to the original domain. The homology policy still prevents other frame from accessing the Name property. Once the Name property is obtained, destroy the frame.

At the top level, the Name property is unsafe and any information that is set in the Name property is available for all subsequent pages. However, the Windowname module always loads resources in an IFRAME, and once you get the data, or when you browse a new page at the top, the IFRAME will be destroyed, so the other pages will never be accessible to the Window.name property.

Basic implementation code, based on YUI, from the Kerjun written sample:

Copy Code code as follows:

(function () {
var yud = YAHOO.util.Dom, YUE = YAHOO.util.Event;

Datarequest = {
_doc:document,
CFG: {
Proxyurl: ' proxy.html '
}
};

Datarequest.send = function (sURL, fncallback) {
if (!surl | | typeof surl!== ' string ') {
Return
}

sURL + = (surl.indexof ('? ') > 0? ' & ': '? ' + ' windowname=true ';

var frame = this._doc.createelement (' iframe '), state = 0, self = this;
This._doc.body.appendchild (frame);
Frame.style.display = ' None ';

var clear = function () {
try{
Frame.contentWindow.document.write (");
Frame.contentWindow.close ();
Self._doc.body.removechild (frame);
}catch (e) {}
};

var getData = function () {
try{
var da = frame.contentWindow.name;
}catch (e) {}
Clear ();
if (fncallback && typeof fncallback = = ' function ') {
Fncallback (DA);
}
};

Yue.on (frame, ' load ', function () {
if (state = = 1) {
GetData ();
else if (state = = 0) {
state = 1;
Frame.contentWindow.location = SELF.CFG.PROXYURL;
}
});

FRAME.SRC = sURL;
};
})();

Current 1/2 page 12 Next read the full text

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.