2

I load a SVG file from an object tag and access its content from a JavaScript.

$(document).ready(function() {
    $(window).load(function () {  
      var a = document.getElementById("svgObj");
      var svgDoc = a.contentDocument;
      // Do something on svgDoc
    });
});

This works fine when I explicitly have a tag in a html page. When I dynamically create an object tag in JavaScript, "svgDoc" is null and I can seem to get contentDocument of the loaded object tag. I've tried using the timer to wait until svg file gets loaded but resulted in getting null object. Is it possible to get "contentDocument" by dynamically creating an object tag and loading svg file on the fly?

http://jsfiddle.net/katakuri/05doayb5/

Thanks, katakuri

katakuri
  • 21
  • 4
  • 1
    Add the code you use to create (when do you do that?) the object tag to your question. – Jeroen Heier Oct 26 '17 at 03:59
  • no, if the node is detached, there's no document associated with it. you can instead make a fresh domdocument, or a fragment, and append the node to that. you can also ajax an xml dataURL or whatnot – dandavis Oct 26 '17 at 04:45
  • This is a quick snippet I have for what I am trying to do. I can't get the contentDocument. http://jsfiddle.net/katakuri/05doayb5/ – katakuri Oct 26 '17 at 17:05

1 Answers1

1

The problem is CROSS-DOMAIN,

because the SVG file is in other domain

Get DOM content of cross-domain iframe

zakariamouhid
  • 81
  • 1
  • 4