getComputedStyle for IE


/ Published in: JavaScript
Save to your folder(s)

fix for IE, adds getComputedStyle method for the object window and getPropertyValue method for the object, which returns getComputedStyle


Copy this code and paste it in your HTML
  1. if (!window.getComputedStyle) {
  2. window.getComputedStyle = function(el, pseudo) {
  3. this.el = el;
  4. this.getPropertyValue = function(prop) {
  5. var re = /(\-([a-z]){1})/g;
  6. if (prop == 'float') prop = 'styleFloat';
  7. if (re.test(prop)) {
  8. prop = prop.replace(re, function () {
  9. return arguments[2].toUpperCase();
  10. });
  11. }
  12. return el.currentStyle[prop] ? el.currentStyle[prop] : null;
  13. }
  14. return this;
  15. }
  16. }
  17.  
  18. EXAMPLE
  19.  
  20. window.onload = function() {
  21. var compStyle = window.getComputedStyle(document.getElementById('test'), "");
  22.  
  23. alert(compStyle.getPropertyValue("color"));
  24. alert(compStyle.getPropertyValue("float"));
  25. alert(compStyle.getPropertyValue("background-color"));
  26. }

Report this snippet


Comments


You need to login to view comments.