Preventing Caching


document.write("<img src=\"image.png?" +   Math.random() + "\" />"); 

Using server-side headers, the caching of dynamic content-like images and also HTML pages can be avoided. However, this approach is not bulletproof, since some browsers or proxy servers can ignore these settings. A technique that always works is appending a meaningless query string parameter to the URL, as in the following fashion: Math.random() returns a random number between 0 and 1, for instance 0.1296601696732852. Appending this to an image usually does not change the data sent from the server, but it is a completely new request for the browser. Therefore, the image (or other data) is not cached.




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net