DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Automatically Reload Stylesheets On HTML Document

05.18.2006
| 13853 views |
  • submit to reddit
        Code is from <a href="http://iamrice.org/files/cssupdater.js">here</a> and described in <a href="http://iamrice.org/articles/2006/04/20/live-css-editing-with-textmate-safari-and-firefox">this great article</a> by dc.

function updateStylesheets() {
	var i,a,s;
	a=document.getElementsByTagName('link');
	for(i=0;i<a.length;i++) {
		s=a[i];
		if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {
			var h=s.href.replace(/(&|\\?)forceReload=d /,'');
			s.href=h+(h.indexOf('?')>=0?'&':'?')+'forceReload='+(new Date().valueOf());
		}
	}
}

setInterval(updateStylesheets, 1000);