加载外部脚本没有使用Javascript进行缓存 [英] Load external <script> without cache using Javascript
本文介绍了加载外部脚本没有使用Javascript进行缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个外部javascript文件加载到页面中,并确保未缓存该文件.我无权访问php,因此无法在文件名后生成随机字符串.
I want to load an external javascript file into the page and make sure its not cached. I do not have access to php so I cant generate a random string after the filename.
在PHP中,脚本如下所示:
In PHP the script would look like this:
<script src="http://site.com/cool.js?<?php echo $randomnumber; ?>"></script>
有没有一种方法可以仅使用javascript来做到这一点?
Is there a way to do something like that using only javascript?
推荐答案
使用jQuery的 getScript 代替脚本标签
Use jQuery's getScript instead of a script tag.
$.getScript("http://example.com/cool.js");
或纯JavaScript
or pure JavaScript
var scr = document.createElement("script");
scr.src = "http://example.com/cool.js" + "?ts=" + new Date().getTime();
document.getElementsByTagName("head")[0].appendChild(scr);
这篇关于加载外部脚本没有使用Javascript进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文