How to clear cache memory using JavaScript?

Unlike mobile applications, a web browser doesn’t allow to clear its cache memory. Though we cannot clear all cache of the client browser it is still possible to load the webpage without caching by using meta tags in the HTML code.

The only way to do this is by making few changes in the code which says the browser not remember the recently loaded memory which is nothing but the chache memory.

The following are two examples that explain
NOTE: The following codes cannot be run as it is and does not have an output. It has to added to an already existing code to see the Outputs.

Method 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Add this part of HTML code which makes the browser to not record the cache memory.



Method 2:
Appending a parameter to the filename in the script tag. Change it when the file changes.

Example:
Let this be the name of the file. Every time you load this page just change the version of the script.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script src = "filename.js?version = 1.0"></script>

chevron_right


The next time you load this page it should be something like this.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script src = "newfile.js?version = 1.1"></script>

chevron_right


NOTE:

  • A browser is designed in such a way that it saves all the temporary cache.
  • It is so because cache memory is the main reason for the website to load faster.
  • Hence there is no direct way to permanently delete it’s cache memory unless certain codings are changed in your HTML code.
  • There may be few other ways to achieve this, but these two are the easiest and most effective one.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.