Blynn Blynn -4 years ago 137
jQuery Question

Add random number to .css so it doesn't cache

I'm trying to get the styles.css to not cache as the server is having issues with the css when it caches.

var numBAH = Math.floor(Math.random()*100);

<link href="styles.css+ numBAH +" rel="stylesheet" type="text/css" />

Answer Source

If you have access to a server-side language it would be neater to render the link tag with a query string that is a hash of the entire content of the file. In that way, the cache invalidator ey will change only when the content of the file has actually changed.

After having seen the discussion that has followed, about how you never want to use cache, because it loads too quickly, I want to change my answer. Not to new Date(), but to: fix your page so that loading quickly is a desired result. If you're having specific problems with that, create a question that targets those problems, don't go directly for the lousy workaround.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download