Skip to content

Instantly share code, notes, and snippets.

@zhuzhuaicoding
Created November 15, 2012 01:44
Show Gist options
  • Select an option

  • Save zhuzhuaicoding/4076125 to your computer and use it in GitHub Desktop.

Select an option

Save zhuzhuaicoding/4076125 to your computer and use it in GitHub Desktop.
getStyle
/*
Example call of the function:
getStyle(document.getElementById("container"), "font-size");
*/
// The regular version
function getStyle(oElm, strCssRule){
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
}
else if(oElm.currentStyle){
strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
return strValue;
}
// The version if you expect any IE 5.0 users whatsoever
function getStyle(oElm, strCssRule){
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
}
else if(oElm.currentStyle){
try{
strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
catch(e){
// Used to prevent an error in IE 5.0
}
}
return strValue;
}
72.47.228.190
@zhuzhuaicoding

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment