jQuery 版本 1.5 - ajax - <script>标签时间戳问题
如果我使用 ajax (jQuery) 加载一些包含脚本标签的内容,jQuery 1.5 会将时间戳添加到脚本标签 src url.请参见下面的示例.
If I load some content with ajax (jQuery) which has a script tag in it, jQuery 1.5 adds the timestamp to the script tag src url. See example bellow.
示例:内容我用 ajax 加载的内容:
Example: content what I load with ajax:
<div>text1</div>
<script type="text/javascript" src="/js/abc-xyz.js?r=1.1"></script>
这是在我将之前的内容插入页面后加载脚本代码的 src url:
This is the src url from where it loads the script code after I insert the previous content to the page:
.../js/abc-xyz.js?r=1.1&_=1297892228466
有人知道为什么会这样吗?它只发生在 jQuery 1.5 上.jQuery 1.4.4 不会发生这种情况.
Does anybody knows why this happening? It happens only with jQuery 1.5. It doesn't happen with jQuery 1.4.4.
代码示例:
$.ajax({
url: content.html,
type: 'GET',
data: someDataObject,
success: function(data) {
// some code here
},
error: function(data) {
// some code here
}
});
谢谢.
推荐答案
请看下面我从 jQuery 团队得到的答案.票证 #8298:http://bugs.jquery.com/ticket/8298
See bellow the answer what I got back from jQuery team. Ticket #8298: http://bugs.jquery.com/ticket/8298
答案:
在检查了您的报告和代码示例后,我得出结论,这不是错误.我还制作了 这个测试用例 jQuery 1.4+(直到 1.5)有一个导致缓存选项的错误对于脚本请求,不要默认为 false.此错误(请参阅 #7578)已在 1.5 中修复.现在您可能知道或不知道的是,jQuery 在执行 DOM 操作时会处理特殊处理脚本标签(以防止 IE 中的某些错误).它过滤掉它们并通过ajax请求它们.这就解释了为什么即使是正常"的内联脚本标签也会突然被请求附加 url 参数.如果它对您有不良副作用,有一些方法可以解决此问题.
After checking your report and your code samples I come to the conclusion that this isn't a bug. I also made this test case jQuery 1.4+ (until 1.5) had a bug which caused the cache option not to default to false for script requests. This bug (see #7578) has been fixed in 1.5 . Now what you might know or not know is, that jQuery does special-handle script tags when doing DOM manipulations (to prevent certain errors in IE). It filters them out and requests them via ajax. This explains why even a "normal" inline script tag suddenly is requested with additional url parameters. There are ways to work around this if it has unwanted side effects for you.
在适当的时候使用
$.ajaxSetup({ cache: true })
使用 prefilter 处理脚本请求,例如检查您不想要的网址要添加并设置缓存的随机参数:在预过滤器中为 true
use a prefilter for script requests and e.g. check for urls where you don't want the random parameter to be added and set cache: true in the prefilter for those
例如成功回调通过执行这些操作自己处理脚本标签..
in e.g. the success call back handle the script tags yourself by doing something along these..
..行:
var elems = $(htmlwithscripttags);
elems.filter("script") //now do whatever with the scripts
elems.filter(":not(script)").appendTo("body"); //e.g.
相关文章