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

前端 未结 2 1889
眼角桃花
眼角桃花 2021-01-22 05:23

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 affe

相关标签:
2条回答
  • 2021-01-22 05:59

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

    0 讨论(0)
  • 2021-01-22 06:16

    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" />
    
    0 讨论(0)
提交回复
热议问题