2

I am using something simple like this to load my js files:

<script src="js/file.js" type="text/javascript" charset="utf-8"></script>

Its running ok, but when making tests on my localhost its taking the js file from cache I guess. My updates are not loading instantly.

How to reload my updates at the moment?

3
  • "it's taking the js file from cookies" Any evidence to confirm this assumption? It just sounds very weird, almost impossible. Commented Jun 29, 2020 at 9:23
  • 2
    "its taking the js file from cookies" I think you mean "cache", not "cookies". You could set headers on your server to tell the browser not to cache that resource, or, if you're using Chrome for example, open your dev console (F12), go to the Network tab, and check the "Disable cache" checkbox Commented Jun 29, 2020 at 9:24
  • See this. Commented Jun 29, 2020 at 9:24

2 Answers 2

2

It might be taking the .js file from cache. Keep developer console open for not to use cached file. (Press F12 to open developer console)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

the file is not loading from the "cookies". The file get loaded from the relative path of your site. If you are serving in localhost ad example, it will serve the files from http://localhost/js/file.js.

The problem that you are having of not showing the changes is because your js get cached in the browser, this is a default feature of many browser to avoid users download again an already downloaded file.

As a workaround, you can change the

<script src="js/file.js" type="text/javascript" charset="utf-8"></script> 

to something like:

<script src="js/file.js?v=1" type="text/javascript" charset="utf-8"></script> 

This will force the browser to load a pristine new copy of the js in question. Don't forget, at every change on your js, you will have to update the src="js/file.js?v=1" to something like: src="js/file.js?v=2" and so on..

If you are using PHP on you backend, you can use smt like this to generate an always unique link:

<script src='js/file.js?v=<?= time() ?>'></script>

Hope this helps!

4 Comments

its a nice method too, but the other solution seems more efficient in this case. Anyway, this knowledge its interesting too. For sure I will keep it on my mind. Thanks a lot!
The other solution works just for you. When you will deploy your code to live users, they will see the old version. But for developing that works too
hmm yea its true. I didnt think about it. You right. I think I can use this method just when deploy my files, cos' when making tests I can use that solution yea? Cos' staying changing this variable all time when changing codes will make my life hard.
Yeah. That's why I aded the solution number 2: <script src='js/file.js?v=<?= time() ?>'></script> in this way it self generate the variable using the current time, and you do not have to do... Nothing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.