Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the content of the "embed" tag in HTML

I have an SVG file, and I don't want to paste it inside the HTML file because, well, it gets messy. So after consulting with w3schools, "embed" tag seemed the safest way to include an external SVG file into HTML.

<embed src="path_to_svg" type="image/svg+xml" id='svgsource' />

However, I need to access the svg elements through DOM via javascript in the main html file. Unfortunately neither

document.getElementById('my_svg_id')

nor

document.getElementById('svgsource').contentDocument

works in any browser. Using "object" tag doesn't do the trick either.

like image 538
narengi Avatar asked Dec 13 '11 21:12

narengi


People also ask

What is embedded content in HTML?

Overview. Embedded content is content that imports another resource into the document, or content from another vocabulary that is inserted into the document. This is the same definition as HTML's embedded content. SVG supports embedded content with the use of 'image' and 'foreignObject' elements.

How can we use the embed tag?

The <embed> tag in HTML is used for embedding external applications which are generally multimedia content like audio or video into an HTML document. It is used as a container for embedding plug-ins such as flash animations. This tag is a new tag in HTML 5, and it requires only starting tag.

What is embed type in HTML?

Attribute ValuesThe Internet media type of the embedded content. Look at IANA Media Types for a complete list of standard media types. ❮ HTML <embed> tag.

What is embed src in HTML?

The src attribute on an <embed> tag specifies the URL or path of a resource to be embedded in the current page. The browser uses the URL to locate and load the source file.


2 Answers

Use .getSVGDocument(). This seems to work for <embed>, <object> and <iframe>:

document.getElementById('svgsource').getSVGDocument()

For <object> or <iframe> you can also use .contentDocument:

document.getElementById('svgsource').contentDocument

For IE7 or IE8, I believe you are out of luck.

like image 113
gilly3 Avatar answered Sep 24 '22 09:09

gilly3


Complete about turn!

Turns out you can, see this link: http://xn--dahlstrm-t4a.net/svg/html/get-embedded-svg-document-script.html

(Also, go and up vote some of Erik Dahlström's other answers to give him some points for me hijacking his answer!)

like image 40
Rich Bradshaw Avatar answered Sep 24 '22 09:09

Rich Bradshaw