JavaScript cross-domain access is a common problem for web developers, what is Cross-domain, a script loaded on one domain Gets or operates a document property on another domain, and three implementations of JavaScript Cross-domain methods are listed below:
1. Cross-domain implementation based on IFRAME
cross-domain requirements based on IFRAME implementation The aa.xx.com,bb.xx.com feature is that two pages must belong to an underlying domain (for example, all xxx.com, or xxx.com.cn), using the same protocol (for example, all HTTP) and the same port (for example, all are 80) so that you can add document.domain to the two pages and then implement the function of the parent page calling the child page, as follows:
Page One:
Page Two:
At this time, the parent page can call the child page a function, to achieve the JS Cross-domain access
2. Implementing Cross-domain based on the script label
The script label itself has access to resources from other domains, is not limited by the browser's homology policy, and can be created dynamically by creating a script label on the page, as follows:
var script = document.createelement (' script ');
SCRIPT.SRC = "Http://aa.xx.com/js/*.js";
Document.body.appendChild (script);
So by dynamically creating a script tag can load other domain JS files, and then through this page can be called after the Load JS file function, the flaw is not to load other domain documents, can only be JS file, Jsonp is achieved by this way, Jsonp by passing in a callback parameter to another domain, wrapping the callback parameter value and the JSON string into a JavaScript function back through the background of the other domain, because the request is made through the script label, The browser will parse the returned string into JavaScript to implement the data transfer between the field and the domain.
The support for JSONP in jquery is also based on this scenario.
3. The Backstage proxy way
This approach solves all cross-domain problems, that is, the background as a proxy, every time a request to another domain is forwarded to the background of the domain, the background of the domain is accessed by simulating an HTTP request to the other domain, returning the returned results to the foreground, and the benefit is that both the document being accessed and the JS file can be implemented across the domain.
The above JS to achieve Cross-domain access to the three methods first to everyone, we carefully study and learn, there will be some harvest