6

Every time i refresh a site and view its page source, the javascript src i.e js.js?version=1364903356; the version number always changes.

My question is: What is the meaning of this number; and if i put js.js in every page, the site is not working.

enb081
  • 3,831
  • 11
  • 43
  • 66
  • can you provide a link to that site. the number is a way to force the browsers to download the "js.js" file from server and not use its cached copy. Read this http://www.impressivewebs.com/force-browser-newest-stylesheet/ – vdua Apr 02 '13 at 12:08
  • Also read this question http://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files – vdua Apr 02 '13 at 12:09
  • [link](http://tokiolab.it) – Tridip Thrizu Apr 02 '13 at 12:12
  • 1
    @vdua thanks for the articles, it certainly cleared my doubt. – Tridip Thrizu Apr 02 '13 at 12:16

4 Answers4

8

The version is generally appended for caching purposes, or rather, for invalidating the cache (by changing the version number, and hence, the requested URL), so it's seen as a new resources and downloaded afresh.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
2

The number is probably meaningless. It is almost certainly just being appended to the URL so that the URL changes so the JS won't be fetched from the cache.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Not really meaningless, it looks like a timestamp expressed in seconds since the epoch (multiplying the value by `1000` and passing it to the `Date()` constructor returns `Tue Apr 02 2013 13:49:16 GMT+0200` on my machine). – Frédéric Hamidi Apr 02 '13 at 12:09
  • @FrédéricHamidi — That just explains the logic used to generate it and make it unique, it doesn't make it meaningful. – Quentin Apr 02 '13 at 12:10
  • @Quentin, ah, I guess you can see it like that too :) – Frédéric Hamidi Apr 02 '13 at 12:11
1

it's just for to avoid Caching purposes and request new each time. whenever you visit a same content. if you set static content caching enabled in IIS, then Browser will issue HTTP 304 not modified status to the resource.

you can view in chrome. open developer tools (f12) then go for network tab. you will see in request header like this.

Request Method:GET
Status Code:304 Not Modified

IIS/Any web server wil determine whether the content is changed or the same content. if the content is the same as resides in the cache then it will not iniitate the new request.

by appendign the version number, filename/url/resource will be changed. so browser will issue a new GET request for the resources.

Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
1

This is a common technique used to prevent or manage caching of javascript and other files that the browser would normally cache.

If the version number always changes, then it means that the page in question is preventing your browser from caching the file at all; every request will load a new copy of the file regardless of whether it's changed or not.

This is poor practice, and likely due to a misconfiguration of the site in question.

More commonly, the version number would remain static, but could be triggered to change by the site itself. This would mean that for most requests the browser's caching would be in play, but that the site owner has control over whether to refresh the cache, for example when he updates the script file.

Without this technique, a browser that has already cached the old version of the file might not know that the file has been updated, and may not fetch the updated version. This could result in version conflicts between script files on the page.

There are, in fact, more technically correct ways of doing this that don't involve adding random values to the end of your URLs. The HTTP standard specifies that the browser should query the URL, and tell the site what version it has cached. The site can then respond with a "Not changed" message, and the browser can use the cached version. This ought to mean that the technique used in the question isn't necessary.

However, the technique is necessary in some cases because some browsers and/or web server configurations may not work correctly with the standard method, and the browser may still end up using the cached version incorrectly.

This technique can therefore be seen as a work-around for that.

Spudley
  • 166,037
  • 39
  • 233
  • 307