20

When I saw many sites' source code, parameters were passed to the linking file (CSS/JavaScript).

In the Stack Overflow source, I got

<script type="text/javascript" src="http://sstatic.net/js/master.js?v=55c7eccb8e19"></script> 

Why is master.js?v=55c7eccb8e19 used?

I am sure that JavaScript/CSS files can't get the parameters.

What is the reason?

2
  • A JS script can find it's own script tag in the DOM, then inspect the parameters. I've seen people do this so that they can offer web page "widgets" using a single script tag. And CSS files can embed JS in some browsers, so they too could read the query string if they wanted to. I don't know of any use for that though. Commented Jun 28, 2010 at 10:39
  • The two answers below could probably be merged into the dup question. Flagged... Commented Jul 11, 2013 at 12:55

9 Answers 9

30

It is usually done to prevent caching.

Let's say you deploy version 2 of your new application and you want to cause the clients to refresh their CSS, you could add this extra parameter to indicate that it should re-request it from the server. Of course there are other approaches as well, but this is pretty simple.

Sign up to request clarification or add additional context in comments.

Comments

19

As the others have said, it's probably an attempt to control caching, although I think it's best to do so by changing the actual resource name (foo.v2.js, not foo.js?v=2) rather than a version in the query string. (That doesn't mean you have to rename files, there are better ways of mapping that URL to the underlying file.) This article, though four years old and therefore ancient in the web world, is still a quite useful discussion. In it, the author claims that you don't want to use query strings for versions because:

...According the letter of the HTTP caching specification, user agents should never cache URLs with query strings. While Internet Explorer and Firefox ignore this, Opera and Safari don’t...

That statement may not be quite correct, because what the spec actually says is

...since some applications have traditionally used GETs and HEADs with query URLs (those containing a "?" in the rel_path part) to perform operations with significant side effects, caches MUST NOT treat responses to such URIs as fresh unless the server provides an explicit expiration time...

(That emphasis at the end is mine.) So using a version in the query string may be fine as long as you're also including explicit caching headers. Provided browsers implement the above correctly. And proxies do. You see why I think you're better off with versions in the actual resource locator, rather than query parameters (which [again] doesn't mean you have to constantly rename files; see the article linked above for more). You know browsers, proxies, etc. along the way are going to fetch the updated resource if you change its name, which means you can give the previous "name" a never-ending cache time to maximize the benefit of intermediate caches.

Regarding:

I am sure that Js/CSS files can't get the parameters.

Just because the result coming back is a JavaScript or CSS resource, it doesn't mean that it's a literal file on the server's file system. The server could well be doing processing based on the query string parameters and generating a customized JavaScript or CSS response. There's no reason I can't configure my server to route all .js files to (say) a PHP handler that looks at the query string and returns something customized to match the fields given. Thus, foo.js?v=2 may well be different from foo.js?v=1 if I've set up my server to do so.

Comments

2

That's to avoid the browser from caching the file. The appending version name has no effect on the JavaScript file, but to the browser's caching engine it looks like a unique file now.

For example, if you had scripts.js and the browser visits the page, they download and cache (store) that file to make the next page visit faster. However, if you make a change the browser may not recognize it until the cache has expired. However, scripts.js?v2 now makes the browser force a re-fetch because the "name's changed" (even though it hasn't, just the contents have).

Comments

2

A server-side script generating the CSS or JavaScript code could make use of them, but it is probably just being used to change the URI when the the content of the file changes so that old, cached versions won't cause problems.

Comments

2
<script type="text/javascript"> 
// front end cache bust
var cacheBust = ['js/StrUtil.js', 'js/protos.common.js', 'js/conf.js', 'bootstrap_ECP/js/init.js'];   
for (i=0;i<cacheBust.length;i++){
     var el = document.createElement('script');
     el.src = cacheBust[i]+"?v=" + Math.random();
     document.getElementsByTagName('head')[0].appendChild(el);
}
</script> 

1 Comment

During development / testing of new releases, the cache can be a problem because the browser, the server and even sometimes the 3G telco (if you do mobile deployment) will cache the static content (e.g. JS, CSS, HTML, img). You can overcome this by appending version number, random number or timestamp to the URL e.g: JSP: <script src="js/excel.js?time=<%=new java.util.Date()%>"></script> In case you're running pure HTML (instead of server pages JSP, ASP, PHP) the server won't help you. In browser, links are loaded before the JS runs, therefore you have to remove the links and load them with JS
1

This is to force the browser to re-cache the .js file if there has been any update.

You see, when you update your JS on a site, some browsers may have cached the old version (to improve performace). Sicne you want them to use your new one, you can append something in the query-field of the name, and voíla! The browser re-fetches the file!

This applies to all files sent from the server btw.

Comments

0

Since javascript and css files are cached by the client browser, so we append some numeric values against their names in order to provide the non-cached version of the file

Comments

0

"I am sure that JavaScript /CSS files can't get the parameters"

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {},
        tokens, re = /[?&]?([^=]+)=([^&]*)/g;
    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }
    return params;
}

1 Comment

The file can't get the parameters. A script could. But the HTTP server doesn't pass the parameters to the file in any way. Unless the file is actually a resource (like cgi-bin) in which it could.
0

This is referred to as Cache Busting.

The browser will cache the file, including the querystring. Next time the querystring is updated the browser will be forced to download the new version of the file.

There are various types of cache-busting, for example:

  • Static
  • Date/Time
  • Software Version
  • Hashed-Content

I've wrote an article on cache busting previously which you may find useful:

http://curtistimson.co.uk/front-end-dev/what-is-cache-busting/

4 Comments

Chrome for example will not cache it at all, forcing downloading of it every time - don't do this.
@Cymbals Are you referring to Date/Time caching? This is only useful when you want to force the client to download it everytime. For example psuedo real-time data.
CSS caching in general. With a querystring it downloads every time - not just when the querystring is changed, as stated above. It may have worked before this way, but others on your blog have reported the same thing.
@Cymbals A different querystring is a different URL and therefore a seperate HTTP request which can be cached individually. There are certain CDNs which can exclude Querystring caching, but this is far from default behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.