Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in using <iframe> and <embed> for displaying SVG and scripting

I'm trying to create Dynamic SVG graphics, it is my understanding that the only way to create dynamic SVG is to use a scripting language, so I have a few questions, basically I'd like to load or embed the SVG to a HTML web page and control the graphics using Inputs in the web page, rather than hardcoding the ECMAscript in the SVG file. I'm not entirely sure if I should use the embed tag or an iframe for displaying the SVG here are my doubts regarding SVG and scripting:

  1. Whats the difference (in terms of scripting) in using an <iframe> or and <embed> tag for accessing the SVG elements?, maybe someone can include simple examples.
  2. Can SVG evaluate math expressions in element attributes(just to be sure)?
like image 239
Tristian Avatar asked Apr 17 '11 06:04

Tristian


2 Answers

Don't use either <iframe> or <embed>. Instead, embed your SVG directly in XHTML like so:
http://phrogz.net/svg/svg_in_xhtml5.xhtml

With that, you have full access to the SVG DOM as part of your document. As shown in that example, you simply need to be certain to create SVG elements (but not attributes) using the SVG namespace. You must also ensure that your web host is sending the content type for xhtml as application/xhtml+xml or text/xml, not text/html.

phrogz$ curl --silent -I http://phrogz.net/svg/svg_in_xhtml5.xhtml | grep "Type"
Content-Type: application/xhtml+xml

For more examples of JavaScript manipulating SVG mixed with HTML, see the various .xhtml files in that same directory. A particularly compelling example is this one, which dynamically creates hundreds of small SVG files as inline elements flowing like text.

And to your question:

Can SVG evaluate math expressions in element attributes(just to be sure)?

Not in general, no. However, the usage of SMIL Animation does allow you to specify various interpolation methods of properties over time.

Finally, note that you don't have to put SVG in HTML to make it dynamic. You can script SVG with JavaScript directly. For example, see this test file (press the green button to start simulation):
http://phrogz.net/svg/SpringzTest.svg

like image 188
Phrogz Avatar answered Sep 29 '22 19:09

Phrogz


Whats the difference (in terms of scripting) in using an or and tag for accessing the SVG elements?, maybe someone can include simple examples.

<iframe>:

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved with window.postMessage.

Source: https://developer.mozilla.org/en/HTML/Element/iframe#Scripting

We access iframe content by iframe_element.contentWindow method:

<html>
  <body>
    <iframe id="SVG_frame" src="image.svg"></iframe>
  </body>
  <script>
    var SVG_frame = document.getElementById ( "SVG_frame" );
    var SVG_content = null;
    function getContent ()
    {
      SVG_content = SVG_frame.contentWindow;
      SVG_content ? alert ( "YAY!" ) : alert ( "BOO!" );
    }
      SVG_frame.onload = getContent;
  </script>
</html>

<embed>:

Example (view source): https://jwatt.org/svg/demos/scripting-across-embed.html

(both methods fail at least in Chromium)

<object>

Example (view source): https://jwatt.org/svg/demos/scripting-across-object.html


Can SVG evaluate math expressions in element attributes(just to be sure)?

like <element attribute="48/2*(9+3)"/>?

I did't find a word about it in SVG spec.


EDIT

Personally, I recommend to use <object> + Data URI Scheme and/or object_element.contentDocument. I've tested both in Chromium and Firefox.

AHA! <object> has similar security behavior to <iframe>: domain, protocol must be same for site and SVG file.


EDIT2

If You are interested how to get markup vector graphics to work in Internet Explorer(s) without plug-in(s), then Vector Markup Language is the way.

like image 36
atlavis Avatar answered Sep 29 '22 19:09

atlavis