无论我做什么,JavaScript 文件都不会更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3951187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:48:59  来源:igfitidea点击:

JavaScript file not updating no matter what I do

javascriptwindowsbrowser

提问by Tarik

I have an external JavaScript file and whether in FireFox or Chrome, whether all browsing data is cleared, it will NOT update no matter what. I believe somethinghappened when I made a backup of my file, which I simply added "_thedate" to the end of the name. Then Save As back to the original name.

我有一个外部 JavaScript 文件,无论是在 FireFox 还是 Chrome 中,无论是否清除了所有浏览数据,它都不会更新。我相信在备份文件时发生了一些事情,我只是在名称末尾添加了“_thedate”。然后另存为原名。

Now I cannot seem to get rid of the old JS no matter what unless I change the name of the file, which I really don't want to do, or add the script to the PHP page, which crowds it.

现在我似乎无论如何都无法摆脱旧的 JS,除非我更改文件的名称(我真的不想这样做),或者将脚本添加到 PHP 页面,这会使其拥挤。

Anyone know the solution to this?

有谁知道这个问题的解决方案?

回答by nonopolarity

You are sure you are linking to the same file and then editing that same file?

您确定要链接到同一个文件然后编辑同一个文件吗?

On some browser, you can use CTRLF5to force a refresh (on the PC). On the Mac, it is ShiftCmdR

在某些浏览器上,您可以使用CTRLF5强制刷新(在 PC 上)。在 Mac 上,它是ShiftCmdR

Firebug also has a net tab with "Disable Browser Cache"

Firebug 也有一个带有“禁用浏览器缓存”的网络选项卡

Update 2020:

2020 年更新:

If you are using Chrome and the DevTools is open, you can click and hold the Refresh icon in front of the address bar, and a box will pop up, and you can choose to "Hard Reload" or even "Empty Cache and Hard Reload":

如果你使用的是 Chrome 并且 DevTools 是打开的,你可以点击并按住地址栏前面的 Refresh 图标,会弹出一个框,你可以选择“Hard Reload”甚至“Empty Cache and Hard Reload” ”:

hard reload button

硬重新加载按钮

Update 2017:

2017 年更新:

If you use the Google Chrome debugger, it is the same, you can go to the Network section and make sure the "Disable cache (while DevTools is open)" is checked, in the Settings of the debugger panel.

如果您使用 Google Chrome 调试器,它是相同的,您可以转到“网络”部分并确保在调试器面板的“设置”中选中“禁用缓存(DevTools 打开时)”。

Also, when you link the JavaScript file, use

此外,当您链接 JavaScript 文件时,请使用

<script src="my-js-file.js?v=1"></script>

or v=2, and so forth, when you definitely want to refresh the file. Or you can go to the console and do a Date.now()and get a timestamp, such as 1491313943549, and use

v=2,等等,当您确定要刷新文件时。或者,您可以转到控制台并执行 aDate.now()并获取时间戳,例如1491313943549,然后使用

<script src="my-js-file.js?t=1491313943549"></script>

Some building tools will do that automatically for you, or can be configured to do that, making it something like:

一些构建工具会自动为您完成,或者可以配置为这样做,使其类似于:

<script src="main.742a4952.js"></script>

which essentially will bust the cache.

这基本上会破坏缓存。

Notethat when you use the v=2or t=1491313943549, or main.742a4952.js, you also have the advantage that for your users, they definitely will get the newer version as well.

请注意,当您使用v=2or t=1491313943549, or 时main.742a4952.js,您还有一个优势,即对于您的用户来说,他们肯定也会获得更新的版本。

回答by nsdel

How about adding a '?2' to the tag?

在标签中添加一个 '?2' 怎么样?

<script src="a.js?2"></script>

<script src="a.js?2"></script>

The server should return the same file with or without the '?2', but the browser should see it as a different file and redownload. You can just change this query string whenever the file is changed.

服务器应该返回带有或不带有“?2”的相同文件,但浏览器应该将其视为不同的文件并重新下载。只要文件更改,您就可以更改此查询字符串。

adapted from: http://blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/

改编自:http: //blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/

回答by Anthony

I've had this problem before, it's very frustrating but I found a work around. Type in the full address of the js file (i.e. yourhost.com/javascript.js) and load it. You will probably see the old version load. Then hit f5 to refresh that page and you should see the new version load. The js file will now be updated in your cache and the code should run as you expect.

我以前遇到过这个问题,这很令人沮丧,但我找到了解决方法。输入js文件的完整地址(即yourhost.com/javascript.js)并加载它。您可能会看到旧版本加载。然后按 f5 刷新该页面,您应该会看到新版本加载。js 文件现在将在您的缓存中更新,并且代码应该按您的预期运行。

回答by Richard Culmone

The solution I use is. Using firefox
1. using web developer --> Web Console
2. open the java-script file in new tab.
3. Refresh the new tab you should see your new code.
4. Refresh the original page
5. You should see your changes.

我使用的解决方案是。使用 firefox
1. 使用 web developer --> Web Console
2. 在新选项卡中打开 java-script 文件。
3. 刷新新选项卡,您应该会看到新代码。
4. 刷新原始页面
5. 您应该会看到您的更改。

回答by Prashanth Subramanian

Rename your js file to something else temporarily. This is the only thing that worked for me.

暂时将您的 js 文件重命名为其他名称。这是唯一对我有用的东西。

回答by Felipe Ferrari ferrari212

I had this problem and solved in Chrome by just disabling Cache: - Click F12; - Go at Network tab; - Click on "Disable Cache".

我遇到了这个问题,并通过禁用缓存在 Chrome 中解决了这个问题: - 单击 F12;- 进入网络选项卡;- 单击“禁用缓存”。

回答by Felipe Ferrari ferrari212

A little late to the party, but if you put this in your html, it will keep your website from updating the cache. It takes the website a little longer to load, but for debugging purposes i like it. Taken from this answer: How to programmatically empty browser cache?

聚会有点晚了,但如果你把它放在你的 html 中,它会阻止你的网站更新缓存。加载网站需要更长的时间,但出于调试目的,我喜欢它。取自这个答案:如何以编程方式清空浏览器缓存?

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

回答by xyzzy.rad

The best way around browsercaches is to append a random number to the path of the js file.

绕过浏览器缓存的最佳方法是在 js 文件的路径中附加一个随机数。

Example in pseudo code:

伪代码示例:

// generate a random number
int i = Random.Next();
echo "<script src='a.js?'" + i + "></script>";

This will make sure your browser always reloads the file, because it thinks it's a different file because of the random number in the url.

这将确保您的浏览器始终重新加载文件,因为它认为这是一个不同的文件,因为 url 中的随机数。

The server will always return the file and ignore what comes after the '?'.

服务器将始终返回文件并忽略 '?' 后面的内容。

回答by Mark Khor

I have the same problem for awhile, and manage to figure out... And my case was because I have 2 javascript with the same function name.

我有一段时间有同样的问题,并设法弄清楚......我的情况是因为我有 2 个具有相同函数名称的 javascript。

回答by Mark Khor

Are you 100% sure your browser is even loading the script? Go to your page in Firefox and use the console in Firebug to check if the script has been loaded or not.

您是否 100% 确定您的浏览器甚至正在加载脚本?在 Firefox 中转到您的页面并使用 Firebug 中的控制台检查脚本是否已加载。