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

How see CSS and Javascript changes

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 requiring the user to clear their browser cache. Also want a solution that works well with a version control system such as Subversion.

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

Could use the SVN revision number to automate this for you: ASP . NET Display SVN Revision Number

Can you specify how you include the Revision variable of another file? That is in the HTML file I can include the Revision number in the URL to the CSS or Javascript file.

In the Subversion book it says about Revision: "This keyword describes the last known revision in which this file changed in the repository".

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

To clarify I am looking for solutions that don't require the user to do anything on their part.

5 Answers

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

hi grom, i have tried this method. however, i found out that once i include parameter the browser did not cache the css file. It loads the css file each time I revisit the page without caching it. I detected this by keep on making changes in css file to see whether css file is cached by the browser. I am using chrome. May I know how to make the browser cache the css file and load only when changes in parameter? Thx.

@benmsia, have you looked at what HTTP headers are being returned when requesting the CSS file?

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.

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.

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.

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');
?>

?? the include_css function will append the last modified timestamp on the file onto the end of the url. No version control required

How can I detect if a browser is blocking a popup? - JavaScript

Detect if a browser is blocking a popup

Occasionally, I've come across a webpage that tries to pop open a new window (for user input, or something important), but the popup blocker prevents this from happening. What methods can the calling window use to make sure the new window launched properly? 9 Answers If you use JavaScript to open the popup, you can use something like this: var newWin = window.open(url); if(!newWin || newWin.closed || typeof newWin.closed=='undefined') { //POPUP BLOCKED } Here's an answer for chrome: detect-blocked-popup-in-chrome @ajwaka could you kindly clarify if this answer fails for chrome, or is there a reason the other answer is better for chrome? thanks! @ajwaka at least today this code seems to work on chrome, hence the question. @Crashalot - You're asking me about something I answered this 6 years ago - I sadly no longer recall the situation and browsers have come a long ways in 6 years. I tried…

Read more…

How do I delimit a databound Javascript string parameter in ASP . NET?

Javascript string parameter in ASP . NET

Triple Quotes? How do I delimit a databound Javascript string parameter in ASP . NET? How do I delimit a Javascript data-bound string parameter in an anchor OnClick event? I have an anchor tag in an ASP . NET Repeater control. The OnClick event of the anchor contains a call to a Javascript function. The Javascript function takes a string for its input parameter. The string parameter is populated with a data-bound value from the Repeater. I need the "double quotes" for the Container.DataItem . I need the 'single quotes' for the OnClick . And I still need one more delimiter (triple quotes?) for the input string parameter…

Read more…

ASP . NET Custom Client-Side Validation

Client-Side Validation

I have a custom validation function in JavaScript in a user control on a .Net 2.0 web site which checks to see that the fee paid is not in excess of the fee amount due. I've placed the validator code in the ascx file, and I have also tried using Page.ClientScript.RegisterClientScriptBlock() and in both cases the validation fires, but cannot find the JavaScript function. The output in…

Read more…