jQuery.getScript( "/path/to/myscript.js" , function (data, status, jqxhr) { /* 做一些加载完成后需要执行的事情 */ }); |
This getscript method returns a JQXHR, and you can use it as follows:
1234567 |
jquery.getscript ( "/path/to/myscript.js" )      .done ( function () { /* Yes, there's no problem here. */ .fail ( function () {          Code class= "JScript Comments" >/*, perform the salvage operation immediately */ |
The most common place to use jquery.getscript is to delay loading a JS plugin and execute it when the load is complete:
1234 |
jQuery.getScript( "jquery.cookie.js" ) .done( function () { jQuery.cookie( "cookie_name" , "value" , { expires: 7 }); }); |
If you need more advanced features, such as loading multiple scripts, or loading different types of files (text files, images, CSS, etc.), I suggest you use a dedicated JavaScript loading tool. If it is to delay loading a JS plugin, to avoid each page to load, this getscript is perfect.
jquery Dynamic load Script $.getscript ();