Jean Hoffmann
Jean Hoffmann

Reputation: 113

How to load script js without using stale file from cache?

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?

Upvotes: 2

Views: 940

Answers (2)

Raffobaffo
Raffobaffo

Reputation: 2856

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!

Upvotes: 1

Shivaji Mutkule
Shivaji Mutkule

Reputation: 1278

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

Upvotes: 2

Related Questions