One, $.getscript () method
- Sometimes it is completely unnecessary to get all the JavaScript files you need when the page is first loaded, and can be taken as needed.
- This function is used to dynamically load the JS file and execute the JS code in the file at the global scope.
- This function can load a cross-domain JS file. Note that the function is loading data asynchronously .
- The function belongs to the global
jQuery
object.
Grammar:
$ (function() { $ ("send"). On ("click",function() { $.getscript (" Script.js ",function() { //// callback //data: returned data ///Textstatus: A string describing the status } );});
Source code for the $.getscript () method:
function (URL, callback) { return jquery.get (url, undefined, callback, "script" ); }
In this method, the $.get () method is called, and the parameters of the response are passed in.
The syntax structure of the $.get () method is as follows:
Jquery.get (URL [, data] [, success] [, type])
The $.get () method is equivalent to:
"GET""script"});
Second, $.getjson () method
- This method is used to load the JSON file
- If the URL of the request includes "callback=?" And so on, jquery automatically sees it as a JSONP and executes the corresponding callback function to get the JSON data.
- The function is to load the data asynchronously .
- The function belongs to the global
jQuery
object.
Grammar:
$ (function() { $ ("send"). On ("click",function() { $.getjson (" Test.json ",function(data) { /// callback function
Data: Returned
Textstatus: A string describing the status of } );});
Source code for the $.getjson () method:
function (URL, data, callback) { return jquery.get (URL, data, callback, "JSON" ); }
In this method, the $.get () method is also called, and the corresponding parameters are passed in.
$.getjson () is equivalent to:
"GET""JSON"});
Both the *$.getscript () and $.getjson () methods are implemented through the encapsulation of the $.get () method. Belongs to the third tier method.
In short
Third floor |
$.getjson (), $.getscript () |
Second floor |
$.get (), $.post () |
First floor |
$.ajax () |
Ajax--getscript ()/getjson () method in jquery source code