且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于jQuery的getScript()的问题

更新时间:2022-02-10 07:03:48

1) jQuery对通过AJAX的脚本请求永远不会被缓存,除非你指定它作为$ .ajax()函数中的一个选项。来自文档

1) jQuery requests for scripts via AJAX are never cached unless you specify that as an option in the $.ajax() function. From the documentation:

cache,Boolean

默认值:true,false表示dataType'script'和'jsonp'

如果设置为false,它将强制您请求的页面不被浏览器缓存。

2)我想我需要看一些示例代码来掌握这部分问题。

2) I think I need to see sample code to grasp this part of the question.

3)如果$ .getScript()失败,则无法执行任何操作。但是,您应该知道$ .getScript()只是$ .ajax()的简写版本,等同于:

3) You can't do anything if $.getScript() fails. But, you should be aware that $.getScript() is just a short-hand version of $.ajax(), equivilant to:

$.ajax({
  url: url,
  dataType: 'script',
  success: function(data) {
    //
  }
});

这意味着你可以实现错误回调,如果文件加载失败,做一些聪明的事情,即:

This means that you can implement the error callback, to do something clever if the file fails to load, ie:

$.ajax({
  url: url,
  dataType: 'script',
  success: function(data) {
    //
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert("panic");
  }
});