JS Clear IE Browser cache method _javascript Tips

Source: Internet
Author: User
Tags browser cache clear browser cache
js automatic removal of IE cache Method-Common

For dynamic files, such as index.asp?id= ... Or index.aspx?id= ... It is believed that experienced programmers know how to prevent browsers from caching data.
But for static files (Css,jpg,gif and so on), under what circumstances do we need to prevent browsers from caching them?

method One:In Dojo we can do it in a simple way: include the Preventcache attribute in methods such as dojo.xhrget (including post), meaning: "The default is to enable browser caching, otherwise the browser cache will be invalidated by automatically adding different parameters" We simply assign this property to: "true".

Method Two:document.write ("
The 113 of ver=113 is the version number, typically the development version number generated by CVS or other tools.
This really makes it possible to cache static files when the version is updated, from getting the latest version and updating the cache.
For images to efficiently utilize and update the cache.

JS Clear Browser Cache two

In order to reduce the network transmission between the browser and the server pressure, often on static files, such as js,css, decorated pictures do cache, that is, the HTTP response headers to these files to add expires and Cache-control parameters, and specify the cache time, Such a certain period of time the browser will not send any HTTP requests to the server (in addition to forced refresh), even in this time the server JS or CSS or picture files have been updated many times, but the browser data is still the original cache of the old data, Is there any way for the browser to get the latest data that has been modified?

Yes, the method is to use AJAX to request the latest files of the server, plus the request headers if-modified-since and Cache-control, as follows:
Copy Code code as follows:

$.ajax ({
Type: "Get",
URL: "Static/cache.js",
DataType: "Text",
Beforesend:function (xmlHttp) {
Xmlhttp.setrequestheader ("If-modified-since", "0");
Xmlhttp.setrequestheader ("Cache-control", "No-cache");

}
});

jquery was used here.

The browser then replaces the old files with the latest ones.

Of course, here is also a problem is JS must know the server updated that JS, CSS, pictures, using cookies and time version should be able to solve.

jquery has ifmodified and cache parameters since 1.2 and doesn't need to add headers on its own.

Ifmodified Boolean Default:false
Allow the request to is successful only if the response has changed since the last request. This is doing 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 would force the pages that you request to not be cached by the browser.
Copy Code code as follows:

$.ajax ({
Type: "Get",
URL: "Static/cache.js",
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.