accessing the CSS in browser using question mark (?) in end

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 02:55:22

问题


can someone explain what is the difference in accessing CSS in browser by putting question mark ? in the end and why the new CSS is not making any affects on Website.

I have deployed a new CSS on web server but its not making any affect.

I tried to open the URL in browser as below:

www.mysite.com/styles/css/main.css

and it loads the older version of CSS.

Then I tried it as below and it loads the new version of CSS.

www.mysite.com/styles/css/main.css?

After doing all this. New CSS change does not affecting the website. Its still displaying the old design.

Kind Regards


回答1:


You need to add something after the ? then change it when you change the CSS. What is happening is a browser will cache anything that doesn't change for a specific period, it does that by checking file names. so main.css? is still main.css? Anything after the question mark is a query string, generally it's used to pass data to a particular file. In this case it's just used to change the file string so the browser will update it every time it changes without affecting the file itself.

There are a couple of ways you can handle this, the first is manually changing the version, probably the easiest idea if you have a single header file, as in a template system that always loads the same head data.

<link rel="stylesheet" type="text/css" href="assets/css/main.css?ver1/>

Then on next change:

<link rel="stylesheet" type="text/css" href="assets/css/main.css?ver2/>

If you'd rather do it automatically you can add a bit of PHP script to the css line like this:

 <link rel="stylesheet" type="text/css" href="assets/css/main.css?time=<?php echo filemtime('./assets/css/main.css');?>" />

This is essentially adding a value that changes every time you save the file and results in something like this, the next time I save the file that time= value will change:

<link rel="stylesheet" type="text/css" href="http://localhost/refficient/trunk/assets/css/main.css?time=1350305706" />



回答2:


browser cache is the reason,Adding ? after css is not recommended.Open your hosting space and clear cache and thread pool as well.



来源:https://stackoverflow.com/questions/12935808/accessing-the-css-in-browser-using-question-mark-in-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!