5

I saw in some HP code that there is a question mark and a number after javascript file name and it seems like a date or something. What does “?” and a number behind after javascript is for? Example:

<script type="text/javascript" src="/folder/js/folderPageNextSubmit.js?20140801"></script>
Johan
  • 74,508
  • 24
  • 191
  • 319
CDMH
  • 51
  • 5

6 Answers6

8

it is for cache breaking, for example:

 file.js?1234

 file.js?12345

the browser treats them as two different files, so even if the first one was cached so the second one will be fetched anyway.

you change the number after you do a change in the file code and deploy it, so for the users to see your new changes and load the new file, you change the number.

Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
  • Using query strings can prevent some browsers from caching the files at all. Adding needless overhead. Renaming the file is better. Not 100% sure of the scope of that still, and if it can be fixed at the server level, but worth looking into. – Leeish Jun 21 '15 at 05:34
  • or if you go with html5 you can use the application cache and automatically generate the manifest and add a comment based on the hash values of the files, a change even in the comments will force a reload, so you don't have to bother with it any more. – maraca Jun 21 '15 at 05:35
  • http://webmasters.stackexchange.com/questions/52948/url-with-query-disables-caching – Leeish Jun 21 '15 at 05:37
  • Per that above link, it seems important to use strings long enough to avoid cache collisions. – Leeish Jun 21 '15 at 05:39
  • yes there several ways like: file-1234.js and file.js?1234 but I think this is the simplest and fastest way. – Tzook Bar Noy Jun 21 '15 at 06:30
6

The "?" in JavaScript or CSS files is a way to add versioning to the file.

Is useful to avoid browser caching when you are working in dev environments because when the versioning number changes, the file URL also changes. So seems to be a different file for the browser.

Is a common practice use a timestamp as versioning number to get something unique but you can use any numbers or chars.

ianaya89
  • 4,153
  • 3
  • 26
  • 34
4

People typically use that to force users to update the browser cache. If I have a file on my page init.js and you visit my site, and then I update it, and you visit again, your browser will probably not download my updated file, but just use what's in it's cache.

Adding a random string or date will force the browser to update it's cache because it thinks the file is different.

The downside is I believe it will never cache it at all. Some browsers will download the file everytime if you use a URL paremeter vs doing something like init.1234.js so using the URL param method is probably not the best idea.

EDIT: Additionally it's better to set the expires header on your server if you need to update some crucial files, but I won't say I've never used this method, just not the best idea. I believe adding the date in the file itself is better.

Leeish
  • 5,203
  • 2
  • 17
  • 45
  • http://stackoverflow.com/questions/14744149/aggressive-caching-do-all-browsers-support-url-parameter-for-updating – Leeish Jun 21 '15 at 05:31
  • FYI, this doesn't look like a random string. It looks like it's probably a date code for 01/08/2014. Browser GET requests with URL query parameters can be cached if caching isn't disabled by the server response. So, just because there's a query parameter doesn't mean it won't be cached. – jfriend00 Jun 21 '15 at 06:10
1

In the web request? That anti-pattern is used so browsers are forced to never cache a file (or rather, always cache a different copy) because it always has a different URL.

Better programmers simply use the server/response cache profile setting and actually allow caching if files don't change.

Blindy
  • 65,249
  • 10
  • 91
  • 131
1

It is a common strategy to force reloading a file instead of using the copy in the cache. Appending a timestamp this way forces the reload.

Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57
1

Generically, the ? in any URL signifies the end of the path and the beginning of query parameters.

So, in your script tag:

<script type="text/javascript" src="/folder/js/folderPageNextSubmit.js?20140801"></script>

The path to the file is "/folder/js/folderPageNextSubmit.js" and a query parameter ?20140801 has been added to the end of the URL.

It is totally up to the server whether it does anything with this specific query parameter or not. For example, the 20140801 could be a date code for 1/08/2014 that means the server will serve a specific version of that file that matches that date code.

Or, it could be that the server doesn't do anything with the query parameter and it is purely used to break browser or proxy caching of this JS file in prior URLs that used a different date code so that a newer version will be served rather than an older cached version.

Without knowing the specific use and how the server is implemented, one can't really tell which case it is.

jfriend00
  • 683,504
  • 96
  • 985
  • 979