87

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 something happened 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.

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.

Anyone know the solution to this?

Tarik
  • 1,847
  • 4
  • 21
  • 30
  • 2
    Try using Fiddler to see if you are downloading the file at all and if the content is correct. You might be getting 304 or 401 back. – Jakub Konecki Oct 16 '10 at 23:05
  • In Safari (Sierra), `Disable cache` did the trick for me while `empty cache` did not. – TheTechGuy Jul 19 '17 at 18:58
  • If you rename a file by changing just uppercase to lowercase or the reverse, and your file was already present on the server, the old file will always be reloaded, even by doing CTR-F5 – tomboul Mar 12 '22 at 09:38
  • Double check the name of the function , I updated name in button but not in function and was facing this issue. – Shashank Bodkhe Apr 19 '23 at 07:23

17 Answers17

166

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

On some browser, you can use CTRL F5 to force a refresh (on the PC). On the Mac, it is Cmd Shift R

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

But I want to give a warning here: even if you can hard refresh, how do you know your customers are getting the latest version? So you need to check, rather than just making sure you and your program manager can do a hard refresh and just go home and take the paycheck next month. If you want to do a job that change the world for the better, or leave the world a little bit better than you found it, you need to investigate more to make sure it works for your customers too (or else, sometimes the customer may call tech support, and tech support may read the script of "clear out the cookies and it will work", which is what happens to me sometimes). Some methods down at the bottom of this post can ensure the customers get the latest version.

Update 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":

hard reload button

Update 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.

Also, when you link the JavaScript file, use

<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

<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.

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

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • The CTRL+F5 might have worked. I haven't seen if I fixed it in a while, but I saw your suggestion and tried it. What does CTRL + F5 do that is different? – Tarik Oct 17 '10 at 02:10
  • 2
    CTRL-F5 ignores the cache when refreshing the page. But as far as Chrome goes, even that's sometimes not enough and one is forced to clear the cache in the preferences. – Ivo Wetzel Oct 17 '10 at 10:31
  • 2
    CTRL + F5 worked for me in chrome after repeatedly pressing it like 3 times thanks. – eran otzap Dec 29 '11 at 03:50
  • Hey, this doesn't work for local js files. I need to upload them to the web to see any changes. I can even uncomment the whole thing and it still runs. Terrible. – Dgloria Jul 27 '21 at 09:10
  • 1
    @Dgloria I have done web development for 26 years and this is not the case... you probably somehow have the files packed or cached somewhere... locally or web, shouldn't matter in this case. You can run a simple Node `live-server` if you want too – nonopolarity Aug 05 '21 at 12:41
24

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

<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.

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

nsdel
  • 2,233
  • 1
  • 16
  • 19
  • Love this. Simple and elegant. If you're using webpack, you can use: `npmjs.com/package/html-replace-webpack-plugin` to alter your JS import. – Tovi Newman Jun 15 '21 at 01:51
17

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.

Anthony
  • 2,411
  • 6
  • 26
  • 23
  • 3
    This was the solution for me. Ctrl + F5 DID NOT solve my problem, for some reason. And this is in March 2017!! – PDoria Mar 13 '17 at 12:37
13

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.

Richard Culmone
  • 131
  • 1
  • 2
6

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

5

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?

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
Community
  • 1
  • 1
3

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

2

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

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.

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

xyzzy.rad
  • 31
  • 1
2

In both Firefox and Chrome, that is really annoying, but because of their default settings which can be changed the following way and then they work. I tried in Chrome and Firefox both with same order of steps.

  1. Press F12 (Open Inspector)
  2. Click Network, and then click Disable Cache
  3. Now click Clear icon. In Firefox, it shows as a trash bin icon on left corner, in Chrome it is the second left icon, in between 'stop recording' and 'Filter'.
  4. Now press F5 or refresh the page

They do update the resources with their fresh copy as they re-download them.

khayal
  • 21
  • 5
1

In Asp.netcore we can use asp-append-version taghelper

<script src="~/js/site.js" asp-append-version="true"></script>
0

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.

Mark Khor
  • 396
  • 5
  • 7
0

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.

0

1.Clear browser cache in browser developer tools 2.Under Network tab – select Disable cache option 3.Restarted browser 4.Force reload Js file command+shift+R in mac   Make sure the fresh war is deployed properly on the Server side

Smitha
  • 555
  • 4
  • 7
0

I was going insane trying to get my js files to refresh and I tried everything. Then I did a header check and remembered I was using Cloudflare!

In Cloudflare you can use dev mode to disable proxy.

Wes
  • 301
  • 4
  • 13
0

Don't forget to check any errors in webpack compilation. Sometimes the application.js in app/javascript/packs/ doesn't reload due to webpack compilation error.

Aditya Joardar
  • 580
  • 5
  • 11
0

When I run into this issue I try this sequence of steps:

  1. Hard refresh the page.

  2. Clear cache + cookies.

  3. Add a static version to my script.

    src="my-script-name.js?v=1"

  4. If the above does not help, add a dynamic version to my script:

    src="my-script-name.js?v=" + Date.now() + Math.random()

Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
0

Simple fix, just add the javascript code to index page, when you reload the page will update the code you were updated

mosawi
  • 20
  • 4