Skip navigation.

SVGs on web pages

, , , , , ,

Up until now, I've been using code similar to the following to put SVG images on my web pages:
<!--[if !IE]> <-->
<object type="image/svg+xml" data="foo.svg" width="420" height="360"></object>
<!--> <![endif]-->
<!--[if IE]>
<embed src="foo.svg" width="420" height="360"></embed>
<![endif]-->
The common mood on the 'net at large is that the Adobe SVG Viewer (ASV) (the only way to get IE to display SVGs) is not happy with the standards-compliant object element, but works much better with the embed element. The conditional comments ensure that standards-compliant browsers (including the HTML validator) got the standards-compliant code, while IE and ASV got what they wanted.

The problem with that is that it's clunky and duplicates the src/data, width and height attributes, which makes it easy to mistakenly put different values in the two elements.

A recent project of mine involving a web-enabled microcontroller caused me to revisit the above code. I was running really tight on memory, literally counting bytes to get everything to fit. The above code was a good candidate for trimming a bit of fat.

After some searching and trial-and-error, I found that ASV's problem with the object element was nothing more than the type attribute! The above code reduced to:
<object data="foo.svg" width="420" height="360"></object>
So much shorter, simpler and more maintainable!

This has changed, see below. I tested it to work with IE+ASV, Firefox, Opera and Safari(Windows). It also validates, of course. The only caveat is that the web server must use the correct MIME type ("image/svg+xml"), which it should be doing anyway.

The bytes saved enabled me to polish off the last couple of remaining bugs in my microcontroller project without spending ages scrounging around for fatty code elsewhere - with a whole two bytes to spare! :eek:

:coffee:

Edit: Last night I was reviewing this and found it had stopped working in IE and Firefox, but I was too tired to be bothered investigating. However, this morning it's working again! It never stopping working in Opera. :D

Edit2: I've just found the following that seems to be more reliable:
<object type="image/svg+xml" data="foo.svg" width="420" height="360">
<param name="src" value="foo.svg" />
<a href="foo.svg">Demo SVG</a>
</object>
(The anchor is not required, it's just there as a fallback)

Source of above: http://joliclic.free.fr/html/object-tag/en/object-svg.html

Five Opera WishesUnrecognized USB hubs

Comments

IceArdor 6. August 2007, 02:16

Is <object> a self-closable tag (<object/>)? If it is, that would have saved you a couple more bytes, too.

What I'm wondering, though, is what project is really so byte-intensive that you needed to trim it down just a couple bytes? Were you trying to store this code in a cookie or run it on a machine with 16 MB of RAM?

Andrew Gregory 6. August 2007, 03:43

It's a PIC microcontroller. Program memory is 64KB plus 4KB RAM, into which is currently fitting: Ethernet stack, TCP/IP stack, HTTP server, DHCP client, serial driver, telnet server (including RFC2217 support), GPS (NMEA) support, NTP server, TIME/DAYTIME support, ICMP timestamps. Plus web pages for remote configuration and monitoring of status. I'm using the SVG to display GPS satellite information.

Edit: Forgot, I'm not using self-closing syntax because the web pages are HTML, not XHTML.

IceArdor 7. August 2007, 07:00

amazing... congratulations on body fat trimming. If only the United States would trim a little body fat here and there.

stelt 8. August 2007, 14:04

you can of course always improve the SVG Wiki with information like that.

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

Download Opera, the fastest and most secure browser
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31