Home > Web Front-end > JS Tutorial > 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

青灯夜游
Release: 2018-09-08 17:50:29
Original
8200 people have browsed it

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();
Copy after login

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"/>
Copy after login

3.ajax

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

cache:false
Copy after login

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");
  }
Copy after login

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>
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template