Aerozeek
Aerozeek

Reputation: 505

What does the "?" sign mean in a request for a static JS file?

I've seen that a lot and I just don't know what it means. This, for example:

<script src="http://server.com/file.js?y=2345678" type="text/javascript"></script>

If it is in deed possible to 'catch' the value of 'y' in the javascript file, how would that be?

Thank you.

PS. I know what mod_rewrite is and that is not the answer, just in case :)

Upvotes: 4

Views: 107

Answers (3)

Chandra Sekhar Walajapet
Chandra Sekhar Walajapet

Reputation: 2554

ok the way i see it in two ways.

  1. it can be used to load js without caching
  2. for every request to the server, the server might log information(if logging is enabled), if i am using it for analytics i can therefore use a different parameter for locations and from the log i can analyse and get required details.

Upvotes: 1

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91912

This is often used to facilitate caching of the JS file. You set a far-future Expires header which means the browser may cache it for a very long time. If you change something in the file you also update the number in the querystring, which will make the browser refetch the file. This works because caching is for unique filenames and the querystring is part of the filename (as far as the browser is concerned).

A similar approach to this is to use rewrite rules in the web server to have some part of the file name which it doesnät care about. Here's a Nginx rule to show what I mean:

rewrite ^/style\..*\.css$ /style.css;

I use this rule to have filenames like style.42750cad6.css, which always points to the file style.css. The text in the middle is changed whenever I change style.css. The difference between the first approach is that this does not use the querystring so the caching will work in more browsers.

Upvotes: 2

3on
3on

Reputation: 6339

This is to force the browser not to cache the file, by making it believe that it is a dynamic file with get parameter rather than a static one.

Upvotes: 5

Related Questions