I'm trying to get an SVG image to show up on my iPhone (or iPad) default browser, but I can't seem to get even just a rect to show up.
Example at: http://www.invalidpage.com/svg/svgtest.html
Source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/html1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
<title>SVG iPhone Test</title>
</head>
<body>
<div>
<svg width="500" height="220">
<rect x="2" y="2" width="496" height="216" stroke="#000" stroke-width="2px" fill="transparent"></rect>
</svg>
</div>
</body>
</html>
Add
xmlns="http://www.w3.org/2000/svg" version="1.1"
to your svg tag.The HTTP MIME type being delivered by
http://www.invalidpage.com/svg/svgtest.html
is"Content-Type: text/html"
. HTML inline svg works with the MIME type"Content-Type: text/xml"
You can create this by ending the document with XML instead of HTML as they have done here.Not sure if Ipad cares about the
Content-Type
but other browsers do.Updated
Can also be used; It is what is being shown on the Ipad svg examples. When the document is delivering as an XML not HTML, it should start with
<xml version="1.0" standalone="no">
;have you tried to embed it inside an tag.
e.g
I think this should do the trick!
I have had this problem before too with mobile Safari. What you can do is load the SVG into the the DOM via javascript:
That's just an example - obviously you're not going to store your SVG in a string like that in practice.
You could retrieve the SVG string from the server via ajax, and then load into the DOM using the above approach if you wanted.
Even with all the other advice on this page I couldn't get it to work (even in Safari).
So I found a working example at:
http://upload.wikimedia.org/wikipedia/en/3/35/Starbucks_Coffee_Logo.svg
and copied the file to my own site. Still no luck, so I had a look at that file and my own using Rex Swain's HTTP viewer:
http://www.rexswain.com/httpview.html
The difference became obvious. My svg was being served as text/html, and the Starbucks logo was being served as image/svg+xml.
The solution was to change the header by adding:
to the .htaccess file.
Mixing SVG tags with HTML tags without using a well-formed XHTML document and namespaces (see Wayne's answer) is not supported in Safari for iOS 4.2.1 and earlier, nor is it supported in Safari 5.0.x and earlier on Mac OS X and Windows.
Including SVG tags within an HTML document is a new feature of HTML5 parsers. If you download and run a nightly build of WebKit for Mac OS X or Windows, you'll see that your test case works as expected.
I would recommend taking a look at the examples here for how to get started with SVG:
http://croczilla.com/bits_and_pieces/svg/samples/
Here are some simple examples that illustrate how to include inline SVG content in XHTML:
http://croczilla.com/bits_and_pieces/svg/samples/dom1/dom1.xml
http://croczilla.com/bits_and_pieces/svg/samples/dom2/dom2.xml