Why querystrings after images & css files?

2019-04-11 18:40发布

问题:

I have seen on various sites a querystring followed by a numbers for images and css files. When I look at the source code (via Chrome Developer), the cached css files and images do not have the number in the query string in their names. I have also seen on sites where the number changes in the querystrings when I refresh the page.

As example:

myimage.jpg?num=12345

myStyles.css?num=82943

After refresh:

myimage.jpg?num=67948

myStyles.css?num=62972

Can anyone explain to me what could possibly be the purpose of these querystrings short of tracking?

回答1:

Often times developers use those query strings with random numbers (or version numbers) to force the browser to request a fresh copy and avoid caching of those files since the request is different each time.

So if you have a file /image.png but it is a generated image, like perhaps a captcha or something, you could follow it with a random number querystring /image.png?399532 which the browser would then not pull image.png from its cache, but instead will download a fresh copy from the server.



回答2:

  1. Prevent caching (the query string can provide a unique URL each time the file is updated causing the browser to download a new copy and not load a stale one from its cache)
  2. Versioning (similar to #1 but with a more specific purpose)


回答3:

The query string is for version controling it force to the navigator to reload the css and the image instead of use the cache