Home  >  Article  >  Web Front-end  >  How to clear browser cache? 4 ways to do it easily with js

How to clear browser cache? 4 ways to do it easily with js

青灯夜游
青灯夜游Original
2018-09-08 17:50:298099browse

Every time we use the browser to access the Internet, some cache files will be saved on the local disk. The cache on these browsers is to save network resources and speed up browsing. The browser stores recently requested documents on the user's disk. When the visitor requests this page again, the browser can display the document from the local disk, so that This will speed up page browsing. But sometimes we have to clear the cache, because the cache may cause problems and some erroneous data may appear.

In this chapter we will bring you 4 js methods on how to clear the cache. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1: What is browser cache?

1. All data can be stored in the server, but this is not efficient. When we access the web page, it will freeze for a while, waste the storage space of the server, and give Pressure on the server

2. Browser caching can improve website performance and browser speed, but for web pages that need to be updated frequently, caching prevents users from displaying updated styles

Two:How to clear the cache through js?

1. Random number

1) The script does not exist, but is dynamically generated by the server, so a version number is included to show the difference. That is, the random number after the path or file name is equivalent to the file, but the browser will think it is a certain version of the file!

2) The client will cache these css or js files, so every time after upgrading the js or css file and changing the version number, the client browser will re-download the new js or css file, refreshing The role of caching

/* 图片的路径+图片名+随机数=图片的版本号更替 */
$("#pic_code").attr('src','/static/img/verify_code.png'+'?temp=' + Math.random());
/* Math.random() 只能在Javascript 下起作用 */<script type="text/javascript" src="core.js?v=20140829"></script>
/*时间*/
?v=new Date().getTime();

2.meta

Add code in the head area of ​​html:

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="content-type" content="no-cache, must-revalidate" />
<meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT"/>

3.ajax

1) Set the attribute cache: false , let ajax not call the browsing cache.

cache:false

2) Use ajax to request the latest file from the server, and add the request headers If-Modified-Since and Cache-Control, as follows:

beforeSend :function(xmlHttp){ 
   xmlHttp.setRequestHeader("If-Modified-Since","0"); 
   xmlHttp.setRequestHeader("Cache-Control","no-cache");
  }

4.replace

replace The principle is to replace the page specified by the replace parameter with the current page

<script language="javascript"> 
             function jump(){ 
                 window.location.replace("b.html"); 
             } 
         </script>


The above is the detailed content of How to clear browser cache? 4 ways to do it easily with js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn