Trying to Understand How jQuery calculates computed properties for IE8 using currentStyle

109 Views Asked by At

I'm trying to understand how jQuery arrives at pixel values for IE8 when dealing with non-pixel based property values, such as margin-top: 2em, or even something like height: auto. For IE9+, getComputedStyle() can obviously provide this easily, but in the case of IE8, currentStyle does not. I am trying to arrive at a solution so I can calculate the total height of an element, including CSS height, padding, border, and margin for all browsers IE8+. I have come across the following answer, but I can't understand what is going on in the accepted answer.

Cross-browser (IE8-) getComputedStyle with Javascript?

I was wondering if anyone could explain what is going on in this code?

1

There are 1 best solutions below

1
On

here is a shim for computed style from WebPlatform.

if (!window.wpo) { window.wpo = {}; }
if (!wpo.utils) { wpo.utils = {}; }

wpo.utils.getComputedStyle = function(_elem, _style)
{// wpo getComputedStyle shim.
   var computedStyle;
   if (typeof _elem.currentStyle != 'undefined')
     { computedStyle = _elem.currentStyle; }
   else
     { try{computedStyle = document.defaultView.getComputedStyle(_elem, null);}catch(e){return '';} }

  return computedStyle[_style];
}