I am using javascript to construct an SVG graphic which I then insert into the web page. I would like to assign a style sheet (from an external .css file) to this dynamically created SVG, but the examples I've seen of SVGs with style sheets have the style references outside the SVG element.
Here's my javascript that constructs the SVG object:
LVMGrapher.prototype.getCanvas = function()
{
if (this.canvas==null) {
var wrap = document.getElementById("svgWrap");
this.canvas = document.createElementNS(SVG_NS, "svg");
removeAllChildren(wrap);
wrap.appendChild(this.canvas);
}
return this.canvas;
}
and later
var svg = this.getCanvas();
var svgNS = svg.namespaceURI;
svg.setAttribute("preserveAspectRatio", "xMinYMin meet");
svg.setAttribute("viewBox", "0 0 1000 400");
svg.setAttribute("width", "100%");
I later fill the SVG with graphics and right now I hard-code the colors, but I'd rather just give them classes and let the style sheet control the color scheme. There's no good reason someone should have to dig into my javascript to be able to control the colors.
The exerimental app is at http://www.purplefrog.com/~thoth/exp/lvm-graph/
Bonus points if you reference a tutorial on how to synthesize a style sheet from javascript. But my primary use case is a style sheet from a URL.
<link rel="stylesheet" type="text/css" href="/path/to/stylesheet.css">but presumably you mean more than that.