jQuery不断在我的CDN脚本中放入缓存破坏符? [英] jQuery keeps putting a cache buster in my CDN scripts?
问题描述
我有一个脚本,该脚本使用$.get( ... )
从服务器中提取一段HTML,并使用$.html
将其呈现在<div>
中.有时,在拉出的HTML中有<script>
标记,我注意到jQuery在对这些脚本的请求中放入了缓存破坏符参数,这导致浏览器再次请求已加载的脚本.
I have a script that uses $.get( ... )
to pull a piece of HTML from my server, and render it inside a <div>
with $.html
. Sometimes I have <script>
tags in the pulled HTML, and I noticed that jQuery is putting a cache buster parameter in the request for these scripts and it's causing the browser to request again for scripts that have already been loaded.
例如:
有什么主意我可以禁用此行为吗?
Any idea how I can disable this behavior?
脚本标签没有附加_
参数,因此问题不在于我的HTML.
The script tags don't have the _
param attached to them so it isn't my HTML that's the problem.
此外,未使用$.html
呈现的<script>
标签也没有这种现象.
Also, <script>
tags that aren't rendered with $.html
aren't exhibiting this behavior.
推荐答案
当jQuery在HTML中看到<script src="URL">
标签时,它会在内部使用$.getScript()
加载脚本.默认情况下,这会发送一个cachebuster,但是您可以通过调用$.ajaxSetup()
覆盖默认的AJAX选项.
When jQuery sees a <script src="URL">
tag in the HTML, it uses $.getScript()
internally to load the script. By default, this sends a cachebuster, but you can override the default AJAX options by calling $.ajaxSetup()
.
$(document).ready(function () {
$.ajaxSetup({
cache: true
});
var x = '<script src="https://d9zdvmiwibsh5.cloudfront.net/js/json.js">';
$('#test').html(x);
});
这篇关于jQuery不断在我的CDN脚本中放入缓存破坏符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!