Last active
August 29, 2015 13:57
-
-
Save tomhodgins/9793614 to your computer and use it in GitHub Desktop.
This is a function to count the total number of CSS rules being displayed on a page. Internet Explorer versions 6–9 only read the first 4096 CSS rules and disregard anything further [ http://support.microsoft.com/kb/262161 ]. countCSS() displays the number of rules being displayed on your page and totals it for you. Simply link this file into yo…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countCSS() { | |
var results = ''; | |
var log = ''; | |
var total = 0; | |
if (!document.styleSheets) { | |
return; | |
}; | |
log += '\n==========================='; | |
log += '\ncountCSS totals begin here:'; | |
log += '\n===========================\n'; | |
for (var i = 0; i < document.styleSheets.length; i++) { | |
countSheets(document.styleSheets[i]); | |
}; | |
function countSheets(sheet) { | |
var count = 0; | |
if (sheet && sheet.cssRules) { | |
total += sheet.cssRules.length; | |
for (var j = 0, l = sheet.cssRules.length; j < l; j++) { | |
if (!sheet.cssRules[j].selectorText) continue; | |
count += sheet.cssRules[j].selectorText.split(',').length; | |
}; | |
log += '\n' + sheet.cssRules.length + ' rules in ' + (sheet.href ? sheet.href : 'an inline <style> tag'); | |
if (count >= 4096) { | |
restuls += '\n Warning: there are ' + count + ' rules in ' + sheet.href + ' and Internet Explorere 6-9 will ignore the last ' + (count - 4096) + ' rules\n'; | |
}; | |
}; | |
}; | |
log += '\n\nTotal CSS Rules on page: ' + total + '\n'; | |
log += '\n========================='; | |
log += '\ncountCSS totals end here.'; | |
log += '\n========================='; | |
console.log(log); | |
console.log(results); | |
}; | |
countCSS(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment