How do I clear the browser cache? JS 4 ways to easily take care of

Source: Internet
Author: User
Tags browser cache website performance
When we surf the internet every time we use a browser, we save some cache files on the local disk. The caches on these browsers are designed to conserve network resources by speeding up browsing, the browser stores the most recently requested documents on the user's disk, and when the visitor requests the page again, the browser can display the document from the local disk so that the page can be viewed faster. But sometimes we have to clear the cache, because the cache can be bad and there are some wrong data.

In this chapter we will bring you 4 ways to clear the cache. Have a certain reference value, the need for friends can refer to, I hope to help you.

A: What is the browser cache?

1. All data can be stored in the server, but this is not efficient, when we visit the Web page, a time, two will waste the server storage space, three will cause pressure on the server

2. Browser caching can improve website performance and browser speed, but for Web pages that require frequent updates, caching prevents users from displaying the updated style

Second: How to clear the cache through JS?

1. Random number

1) The script does not exist, but the server is dynamically generated, so with a version number, to show the difference. The random number after the path or file name is equivalent to the file, but the browser thinks he is a version of the file!

2) The client will cache these CSS or JS files, so each time you upgrade the JS or CSS files, change the version number, the client browser will re-download the new JS or CSS files, the role of the brush cache

/* Path of picture + image name + random number = version number of the image replacement */$ ("#pic_code"). attr (' src ', '/static/img/verify_code.png ' + '? temp= ' + math.random ());/* Math.random () can only function under JavaScript */<script type= "Text/javascript" src= "core.js?v=20140829" ></script>/* Time */?v=new date (). GetTime ();

2.meta

Add code to the head area in HTML:

<meta http-equiv= "Pragma" content= "No-cache"/><meta http-equiv= "Content-type" content= "No-cache, "/><meta http-equiv=" Expires "content=" Wed, Must-revalidate 1997 08:21:57 GMT "/>

3.ajax

1) Set the property Cache:false so that Ajax does not invoke the browse cache.

Cache:false

2) Use Ajax to request the latest server files, plus 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

The principle of replace replaces the page specified by the Replace parameter with the current page

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.