Return URL with JavaScript that Prevents Loading of Cached Pages

If you want to force a page to reload from the server rather than getting a cached copy, one way to do it is to append a query string to the URL.

If you want your web application to generate URLs that have this functionality, the code below can be implemented using JavaScript. Inside a file called BrowserUtility.js, insert this code.

function BrowserUtility()
	this.addNoCacheTimestamp =
			var ts = 'noCacheTS=' + (new Date()).getTime();
			sUrl = sUrl.trim();
			var nQIndex = sUrl.indexOf('?');
			return sUrl+=ts;

var browserUtility = new BrowserUtility();

Once this is loaded, the function can be called like this, where the variable sUrl stores a URL:

sUrl = browserUtility.addNoCacheTimestamp(sUrl);

If the variable sUrl initially contained “”, the returned string would be something like “”.

Another option, if you did not want to use the current time as the query string value, would be to use a random number function instead.

(This is a snippet of code that I didn’t write – it was authored by my friend and colleague Jeff Konicky.)

Loading Facebook Comments ...
Loading Disqus Comments ...