I want to access the DOM of an SVG file (let's call it /img/my_image.svg
). I know two ways to achieve this:
1. Via the object
tag
<object id="my_image" data="/img/my_image.svg" type="image/svg+xml" style="height: 100%">
Image can't be displayed
</object>
JS code:
$('#my_image').on('load', function () {
$(this.contentDocument).find('whatever').doWhatever();
});
2. Via an XMLSerializer
<div id="my_image"></div>
JS code:
$.get("/img/my_image.svg", function (data) {
$('#my_image').html(new XMLSerializer().serializeToString(data.documentElement));
$('#my_image').find('whatever').doWhatever();
});
Questions:
- Is one of these methods obviously better? Or is it a matter of preference?
- In which case should I prefer one or the other?