JS Method for clearing IE browser cache _ javascript skills

Source: Internet
Author: User
Tags clear browser cache
This article describes in detail several methods for automatically clearing ie cache in js. You can choose the method that suits your needs. I hope it will help you learn about js. How to automatically clear ie cache in js-Common

For dynamic files, such as index. asp? Id =... or index. aspx? I believe experienced programmers know how to prohibit the browser from caching data.
But for static files (css, jpg, gif, and so on), where do we need to disable browsers from caching them?

Method 1:In Dojo, we can do this in a simple way: in dojo. xhrGet (including post) and other methods all contain the preventCache attribute. The meaning of this attribute is: "browser cache is enabled by default, otherwise, different parameters will be automatically added to ensure that the browser cache is invalid. "You only need to assign this attribute to" true.

Method 2:Document. write ("
Among them, 113 of ver = 113 is the version number, which is generally generated using CVS or other tools.
In this way, static files are cached when the cache is applied. When the version is updated, the latest version is obtained and the cache is updated.
The cache can be effectively used and updated for images.

Js clear browser cache 2

To reduce the network transmission pressure between the browser and the server, cache is usually performed on static files, such as js, css, and modified images, that is, add the Expires and Cache-Control parameters to the HTTP Response Headers of these files, and specify the Cache time, in this way, the browser will not send any HTTP Request (except force refresh) to the server within a certain period of time, even if the server's js, css, or image files have been updated multiple times during this period, however, the browser data is still the old data that can be cached at the beginning. Is there a way for the browser to obtain the latest data that has been modified?

Yes, the method is to use ajax to request the latest file on the server, and add the request header If-Modified-Since and Cache-Control, as follows:

The Code is as follows:


$. Ajax ({
Type: "GET ",
Url: "static/cache. js? 1.1.15 ",
DataType: "text ",
BeforeSend: function (xmlHttp ){
XmlHttp. setRequestHeader ("If-Modified-Since", "0 ");
XmlHttp. setRequestHeader ("Cache-Control", "no-cache ");

}
});


Jquery is used here.

In this way, the browser will replace the latest file with the old local file.

Of course, another problem here is that js must know that the server has updated the js, css, and image, which can be solved by using cookies and time versions.

Jquery has ifModified and cache parameters SINCE 1.2. You do not need to add the header yourself.

IfModified Boolean Default: false
Allow the request to be successful only if the response has changed since the last request. This is done by checking the Last-Modified header. Default value is false, ignoring the header.
Cache Boolean Default: true
Added in jQuery 1.2, if set to false it will force the pages that you request to not be cached by the browser.

The Code is as follows:


$. Ajax ({
Type: "GET ",
Url: "static/cache. js? 1.1.15 ",
DataType: "text ",
Cache: false,
IfModified: true
});

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.