For security reasons, JavaScript does not allow cross-origin calls to objects on other pages. However, security restrictions also bring a lot of trouble to inject IFRAME or Ajax applications. Here we will simply sort out the cross-origin problems:
First of all, what is cross-origin is simply because of restrictions on the Javascript same-origin policy, JS under the.com domain name cannot operate on B .com or the objects under the c.a.com Domain Name
Because of project requirements, a single sign-on function is required, that is, two websites with different domain names log on and exit at the same time. This is a very good function of jquery.
Jquery. getscript (URL, [callback]): load and execute a Javascript file using GET requests.
URL (string): the URL of the JS file to be loaded.
Callback (function): (optional) callback function after successful loading.
Before jquery 1.2, getscript can only call JS files in the same domain. 1.2, you can call JavaScript files across domains. Note: Safari 2 or earlier versions cannot execute scripts simultaneously in the global scope. If you use getscript to add a script, add the latency function.
This method can be used, for example, to load the JS file required by the editor only when the editor focus (). See some sample code below:
Load and execute test. js. Jquery code:
$. Getscript ("test. js ");
--------------------------------------------------------------------------------
Load and execute ajaxevent. js. The information is displayed after successful execution.
Jquery code:
$. Getscript ("ajaxevent. js", function () {alert ("ajaxevent. JS is loaded and executed completely. You can click the get or post button above to see what is the difference? ");});