Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery动态向页面添加脚本时,不会使用缓存文件_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery动态向页面添加脚本时,不会使用缓存文件

Javascript 使用jQuery动态向页面添加脚本时,不会使用缓存文件,javascript,jquery,Javascript,Jquery,我正在使用jQuery向我的页面动态添加一个脚本,它可以工作,但是jQuery在URL上附加了“=TIMESTAMP”,导致浏览器从不使用缓存。使用以下代码: <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xht

我正在使用jQuery向我的页面动态添加一个脚本,它可以工作,但是jQuery在URL上附加了“=TIMESTAMP”,导致浏览器从不使用缓存。使用以下代码:

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        $("head").append('<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js"></scr' + 'ipt>');
    </script>
</body>
</html>
有人知道如何告诉jQuery不要这样做吗


谢谢

您可以使用
$.ajax
来获取脚本,而不是附加脚本标记

$.ajax({
  url: "http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js",
  dataType: "script",
  cache: true,//This will decide whether to cache it or no so that it will not add the timestamp along with the request
  success: function(){}//In the success handler you can write your code which uses resources from this js file ensuring the script has loaded successfully before using it
});

要回答您的原始问题,可以看到附加的时间戳,因为jQuery为
script
jsonp
调用设置了
cache:false
,这些调用将时间戳附加到URL

要避免时间戳,可以执行以下操作:

$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
  if ( options.dataType == 'script' || originalOptions.dataType == 'script' ) {
      options.cache = true;
  }
});
这将为所有
$.ajax
调用设置一个全局调用,包括jQuery在请求
脚本时发出的调用

我们检查
数据类型
,以确保我们没有无意中针对其他ajax调用,并显式地将
缓存
设置为
。这将避免时间戳附加问题


您现在可以使用原始代码,它将从缓存中提取它。

这里有一些对我有用的东西

// Backup the jquery ajax cache setting
var jQueryAjaxSettingsCache = jQuery.ajaxSettings.cache;

// Disable the ?_=TIMESTAMP addition
jQuery.ajaxSettings.cache = true;

// Do the cached script inserting ...
$("head").append('<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js"></scr' + 'ipt>');

// Restore the jquery ajax cache setting
jQuery.ajaxSettings.cache = jQueryAjaxSettingsCache;
//备份jquery ajax缓存设置
var jQueryAjaxSettingsCache=jQuery.ajaxSettings.cache;
//禁用?u=时间戳添加
jQuery.ajaxSettings.cache=true;
//执行缓存脚本插入。。。
$(“标题”)。附加(“”);
//恢复jquery ajax缓存设置
jQuery.ajaxSettings.cache=jqueryyajaxsettingscache;

您可以使用jquery的
getScript
方法在页面上获取js脚本文件,而不是附加脚本标记。作为提示,您应该使用以“//ajax.googleapis…”开头的URL,而不是“…”。因为如果您的页面未通过SSL访问,这将导致错误。请注意,您获得的url需要支持此黑客行为(谷歌服务提供)。这将确保你的页面在SSL上和没有SSL的情况下都能正常工作。我想你的意思是
缓存上的
true
,而不是
true
。你的解决方案正是我所需要的,但我没有将其标记为答案,因为我仍然想知道是否有办法解决原始问题。在这个特定的例子中,我只是想加载脚本,但是如果我是从使用脚本包含的ajax请求加载HTML呢?我还是想用缓存。回答得很好。另一个对我的特殊情况有效,这是一个更完整的答案。这帮助了我,谢谢。为了完整起见:那
需要是
=
@Mrchief,你把这段代码放在哪里?我试着把它放在头上,但是每个脚本标记之后仍然会附加一个时间戳。所以,若你们把它放在头上,一定要在下载jQuery之后放if。另外,检查代码的任何其他部分是否正在再次下载jQuery。如果是这样的话,那么jQuery下载将消灭这个家伙。
// Backup the jquery ajax cache setting
var jQueryAjaxSettingsCache = jQuery.ajaxSettings.cache;

// Disable the ?_=TIMESTAMP addition
jQuery.ajaxSettings.cache = true;

// Do the cached script inserting ...
$("head").append('<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js"></scr' + 'ipt>');

// Restore the jquery ajax cache setting
jQuery.ajaxSettings.cache = jQueryAjaxSettingsCache;