我写了一个小函数来动态包含CSS文件或JS文件.我用jquery函数加载JS文件$.getScript().
我希望在我的AJAX调用完成之前阻止我的函数返回.
我尝试过这样的东西,但它不起作用:
CAPTIVEA.resources._includeResource = function(resource) {
var ret = false;
if (CAPTIVEA.resources.isIncluded(resource))
{ // Resource already included => do nothing
console.log('CAPTIVEA.resources : "' + resource + '" is already included.');
ret = true;
}
else
{ // Resource not included => try to include it
var resType = CAPTIVEA.resources.getType(resource);
switch (resType)
{ // Check resource type to know how to include it
case 'js':
$.when(
$.getScript(resource, function(data, textStatus){
console.info('CAPTIVEA.resources : "' + resource + '" dynamically loaded.'); //success
})
).done(function(){
ret = true;
});
break;
case 'css':
// Load CSS file ...
break;
default:
console.error('CAPTIVEA.resources : "' + resource + '" is not an includable resource.');
break;
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用$ .ajax并将async设置为false,以便在继续之前等待响应:
$.ajax({
url: resource,
success: function(data, textStatus) {
console.info('CAPTIVEA.resources : "' + resource + '" dynamically loaded.'); //success
},
dataType: 'script',
async: false
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3151 次 |
| 最近记录: |