Reference JS and CSS in HTML and set the version number

Source: Internet
Author: User
Background

Search for keywords in a search engine. Htaccess CacheYou can find a lot of tutorials on setting website File Cache. By setting, you can cache infrequently updated files such as CSS and JS on the browser, in this way, every time a visitor accesses your website, the browser can obtain CSS and JS from the browser's cache without reading from your server, in this way, the website opening speed is accelerated to a certain extent, and the traffic on your servers can be saved.

Problem

Now the question is ,. the CSS and JS cache set by htaccess have an expiration time. If CSS and JS are cached in the visitor's browser, before these CSS and JS caches expire, the browser only reads CSS and JS from the cache. If you modify CSS and JS on the server, these changes will not change in the browser of regular customers, unless you press Ctrl + F5 to refresh your website page or manually clear the browser cache. There are tens of thousands of visitors to a website, and there will also be many regular visitors. You cannot refresh the cache for every visitor after updating CSS. What do you do with this problem?

Method 1

Change the CSS file name: in fact, it is very easy to solve this problem. The cache uses the file name to mark the cached content. After you update the CSS file content of the website, you can change the CSS file name. For example, the original css call statement in HTML is as follows:

1 <Linkrel = "stylesheet" href = "style.css" type = "text/CSS" Media = "screen"/>

You can change the CSS file name:

1 <Linkrel = "stylesheet" href = "index.css" type = "text/CSS" Media = "screen"/>

Another way to change the CSS file name is to write the version number to the file name, for example:

1 <Linkrel = "stylesheet" href = "index.v2011.css" type = "text/CSS" Media = "screen"/>

After the CSS file is updated, change the version number in the file name:

1 <Linkrel = "stylesheet" href = "index.v2012.css" type = "text/CSS" Media = "screen"/>
Method 2

Adding a version number to the CSS file: in fact, it is a bit difficult to modify the CSS file name after each CSS file modification, so we can add a version number (that is, in the CSS link? . For example, the original css call statement in HTML is as follows:

1 <Linkrel = "stylesheet" href = "style.css? V = 2011 "type =" text/CSS "Media =" screen "/>

Change the version number of the CSS file to 2012:

1 <Linkrel = "stylesheet" href = "style.css? V = 2012 "type =" text/CSS "Media =" screen "/>

Note that some proxy cache servers do not include "? ", So method 2 may cause your original cache function to fail. You can use the first method instead.

Summary

In fact, the question mark after the CSS file does not actually play a role. It can only be used as a suffix. If the question mark is used to add parameters, you can add version numbers and other information, and refresh the browser cache. A small detail can bring us great convenience.

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.