缓存问题异步JavaScript加载与onload事件 [英] Caching problem with asynchronous javascript loading with onload event
问题描述
我目前正在异步加载一些js文件,使他们无法阻止网站的其余部分。
我主要是跟着在这里找到的说明:
I am currently trying to load some js files asynchronously, so that they are not able to block the rest of the website. I mainly followed the descriptions found here:
在javascript文件这一伟大工程的无阻塞负荷方面,但我现在得到的JavaScript文件被缓存,并保持高速缓存,即使我改变内容的问题(也做移重载没有帮助任何东西)
In terms of the non blocking loading of the javascript file this works great, but i got now the problem that the javascript file is cached and stays cached even if i change the content (also doing shift-reload does not help anything).
加载脚本的我现在的code如下所示:
My current code of loading the script looks like the following:
(function() {
function xx_async_load() {
var xx = document.createElement('script');
xx.type = 'text/javascript';
xx.async = true;
xx.src = 'http://myserver.de/myjs.js';
var el = document.getElementsByTagName('script')[0];
el.parentNode.insertBefore(xx, el);
}
if (window.addEventListener) {
window.addEventListener('load', xx_async_load, false);
} else if (window.attachEvent){
window.attachEvent('onload', xx_async_load);
}
})();
如果我直接调用里面的xx_async_load的code和改变myjs.js,变化越来越认可,但如果我加载此通过onload事件,它总是保持高速缓存和改变永远不会承认。
If i call the code inside "xx_async_load" directly and change the myjs.js, the changes are getting recognized, but if I am loading this through the onload event it always stays cached and the changed are never recognized.
是否有人知道一个解决方案,我该怎么做,以识别缓存文件(问题出现在歌剧,FF和IE浏览器很好地工作)?
Does anybody know a solution how I make the browser to recognize the changes in the cached files (problem appears in Opera, FF and IE work fine)?
编辑:如果我看歌剧蜻蜓的网络选项卡中,竟然没有上重装了缓存的JS文件做了一个请求,它似乎是直接从加载它高速缓存甚至没有检查对服务器上的文件。
If i look at the "Network" tab of Operas Dragonfly, there isn't even a request done on reload for the cached JS file, it seems that it is directly loading it from cache without even checking against the file on the server.
EDIT2:我将测试它保留在缓存中多久。如果它消失到明天的罚款。否则我还是可以提出一个日期参数的解决方法(因此接受这个答案)。再次THX。
I will test how long it stays in the cache. If its gone till tomorrow its fine. Else I can still propose the workaround with a date param (so accepting that answer). Thx again.
推荐答案
是的,这是相当简单的。
Yes, this is fairly simple.
只要给一个随机参数您的网址,如:URL = http://www.yoururl.com - > http://www.yoururl.com/?number=(random 号)
Just give a random parameter to your URL, like : URL = http://www.yoururl.com -> http://www.yoururl.com/?number=(random number)
这样,你将永远有一个唯一的URL。参数将由页面当它被加载,因为它不用于被扔掉。
this way you will always have a unique url. the parameter will be thrown away by the page when it is loaded because it is not used.
让我知道,如果这有助于
Let me know if this helped
这篇关于缓存问题异步JavaScript加载与onload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!