No problem, although it might be quite a simplistic explanation, as I'm no HTTP protocol guru
When a web server sends a response back to the browser (whether a whole web page, or a page fragment via AJAX), there are various headers that can be sent as well. One of these is the 'Cache-Control' header.
This header can have various values, one of which is 'no-cache'. This tells the browser that it should not cache the content being sent to it, so that next time it is requested, it will always have to get a new version (as there is no previous version in its cache).
Now, as you've already seen, there is no need to use this if you are OK passing a cachebuster parameter through, as it will have much the same effect because the URL being requested is different every time (see footnote).
There will be times when passing a cachebuster parameter is not desirable, however. For example, if your application server sits behind a caching layer, you wouldn't want the URL to be different every time for idempotent requests, as the cached version would never get used, and so your application server would get a direct hit every time.
Now, I'm not going to say that using cachebusters is wrong, and I'm not going to say that you shouldn't be using them. They do work, and they require no server-side modification.
So, it's not really about 'doing it the right way', but more about doing it the right way for your requirements.
If you don't get 30,000 hits an hour and have no caching layer, then you will probably be fine with the cachebuster, although setting the response header will also work.
I think that it boils down to this: If you know that the cachebuster will have a performance hit on your setup, then don't use it, otherwise use whichever method you prefer
Should you wish to use the response header, you'd need to ask in the forum for whichever server-side technology you are using (e.g. PHP, ASP, JSP, etc) for information on how to set it.
Hope this helps,
Dan
Footnote: Your example above uses random numbers, which are not guaranteed to be unique, so could still cause problems. If you wanted to stick with the client-side method, I'd switch to using the number of milliseconds since the epoch:
Code:
rn = new Date().getTime();
This will always be unique per PC, unless you change the clock settings.
Coedit Limited - Delivering standards compliant, accessible web solutions
Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info: