Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created August 29, 2011 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbostock/1177588 to your computer and use it in GitHub Desktop.
Save mbostock/1177588 to your computer and use it in GitHub Desktop.
style.setProperty
<!DOCTYPE html>
<html>
<body>
<pre><script type="text/javascript">
try {
document.createElement("div").style.setProperty("opacity", 0, "");
document.write("CSSStyleDeclaration's setProperty coerces strings. Hooray!\n");
} catch (error) {
document.write("CSSStyleDeclaration's setProperty requires strings. Patching…\n");
var d3_style_prototype = CSSStyleDeclaration.prototype,
d3_style_setProperty = d3_style_prototype.setProperty;
d3_style_prototype.setProperty = function(name, value, priority) {
d3_style_setProperty.call(this, name, value + "", priority);
};
try {
document.createElement("div").style.setProperty("opacity", 0, "");
document.write("CSSStyleDeclaration's setProperty now coerces strings. Hooray!\n");
} catch (error2) {
document.write("CSSStyleDeclaration's setProperty still requires strings. I give up. :(\n");
}
}
</script></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment