How can I make the browser see CSS and Javascript changes?

后端 未结 5 1172
迷失自我
迷失自我 2020-12-09 01:12

CSS and Javascript files don\'t change very often, so I want them to be cached by the web browser. But I also want the web browser to see changes made to these files without

相关标签:
5条回答
  • 2020-12-09 01:48

    In my opinion, it is better to make the version number part of the file itself e.g. myscript.1.2.3.js. You can set your webserver to cache this file forever, and just add a new js file when you have a new version.

    0 讨论(0)
  • 2020-12-09 01:53

    Some solutions I have seen involve adding a version number to the end of the file in the form of a query string.

    <script type="text/javascript" src="funkycode.js?v1">
    

    You could use the SVN revision number to automate this for you by including the word LastChangedRevision in your html file after where v1 appears above. You must also setup your repository to do this.

    I hope this further clarifies my answer?

    Firefox also allows pressing CTRL + R to reload everything on a particular page.

    0 讨论(0)
  • 2020-12-09 01:59

    I found that if you append the last modified timestamp of the file onto the end of the URL the browser will request the files when it is modified. For example in PHP:

    function urlmtime($url) {
       $parsed_url = parse_url($url);
       $path = $parsed_url['path'];
    
       if ($path[0] == "/") {
           $filename = $_SERVER['DOCUMENT_ROOT'] . "/" . $path;
       } else {
           $filename = $path;
       }
    
       if (!file_exists($filename)) {
           // If not a file then use the current time
           $lastModified = date('YmdHis');
       } else {
           $lastModified = date('YmdHis', filemtime($filename));
       }
    
       if (strpos($url, '?') === false) {
           $url .= '?ts=' . $lastModified;
       } else {
           $url .= '&ts=' . $lastModified;
       }
    
       return $url;
    }
    
    function include_css($css_url, $media='all') {
       // According to Yahoo, using link allows for progressive 
       // rendering in IE where as @import url($css_url) does not
       echo '<link rel="stylesheet" type="text/css" media="' .
            $media . '" href="' . urlmtime($css_url) . '">'."\n";
    }
    
    function include_javascript($javascript_url) {
       echo '<script type="text/javascript" src="' . urlmtime($javascript_url) .
            '"></script>'."\n";
    }
    
    0 讨论(0)
  • 2020-12-09 02:00

    When you release a new version of your CSS or JS libraries, cause the following to occur:

    1. modify the filename to include a unique version string
    2. modify the HTML files which reference the library to point at the versioned file

    (this is usually a pretty simple matter for a release script)

    Now you can set the Expires for the CSS/JS to be years in the future. Whenever you change the content, if the referencing HTML points to a new URI, browsers will no longer use the old cached copy.

    This causes the caching behavior you want without requiring anything of the user.

    0 讨论(0)
  • 2020-12-09 02:05

    I was also wondering how to do this, when I found grom's answer. Thanks for the code.

    I struggled with understanding how the code was supposed to be used. (I don't use a version control system.) In summary, you include the timestamp (ts) when you call the stylesheet. You're not planning on changing the stylesheet often:

    <?php 
        include ('grom_file.php');
        // timestamp on the filename has to be updated manually
        include_css('_stylesheets/style.css?ts=20080912162813', 'all');
    ?>
    
    0 讨论(0)
提交回复
热议问题