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='stylesheet' type='text/css' href='/css/layout.css?v="+new Date().getTime()+"'>"); </script>
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() %>">
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!