$.getScript
最后更新于:2022-04-02 03:19:24
[TOC]
## 语法
```
jQuery.getScript( url [, success(script, textStatus, jqXHR) ] )
```
相当于
```
$.ajax({
url: url,
dataType: "script",
success: success
});
```
### 缓存响应
全局的使用 `$.ajaxSetup()`设置cache(缓存)属性覆盖该功能
```
$.ajaxSetup({
cache: true
});
```
## 示例
### 导入脚本并执行
```
$.getScript("ajax/test.js", function(data, textStatus, jqxhr) {
console.log(data); //data returned
console.log(textStatus); //success
console.log(jqxhr.status); //200
console.log('Load was performed.');
});
```
### 定义了一个 $.cachedScript() 方法可以获取缓存的脚本
```
jQuery.cachedScript = function(url, options) {
// allow user to set any option except for dataType, cache, and url
options = $.extend(options || {}, {
dataType: "script",
cache: true,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
// Return the jqXHR object so we can chain callbacks
return jQuery.ajax(options);
};
// Usage
$.cachedScript("ajax/test.js").done(function(script, textStatus) {
console.log( textStatus );
});
```
';