Archive

javascript cache buster url unique query parameter

Browsers use different of caching files, so you want to ensure that you files will not be cached, apart from setting your servers right, here is a little javascript function that will add a random parameter at the end of the url

// Cache buster.
var cacheBuser = function (url) {
	
	// Check if query already in url.  	
	if(url.indexOf('?') != -1) {
		
		// Check for '&' as the last character.
		if(url.substring(0, url.length - 1) === '&'){
			return url + '_r=' + Math.random() * 5;
		}
		return url + '&_r=' + Math.random() * 5;
	} else {
		return url + '?_r=' + Math.random() * 5;
	}
};

Comments: