Clear Client Cache [JavaScript]

Source: Internet
Author: User
<SCRIPT src = "common. js"> </SCRIPT>
V1.1 after modification:
<SCRIPT src = "common. js"> </SCRIPT>
<SCRIPT src = "foo. js"> </SCRIPT>
A new Foo. js file is added, and common. JS is modified. The new class is defined in common. JS, and common. JS is used in Foo. js.

In this case, if you have browsed HTML files of version 1.0 before, the browser automatically caches common. js
When he browses the new version, it uses Foo. js of V1.1 and common. js of V1.0, which will cause a script error.

Solution:
Because CSS and JS are loaded using the <SCRIPT src =...> method, it is difficult to use ASP's server to disable caching. It is also difficult to use ajax to disable caching by setting HTTP request headers.

It seems that the random number is a good method.

// Method 1:
Document. Write ( " <SCRIPT src = 'test. js? RND = " + Math. Random () + " '> </S " + " Latency> " )

//Method 2:
VaRJS=Document. createelement ("Script")
JS. SRC="Test. js"+Math. Random ()
Document. Body. appendchild (JS)

However, if a random number is used, the JS file will never be cached and must be loaded from the server again each time, even if no changes are made.
If you visit a foreign website frequently, you can see that they usually use this method to solve the problem:
<SCRIPT src = "test. js? Ver = 113 "> </SCRIPT>
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.
for images to effectively use and update the cache.

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.