I'm programmatically adding an <embed>
tag to my HTML document:
var e = document.createElement('embed');
e.src = "some-image.svg";
e.type = "image/svg+xml";
document.body.appendChild(e);
This works fine, and the SVG element displays as expected. However, I want to manipulate the SVG elements with JavaScript, and attempting to do so immediately after adding the element to the DOM fails, as the content hasn't loaded yet.
How can I do this. Note that I want to do this without jQuery so please don't point to the jQuery API.
As @RGraham points out in a comment, the
<embed>
element raises theload
event when its content is ready.So my code becomes:
This is better than polling as there is no flicker. The SVG is modified immediately upon load, before it is painted.