Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created Aug 29, 2011
Embed
What would you like to do?
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