jQuery 版本 1.5 - ajax - <script>标签时间戳问题

jQuery version 1.5 - ajax - lt;scriptgt; tag timestamp problem(jQuery 版本 1.5 - ajax - lt;scriptgt;标签时间戳问题)
本文介绍了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.

  1. 在适当的时候使用 $.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.

这篇关于jQuery 版本 1.5 - ajax - &lt;script&gt;标签时间戳问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)