加载外部< script>没有缓存使用Javascript [英] Load external <script> without cache using Javascript
本文介绍了加载外部< script>没有缓存使用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);
这篇关于加载外部< script>没有缓存使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文