刷新外部加载的javascript文件 [英] Refresh external loaded javascript files

查看:105
本文介绍了刷新外部加载的javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多js文件,我将它们放在另一个文件夹中,以方便更新和在不同页面之间共享.这些文件是从另一个页面调用的,我使用

I have a number of js files that I put in an another folder for ease of updating and for sharing between different pages. These files are called from another page and I use the

<script type="text/javascript" src="/js/file.js"></script>

符号.

这可以正常工作,但是当我在这些js文件之一中更改内容时,浏览器(chrome,firefox,safari)仍会加载以前的版本而不进行更改,因此不会将其使用的js文件更新为已更新的js文件.我试图清理缓存以强制其再次加载js文件(带有更改),但无济于事.

This works fine but when I change something in one of these js files, the browser (chrome, firefox, safari) still loads the previous version without the changes, therefore not updating the js file it uses to the updated one. I tried to clean out the cache to force it to load the js file again (with the changes), but to no avail.

我唯一的解决方法是将外部文件重命名为file2.js并将其包含在调用它的页面中,但这是一个繁琐的过程,因为如果进行其他更改,我必须将名称更改为file3.js,等

The only workaround that I have is to rename the external file to file2.js and include that in the page calling it but it is a tedious process because if I make another change I have to change the name to file3.js, etc.

有没有一种方法可以强制浏览器重新加载原始js而不使用以前存储的js>

Is there a way to force the browser to reload the original js and not use a previously stored one>

推荐答案

您可以通过在URL中添加唯一的内容来强制刷新:

You can force the refresh by adding something unique in the URL:

在下面的代码中,必须为每个请求生成字符串"d6aa97d33d459ea3670056e737c99a3d".您可以根据需要使用时间戳,哈希,随机数. 因此,浏览器将不会重复使用其缓存中的内容,并且将始终下载JS文件.

In the code below the string "d6aa97d33d459ea3670056e737c99a3d" has to be generated for each request. You can use a timestamp, a hash, a random number, whatever you want. Because of this, the browser will not reuse what he has in cache and will always download the JS file.

<script type="text/javascript" src="/js/file.js?d6aa97d33d459ea3670056e737c99a3d"></script>

这篇关于刷新外部加载的javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆