Load and execute a JavaScript file from the server through an HTTP GET request
jquery.getscript (URL, [Success (data, Textstatus)])
URL a URL string containing the request sent.
Success (data, textstatus) a callback function that executes when a request succeeds.
This is a fast AJAX processing function, which is equivalent to:
The code is as follows |
Copy Code |
$.ajax ({ Url:url, DataType: ' Script ', Success:success }); |
Callback by a file that returns JavaScript. Usually not useful as the script has been executed to this point.
This script has been executed in the global environment, so point to other variables and use the jquery function. The included script must have some effect on the current page:
The code is as follows |
Copy Code |
$ ('. Result '). html (' <p>lorem ipsum dolor sit amet.</p> '); |
By referencing this file name, the script is included and executed:
The code is as follows |
Copy Code |
$.getscript (' Ajax/test.js ', function () { Alert (' Load was performed. '); }); |
Jquery.getscript () cross-domain request
The code is as follows |
Copy Code |
Can implement Cross-domain requests to get remote resources
$.getscript ("http://www.vipinit.com/test.php", function (data, textstatus) { Console.log (data); Data returned Console.log (Textstatus); Success Console.log ("Load was performed."); }); |
Test.php need to return a JSON result.
You can also use this method:
The code is as follows |
Copy Code |
$.ajax ({ Url:url, DataType: "Script", Success:success }); |
Jquery.getscript cannot set the encoding type, $.ajax can make up for the
The code is as follows |
Copy Code |
$.ajax ({ Url:url, DataType: "Script", Scriptcharset: "gb2312", Success:success });
|
In addition to resolving cross-domain problems, you can also load remote scripts through the script label's SRC:
The code is as follows |
Copy Code |
<script src= "test.php" type= "Text/javascript" ></script> |
or open a new page under this domain to make a remote request, return the data to the domain under the access