Front-end development JavaScript method to clear browser cache

Source: Internet
Author: User
Tags browser cache clear browser cache

Ways to view and delete browser caches ===== "open

Recently found in development projects sometimes always have to clear the browser cache frequently, otherwise it always shows the outdated information

Browser caching has pros and cons, some data needs to be cached to make the page open faster to improve site performance, but some cache content must be cleared, the cache may cause some error data to be displayed.

Describes some ways to clear the browser cache:

Method one: Using META tags

 <  meta  http-equiv  = "Pragma"   CONTENT  = "No-cache"  >  <  meta  http-equiv  = "Cache-control"   CONTENT  = "No-cache, Must-revalidate"  >  <  meta  http-equiv  = "Expires"   CONTENT  = "0"  >  

This approach is simple, but not supported by all browsers, such as Chrome, which enforces the use of cached browsers.

Method Two: jquery Ajax clears the browser cache

//use Ajax to request the latest server files, plus the request headers if-modified-since and Cache-contro$.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) {//Do something} async:false  });//directly with Cache:false,$.ajax ({URL:' Www.haorooms.com ', DataType:' JSON ', data:{}, cache:false, ifmodified:true, Success:function(response) {//Do something} async:false  });

Method Three: Add a random number (letter) or the current timestamp after the URL

URL? ran= " + math.random (); URL? timestamp=+ new Date (). GetTime ();

Method Four: Add a date stamp after JS

Although it looks like the URL plus random number, but the principle is different, this best all JS files in the form of public introduction files, can save some trouble

<script src= "/res/js/plugins/metismenu/jquery.metismenu.js?${.now?string (' Yyyy-mm-dd ')}" ></script>

Method Five: Back-end processing

Front-end development JavaScript method to clear browser cache

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.