js set the browser not to cache

About browser cache

Browser cache, sometimes we need him, because he can improve website performance and browser speed, and improve website performance. But sometimes we have to clear the cache, because the cache may be wrong and some wrong data may appear. Such websites, such as real-time updates of stock websites, do not need to be cached. For example, some websites are rarely updated, so it is better to have a cache. Today we mainly introduce several methods of clearing the cache.
Several ways to clean up the website cache
meta method

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

Clear the temporary cache of the form form

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

In fact, the cache of the form form is still helpful for our writing. Generally, it is not recommended to clean it up, but sometimes it needs to be cleaned up for security issues, etc.!
jquery ajax clear browser cache

Method 1: Use ajax to request the latest file from the server, and add the request header 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){
             //操作
         }
         async:false
      });

Method 2, directly use cache: false,

     

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

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

URL 参数后加上 "?ran=" + Math.random(); //当然这里参数 ran可以任意取了

Method 4: Use random time, the same as random numbers.

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

clean up with php backend

add on server side

header("Cache-Control: no-cache, must-revalidate");

 These methods can control the browser not to cache data.

 

Guess you like

Origin blog.csdn.net/wepe12/article/details/90229896