ajax getScript() method
Translation results:
get
英[get] 美[ɡɛt]
vt. Get; catch; persuade; receive (punishment, etc.)
vt.& vi. Arrive, come
vi. Become; start; try to deal with; obtain benefits or wealth
n. Reproduce, cub; profit
script
英[skrɪpt] 美[skrɪpt]
n.Script, handwriting; written typeface; script, broadcast script or movie script
vt. Write a script for a movie (or drama, etc.); make it up
ajax getScript() methodsyntax
Function: getScript() method loads and executes JavaScript files through HTTP GET requests.
Syntax: jQuery.getScript(url,success(response,status))
Parameters:
Parameters | Description |
url | The URL string to be requested. |
success(response,status) | Optional. Specifies the callback function to be executed after the request is successful. Additional parameters: response - contains the result data from the request status - contains the status of the request ("success", "notmodified", "error", "timeout" or "parsererror") |
Description: This function is the abbreviated Ajax function, equivalent to: $.ajax({url: url, dataType: "script",success: success}); The callback function here will Pass in the returned JavaScript file. This is usually not very useful since the script has already been run by then. Loaded scripts are executed in the global environment and therefore can reference other variables and use jQuery functions. For example, load a test.js file, which contains the following code: $(".result").html("<p>Lorem ipsum dolor sit amet.</p>"); By referencing the file name, You can load and run this script: $.getScript("ajax/test.js", function() {alert("Load was performed.");});
ajax getScript() methodexample
<html> <head> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $.getScript("这里是你的JavaScript文件地址"); }); }); </script> </head> <body> <button>使用 Ajax 来获得并运行一个 JavaScript 文件</button> </body> </html>