Several Methods of Cleaning Browser Cache by js

  • 2021-08-03 09:10:49
  • OfStack

About Browser Caching

Browser cache, sometimes we need him, because he can improve website performance and browser speed, improve website performance. But sometimes we have to clear the cache, because the cache may fail and some wrong data will appear. Real-time updates like stock websites, such websites are not cached, like some websites are rarely updated, and caching is better. Today, we mainly introduce several methods to clear the cache.

Several Methods of Cleaning Website Cache

meta method


// Not caching 
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT="0">

Clean up the temporary cache of form forms

<body onLoad="javascript:document.yourFormName.reset()">

In fact, form form cache is helpful for us to write, 1 general situation is not recommended to clean up, but sometimes for security issues, etc., need to clean up 1!

jquery ajax Clear Browser Cache

Mode 1: Request the server's latest file with ajax, and add the request headers If-Modified-Since and Cache-Control, as follows:


 $.ajax({
   url:'www.haorooms.com',
   dataType:'json',
   data:{},
   beforeSend :function(xmlHttp){ 
    xmlHttp.setRequestHeader("If-Modified-Since","0"); 
    xmlHttp.setRequestHeader("Cache-Control","no-cache");
   },
   success:function(response){
     // Operation 
   }
   async:false
 });

Method 2: cache: false,


 $.ajax({
   url:'www.haorooms.com',
   dataType:'json',
   data:{},
   cache:false, 
   ifModified :true ,
   success:function(response){
     // Operation 
   }
   async:false
 });

Method 3: Using random numbers, random numbers are also a very good way to avoid caching!

Add "?" After URL parameter. ran = "+ Math. random (); //Of course, the parameter ran can be taken at will here

Method 4: Use random time and random number 1.

Add "?" After the URL parameter. timestamp = "+ new Date (). getTime ();

Cleanup with php backend

Add header on the server ("Cache-Control: no-cache, must-revalidate"); And so on (as in php)

Method 5:

window.location.replace("WebForm1.aspx");  

Parameter is the page you want to cover. The principle of replace is to replace the page specified by replace parameter with the current page.

This prevents the user from clicking the back key. The javascript script is used, for example:

a.html

The following is a reference fragment:


<html> 
   <head> 
     <title>a</title>   
     <script language="javascript"> 
       function jump(){ 
         window.location.replace("b.html"); 
       } 
     </script> 
   </head> 
   <body> 
    <a href="javascript:jump()" rel="external nofollow" rel="external nofollow" >b</a> 
  </body> 
</html> 

b.html

The following is a reference fragment:


<html> 
   <head> 
     <title>b</title>   
     <script language="javascript"> 
       function jump(){ 
         window.location.replace("a.html"); 
       } 
     </script> 
   </head> 
   <body> 
    <a href="javascript:jump()" rel="external nofollow" rel="external nofollow" >a</a> 
  </body> 
</html>

Related articles: