CSS 强制刷新背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12638821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forcing refresh of a background image
提问by drake035
Is there a way to make sure that a certain background image recently updated does appear in its last version on everybody's browser (rather than the old version stored in cache)?
有没有办法确保最近更新的某个背景图像确实出现在每个人的浏览器上的最新版本中(而不是存储在缓存中的旧版本)?
It has to be a solution not involving touching the line of code calling the CSS (I could put a question mark and a version number after the css file name, like : <link href="styles.css?v=2" rel="stylesheet" type="text/css"/>
, but in this case I don't have access to this part of the code)
它必须是一个不涉及调用 CSS 的代码行的解决方案(我可以在 css 文件名后放一个问号和一个版本号,例如 : <link href="styles.css?v=2" rel="stylesheet" type="text/css"/>
,但在这种情况下我无权访问这部分编码)
回答by Anshu
You can use the following approach:
您可以使用以下方法:
.button {
background: url(../Images/button.png?v=1234);
}
回答by Murali Murugesan
Few things i like to put here
我喜欢放的东西很少
- Images, JS, CSS files referred in HTML pages are cached in browser based on the URL
- Change in URL will issue a new request to server and store the latest in cache.
- HTML 页面中引用的图片、JS、CSS 文件根据 URL 缓存在浏览器中
- 更改 URL 将向服务器发出新请求并将最新请求存储在缓存中。
All you need to do is Change URL .
您需要做的就是更改 URL。
Though you modified css style sheet, you need to update HTML for CSS reference.
虽然您修改了 css 样式表,但您需要更新 HTML 以供 CSS 参考。
background: url(../Images/button.png?v=1234);
Ex:
http://www.site.com/css/style.css
to http://www.site.com/css/style.css?ver=002
例如:
http://www.site.com/css/style.css
到http://www.site.com/css/style.css?ver=002
<link rel="stylesheet" href="css/style.css?ver=002" type="text/css" />
回答by Guffa
You can add another CSS rule after the other CSS, that overrides the rules that loads the image and uses an image with an added version number.
您可以在另一个 CSS 之后添加另一个 CSS 规则,该规则覆盖加载图像的规则并使用具有添加版本号的图像。
CSS rules loaded later have higher priority, so you can use the same selector as in the main CSS, and it will override it.
稍后加载的 CSS 规则具有更高的优先级,因此您可以使用与主 CSS 中相同的选择器,它将覆盖它。
If you can't do that, then you are screwed. You have to change the URL of the image that is loaded to surely get the new image.
如果你做不到,那你就完蛋了。您必须更改加载的图像的 URL 才能确保获得新图像。
回答by SMacFadyen
You could add a Expires
in htaccess
, but If the image is really large then I would advise against this. As you ask without CSS, try:
您可以添加一个Expires
in htaccess
,但是如果图像真的很大,那么我会建议不要这样做。当您在没有 CSS 的情况下询问时,请尝试:
<Files "your_bg.jpg">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</Files>