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

2023-03-17前端开发问题
1

本文介绍了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;标签时间戳问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
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...
2024-10-18 前端开发问题
125