如何使用javascript和基本脚本加载强制不缓存

时间:2012-02-22 17:31:28

标签: javascript caching src

我有一个继续使用http://www.imvu-e.com/products/hpv/download/HPV.js缓存版本的书签。我希望它永远不会缓存它并始终重新加载它。

这是我用来保存书签的超链接(用户拖动到安装它的浏览器工具栏):

<a href="javascript:(function(){
     c = document.createElement(&quot;script&quot;);
     c.type = &quot;text/javascript&quot;;
     c.src = &quot;http://www.imvu-e.com/products/hpv/download/HPV.js&quot;;

     c.onload = c.onreadystatechange = function()                    {
            if ( ! (d = this.readyState) || d == &quot;loaded&quot; || d == &quot;complete&quot;){
                document.documentElement.childNodes[0].removeChild(c);
                version='beta';
            }
     };
     document.documentElement.childNodes[0].appendChild(c);
})();">Run HPV</a>

3 个答案:

答案 0 :(得分:17)

在网址的末尾添加一个无用的查询字符串:

c.src = "http://www.imvu-e.com/products/hpv/download/HPV.js?" + (new Date).getTime(); 

答案 1 :(得分:0)

使用jQuery的getScript而不是脚本标记并更改源

names = ["john", "mike", "soniya"]

# Use a list comprehension to create the objects and put them in a list
employees = [employee(name) for name in names]

for employee in employees:
    employee.display()

答案 2 :(得分:0)

纯JS解决方案:

<script> 
var scr = document.createElement("script");
scr.src = "http://example.com/cool.js" + "?ts=" + new Date().getTime();
document.getElementsByTagName("head")[0].appendChild(scr);
</script>