I have a HTML file, which is then calling a javascript file. Basic function of the javascript file is to draw a svg file, and do modifications on it. for example
I am embedding the svg image in my JS file like this
this.my_object.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><image xlink:href="img/gauge.png" width="122" height="127"/><g id="needle" transform="rotate(0,62,62)"><circle cx="62" cy="62" r="4" style="fill: #c00; stroke: none"/><rect transform="rotate(-130,62,62)" name="arrow" x="58" y="38" width="8" height="24" style="fill: #c00; stroke: none"/><polygon transform="rotate(-130,62,62)" points="58,39,66,39,62,30,58,39" style="fill: #c00; stroke: none"/></g><text id="value" x="35" y="103" focusable="false" editable="no" style="stroke:none; fill:#fff; font-family: monospace; font-size: 12px"></text></svg>';
Now, according to this post
Safari embeded SVG doctype
I cant inline svg image because then it wont work on safari. Now due to certain restrictions I cant embed svg file in html, it has to be accessed through javascript. Is there any way svg image can be used in javascript without using innerHTML, as finally the image has to be renedered on safari.
PS: This image has to be copied in the same folder when compiling.
https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg
I’m currently in Linux and can’t test with Safari, but still will post the answer…
Try to do in this way.
HTML:
JavaScript:
UPDATE #1 – data URI encoding:
The usage of unencoded data URI might be failed in IE 8 and 9.
So you can determine the browser using
navigator.userAgentand if it’s IE >= 8,then encode the string to Base64 before assigning it as value of
image.src.UPDATE #2 – using
objecttag:You can set either the data URI or direct link to .svg file location.
objectDEMOUPDATE #3 – CSS approach:
DEMO of CSS approach
UPDATE #4 – MIME type:
Comment by UnderDog:
<staticContent><mimeMap fileExtension=".svg" mimeType="image/svg+xml" /></staticContent>The server should send correct
Content-Typeheader.