views:

73

answers:

4

While browsing the code of some websites I sometimes notice that some JavaScript files are included like this:

  <script type="text/javascript" src="javascripts/jquery.js?1252521516"></script>

But I don't see what the jquery.js ?1252521516 part of the src parameter does. Can anyone explain this to me please?

+2  A: 

It's known as a cache breaker. It stops the browser using an old version of your javascript due to caching rules.

robertc
+1  A: 

The purpose of the query string is to prevent browsers from caching an old version of the file. Whenever the file is updated, the number in the query string is changed to force the browser to download the new version instead of using the one in its cache.

Ayman Hourieh
+1  A: 

Probably some way to avoid caching (the 1252521516 part is generated each time the page is loaded), but I don't see why this is used for jquery

RC
They either updated to a new version of the library or their build script just appends it to every file since it was coded badly.
epascarello
+4  A: 

It allows websites to force browsers to update their cached version of a script or other resource.

For example, when the Javascript code on that website changes, the number at the end would be changed. The browser would see as a totally new file, meaning it would not use whatever outdated, cached version it had previously downloaded. This gives you all the benefits of caching with the ability to bypass the cached version at any time.

William Brendel
Ahh that's brilliant! Thank you, learn something new everyday!
James Brooks
I agree, its a normal "trick" to force the client browser to request the file again for every pagevisit. But that should only be used in development, as I believe it generates unneccessary webtraffic on a production server.
BerggreenDK
Note: you can point src="any-script-file.extension" too, so src="dynjs.php" or "dynjs.asp" is valid too
BerggreenDK
@BerggreenDK: In the scenario I mentioned, bandwidth will actually be saved. The random number in the query string can stay the same for months, which means the browser will happily go on using the cached version of the file. This results in zero traffic to the server for that resource. Then one day, the file is updated and the random number changes, so the browser requests (and caches) a new copy of the file. Your scenario is an equally valid use of this technique. If a developer is doing this, it is presumably for a very good reason (stale caches can be a real pain with real-time data).
William Brendel
Well my original thought after finding out what it is, was to add some PHP to generate a random number at the end, but web-traffic is a problem.
James Brooks
If using PHP, you can get an easy new number by appending the `filemtime()` of the external JS file.
alex