How to disable javascript caching for html pages and cache JavaScript For html pages

Source: Internet
Author: User

How to disable javascript caching for html pages and cache JavaScript For html pages

The example in this article describes how to make html pages not cache js. Share it with you for your reference. The specific implementation method is as follows:

Many of our friends will encounter this situation: if Javascript is loaded on our page, this js cache file will be called the next time we open it, but it is very inconvenient for us to debug it, this article will talk about how to solve this problem. Let's take a look at it.

The method of not caching JS is actually quite simple, and CSS can also be used under certain conditions;

Let us first understand a simple principle of no cache:

When browsing different URLs, the browser will automatically cache the current accessed address once, while the second access will call the cached page to achieve rapid page loading (page loading optimization);

Therefore, we can set a different value for the back of the page so that the page remains correct for access to achieve the purpose of not caching!

The following is a simple example:

Copy codeThe Code is as follows: <script>
Document. write ("<script type = 'text/javascript 'src = '/js/test. js? "+ Math. random (); +" '> </script> ");
</Script>
For others, you only need to add + Math. random () after the address ()

Note: Because Math. random () can only work under Javascript, it can only be called through Javascript.

Add an ajax method that does not allow caching.

Copy codeThe Code is as follows: xmlHttp. open ("GET", "ajax. asp? Now = "+ new Date (). getTime (), true );
Remember that now = "+ new Date (). getTime () is the focus and requires parameters.

I hope this article will help you design javascript programs.


Image, css, and js are not required to be loaded every time a page is loaded. How can this problem be obtained from the ie cache? Java Implementation

Javascript cannot control the cache. The cache is determined by the http header.

When the server sends a response, it can tell the client to cache the request in two ways:

The first type is Expires. For example: Expires: Sun, 16 Oct 2016 05:43:02 GMT before this date, the client will regard the cache as valid.

However, Expires has some disadvantages. For example, the time settings on the server and the client may be different, which may make the cache invalid and may not be accurate as expected by the server.

The second type is Cache-Control, for example, Cache-Control: max-age = 3600.

Here we declare a relative number of seconds, indicating that the cache is valid within 3600 seconds from now on, thus avoiding time inconsistency between the server and the client.

However, Cache-Control is only available in HTTP1.1 and does not apply to HTTP1.0. Expires applies to both HTTP1.0 and HTTP1.1, therefore, sending these two headers at the same time is a better choice in most cases. When both headers of the client can be parsed, the Cache-Control will be used first.

Taking PHP as an example, use the header function to send the header:

Header ('cache-Control: max-age = 000000 ');

Note: Unless output buffering is enabled, these commands must be executed before any output is returned.
Enable output buffer: ob_start ()
Reference: hi.baidu.com/..e.html

How does js address page cache?

You can disable caching on html pages, for example, adding tags to html pages.
<META HTTP-EQUIV = "pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-store, must-revalidate">
<META HTTP-EQUIV = "expires" CONTENT = "Wed, 26 Feb 1997 08:21:57 GMT">
<META HTTP-EQUIV = "expires" CONTENT = "0">
And so on.

For details, refer to Baidu.
 

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.