javascript - 我可以缓存由 jQuery.getScript() 加载的内容吗

标签 javascript jquery ajax file loading

根据 jQuery 文档 (https://api.jquery.com/jquery.getscript/) 使用更灵活的 $.ajax() 方法,但它不适用于此处描述的我 (jQuery cannot load plugin file using ajax before calling the plugin function, thus, gives kind of weird result)

By default, $.getScript() sets the cache setting to false. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested. You can override this feature by setting the cache property globally using $.ajaxSetup():

$.ajaxSetup({
  cache: true
});

但我需要缓存一些内容,而不是全部。

Alternatively, you could define a new method that uses the more flexible $.ajax() method.

它对我不起作用,因为它不能保证按顺序加载文件。

现在这种情况的最佳解决方案是什么?

最佳答案

$.getScript({
    url: "foo.js",
    cache: true
})

jQuery 1.12.0 或更高版本支持

关于javascript - 我可以缓存由 jQuery.getScript() 加载的内容吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630017/

相关文章:

javascript - 如何在创建 React 应用程序时使用 webpack devServer 代理

Jquery:悬停时不透明度变化不起作用

javascript - 防止页面不断重新加载

jquery - 如何管理:absolute css block位置的图层

c# - 如何将参数传递给在 JQuery dialog() Open 事件上调用的操作

php - 使用 ajax 自动完成处理下拉选择框选项

javascript - 以二维方式显示迷宫游戏服务的单元格

javascript - 如何在不在 JS 中声明全局变量的情况下将值从一个函数传递给另一个函数?

javascript - Phantomjs onResourceReceived 响应体工作状态

javascript - Electron的WebView.loadURL触发重新加载