Home >Web Front-end >HTML Tutorial >HTML implements automatic cleaning of the cache of js and css files

HTML implements automatic cleaning of the cache of js and css files

王林
王林forward
2020-03-02 18:04:523624browse

HTML implements automatic cleaning of the cache of js and css files

The method is as follows:

1. Manually clear the browser cache;

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

Personally, I think 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 we need to find a way to automatically add the version number.

(Recommended tutorial: html tutorial)

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

<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>

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

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

For more programming-related content, please pay attention to the php Chinese websiteIntroduction to Programming Column!

The above is the detailed content of HTML implements automatic cleaning of the cache of js and css files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete