强制动态加载的 JavaScript 不在 google chrome 上缓存
我正在开发谷歌地图加载器(使用谷歌地图 javascript API)。我想在可用时将点动态添加到路径中。 编辑:网页和 JavaScript 是在本地存储和加载的,而不是通过网络服务器。
为此,我创建了一个外部 javascript 文件,在其中放置了我想要使用的数据。
我使用我在 stackoverflow 上其他地方找到的一个很棒的技巧来加载 java 脚本对象:
function loadjsfile( filename, callback )
{
var fileref = document.createElement( 'script' );
fileref.setAttribute( "type", "text/javascript" );
fileref.setAttribute( "src", filename );
var done = false;
fileref.onload = fileref.onreadystatechange = function()
{
if( !done && ( !this.readyState
|| this.readyState == "loaded"
|| this.readyState == "complete") )
{
done = true;
// Continue your code
callback();
// Handle memory leak in IE
fileref.onload = fileref.onreadystatechange = null;
document.getElementsByTagName( "script" )[0].removeChild( fileref );
}
};
document.getElementsByTagName( "script" )[0].appendChild( fileref );
}
这非常有效,因为它会等到外部 javascript 实际加载后再继续初始化。
所以这一切都很棒,加载得很好。然后我设置了 5 秒的超时来重新加载外部 javascript 文件。这是我有问题的地方。 Google Chrome 正在从缓存中重新加载外部 JavaScript 文件,而不是直接加载到磁盘。这是一个巨大的痛苦,因为这意味着我的地图路径不会随着时间的推移而更新。我真的需要它。
当我刷新整个页面时,它重新加载了 JavaScript,但闪烁非常烦人(它迫使地图回到“初始”状态)。如果我可以强制它每次重新加载 javascript 文件,这个新方法将完美地工作。
那么有人可以帮助我吗?
我对 Javascript 很陌生(我主要是 C++ 编码员),所以如果我问一些愚蠢的问题,我深表歉意:D
I'm working on a google maps loader (using the google maps javascript API). I want to dynamically add points to a path as and when they come available. Edit: The webpage and javascript are stored and loaded locally and not through a web server.
To this end I have created an external javascript file in which I put the data I want to use.
I load the java script object using a great trick I found elsewhere on stackoverflow:
function loadjsfile( filename, callback )
{
var fileref = document.createElement( 'script' );
fileref.setAttribute( "type", "text/javascript" );
fileref.setAttribute( "src", filename );
var done = false;
fileref.onload = fileref.onreadystatechange = function()
{
if( !done && ( !this.readyState
|| this.readyState == "loaded"
|| this.readyState == "complete") )
{
done = true;
// Continue your code
callback();
// Handle memory leak in IE
fileref.onload = fileref.onreadystatechange = null;
document.getElementsByTagName( "script" )[0].removeChild( fileref );
}
};
document.getElementsByTagName( "script" )[0].appendChild( fileref );
}
This works perfectly as it waits until the external javascript has actually loaded before proceeding with the intialisation.
So thats all great it loads up fine. I then set a 5 second timeout which reloads the external javascript file. This is where I have an issue. Google chrome is re-loading the external javascript file from cache instead of going straight to disk. This is a huge pain as it means that my map path doesn't update as it goes along. I really need it to.
When I refreshed the entire page then it was fine it reloaded the javascript but the flicker was very annoying (and it forced the map back to the "initial" state). This new method would work perfectly provided I could force it to re-load the javascript file everytime.
So can anyone help me?
I'm very new to Javascript (I'm mainly a C++ coder) so apologies if I'm asking something stupid :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有其他人有其他想法,请尝试将随机数作为查询字符串附加到文件名中。这将告诉脚本该文件是新并且应该强制重新加载。
基本上每次都会生成一个随机数。
所以你的代码看起来像这样
If noone else has other ideas, try appending a random number as a query string to the filename. This will tell the script the file is new and should force a reload.
Basically generate a random number each time.
So you code would look something like
您可以(如果您的文件托管在 apache Web 服务器上)将 .htaccess 文件添加到该目录,其中包含带有以下内容的脚本文件:
这将阻止文件被缓存。
还有其他方法可以发送这些标头信息。
You could (if your file is hosted on an apache webserver) add a .htaccess file to the directory, containing your script file with the content:
This will prevent the file from being cached.
There are also other ways to send these header informations.
您可以放置 date.now() ,而不是像 Marko 建议的那样使用随机数,它在每次新上传时绝对总是不同的,并且写入更加紧凑。
经过测试并适用于 Chrome、Opera、Edge 和 Firefox。
Instead of using a random number as Marko suggested, you can put date.now() which is absolutely always different with each new upload and writing is more compact.
Tested and works with Chrome, Opera, Edge and Firefox.