Several methods of JavaScript cross-domain

Source: Internet
Author: User
Tags script tag

The following examples contain files that are for , with , to be done from a.html the data obtained in b.html the


jsonpis to use the script attribute with no cross-domain restriction on the label, by src appending the url callback function name to the parameter, and then the server receives the callback function name and returns a callback function containing the data

  function dosomething (data) {        //  to Data processing     }    var script = Document.createelement ("script");     = "Http://";    Document.body.appendChild (script);     // 1. Generate a script tag, append it on the body, make a request    to the server // 2. The server generates a function dosomething ({"A", "1"}) that contains the data according to the callback parameter.    // 3. The page has previously declared the DoSomething function, at which time the dosomething (data) function is executed to obtain the data

2.HTML5 's PostMessage

Let's say we're a.html nesting inside <iframe src="" frameborder="0"></iframe> , communicating with each other in these two pages.


function () {        Window.addeventlistener(function(e) {            alert (;        });        window.frames[0].postmessage ("B Data", "");    }


function () {        Window.addeventlistener(function(e) {            alert (;        });        Window.parent.postMessage ("A Data", "");    }

This opens a the page first a data , then pops upb data + iframe

window.nameThe principle is to use the same window on different pages share a , this need to create a proxy file c.html , so that a.html the homologous can be obtained after c.html


 variframe = document.createelement ("iframe"); IFRAME.SRC= "Http://"; Document.body.appendChild (IFRAME); //now a.html a reference to the b.html iframe, get B data    varFlag =true; Iframe.onload=function() {        if(flag) {IFRAME.SRC= "C.html"; //If the judgment is the first time, set the proxy c.html and a.html in the same directory as the same, in order to be in the following else to fetch the dataFlag =false; } Else{//second load due to a and C homology, a can directly obtain C's Window.namealert (;            Iframe.contentWindow.close ();            Document.body.removeChild (IFRAME); IFRAME.SRC= ' '; IFrame=NULL; }    }

B.html = "This is data on page B";
4.window.location.hash + iframe

b.htmlAppend the data to hash c.html the value, and upload it to the url c.html page by location.hash fetching it a.html (this example is uploaded a.html hash , and of course it can be uploaded elsewhere)


var iframe = document.createelement ("iframe");     = "";     // Reference B    on page A function // set a timer to constantly monitor the change of the hash, a change in the hash indicates that the        data passed over var hashs = window.location.hash;         if (HASHS) {            clearinterval (time);            Alert (hashs.substring (1));        }    }     var time = setinterval (check, 30);


function () {        var data = "This is B ' s data";          var iframe = document.createelement ("iframe");         = "" + data;         // append the data to the c.html hash    }


// get the hash of itself and then upload to a.html hash, data transfer is complete .

CORSIs XMLHttpRequest Level 2 a cross-domain approach stipulated in the. In browsers that support this approach, the wording is exactly the same as that of a javascript non-cross-domain ajax , as long as the server needs to setAccess-Control-Allow-Origin: *


This approach applies to the same primary domain, with different subdomains, such as http://www.a.com
If each of the two domains has a a.html and b.html ,


Document.domain = "";     var iframe = document.createelement ("iframe");     = "";    Document.body.appendChild (IFRAME);     function () {        //  manipulate element data in b.html here    }


Document.domain = "";


Note: document.domain The parent domain needs to be set to its own or higher level, and the primary domain must be the same.

Several ways to cross-domain JavaScript

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.