关于jQuery的getScript()的问题 [英] Questions about jQuery's getScript()

查看:248
本文介绍了关于jQuery的getScript()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.getScript("somescript.js", function() {
    alert('Load Complete');
});




  1. 一旦加载,它是否被缓存,或者如果被引用更多则再次加载

  2. 如果某个事件依赖于正在加载的文件,那么如果文件需要更长时间加载或不加载,事件是否会延迟失败/超时?

  3. 如果由于某种原因导致文件无法加载,如何检查并执行某些操作?

谢谢提前帮助。

推荐答案

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");
  }
});

这篇关于关于jQuery的getScript()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆