如何通过$ .getScript()获取脚本文件

soz*_*hen 5 javascript jquery

我试图从一些代码主机导入一些JavaScript文件.

    $.when(
        $.getScript('http://pingzi.googlecode.com/svn-history/r30/branches/wangqi/web/jquery.window.min.js'),
        $.getScript('http://mottie.github.com/tablesorter/js/jquery.tablesorter.js'),
        $.getScript('http://tconnell.com/samples/scroller/lib/jquery.tablesorter.scroller.js'),
        $.getScript('http://code.highcharts.com/stock/highstock.js'), 
        $.Deferred(
            function(deferred) { 
                $(deferred.resolve);
            }
        )
    ).done(function() {  
       // my function goes here....
    });
Run Code Online (Sandbox Code Playgroud)

当我尝试调用这些URL来导入js文件时,URL会附加?_=1344036242417,然后我实际上无法访问我想要的脚本文件.

"NetworkError: 404 Not Found - http://pingzi.googlecode.com/svn-history/r30/branches/wangqi/web/jquery.window.min.js?_=1344036242417"

任何人都有想法如何绕过这个问题?先感谢您.

ade*_*neo 7

那是因为在jQuery中默认关闭ajax中的缓存,打开它并删除查询字符串do:

$.ajaxSetup({ 
    cache: true 
}); 
Run Code Online (Sandbox Code Playgroud)

但这也可能会影响你不想缓存的其他ajax调用,在getScript文档中还有很多这方面的内容,甚至还有一些关于创建一个名为cachedScript的缓存getScript函数的方法.

您还可以通过使用新选项重新定义函数来启用$ .getScript中的缓存,以通过传递true或false来打开/关闭缓存:

$.getScript = function(url, callback, cache){
    $.ajax({
            type: "GET",
            url: url,
            success: callback,
            dataType: "script",
            cache: cache
    });
};
Run Code Online (Sandbox Code Playgroud)


Łuk*_*.pl 5

jQuery有这种查询的自动缓存机制.如果您不想添加额外的参数,请使用以下设置:

$.ajaxSetup({
  cache: true
});
Run Code Online (Sandbox Code Playgroud)

来源:http://api.jquery.com/jQuery.getScript/#caching-requests