Home > Web Front-end > HTML Tutorial > HTML pages automatically clean up the cache of js and css files (automatically add version numbers)_HTML/Xhtml_Web page production

HTML pages automatically clean up the cache of js and css files (automatically add version numbers)_HTML/Xhtml_Web page production

韦小宝
Release: 2017-12-16 11:48:38
Original
1652 people have browsed it

This article mainly introduces the HTML page to automatically clean up the cache of js and css files (automatically add version numbers). The editor thinks it is quite good. Now I will share the HTML source code with you and give it a reference. Friends who are interested in HTML, please follow the editor to take a look.

During the development process of web projects, we often refer to css and js files. Cache often occurs after updating the files Problem (the code has been changed, but it has not changed when accessing it on the browser). In this case, we usually use the following two solutions:

1. Manually clear the browser cache

2. Add the version number (such as layout.css?v=1)

Personally think that method 2 is faster, because clearing the browser cache requires waiting for the browser to respond. But it is troublesome to change the version number every time, so you need to find a way to automatically add the version number.

The following are the methods I collected:

Method 1: You can automatically add the version number to the html through js Add version number


 <script type="text/javascript">  
document.write("<link rel=&#39;stylesheet&#39; type=&#39;text/css&#39; href=&#39;/css/layout.css?v="+new Date().getTime()+"&#39;>");   
</script>
Copy after login


Method 2: If it is a jsp page, you can use java code to generate time Poke (If it is a jsp page, you can also use method one, but this method is more convenient)


<link rel="stylesheet" type="text/css" href="/css/layout.css?v=<%=System.currentTimeMillis() %>">
Copy after login


Method 3: Use other methods to add the version number, such as using node.jsautomatic configuration, etc.

ps: Our purpose of clearing the cache is to see page updates in time In this case, when we put the page online (that is, deploy it to the official environment and will not make any changes again), it is recommended to fix the version number, because the cached page is accessed faster, and the fixed version number should be replaced when it needs to be updated.

The above is the entire content of this article. I hope it will be helpful to everyone’s study and I hope you will support me a lot.

Related recommendations:

How to use the span tag in html

Scroll bar style setting of html

Why there are garbled characters in html and the solution

The above is the detailed content of HTML pages automatically clean up the cache of js and css files (automatically add version numbers)_HTML/Xhtml_Web page production. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template