Adding Scalable Vector Graphics (SVG) in Unsupported Browser

In an earlier post in this series, we have mentioned a bit about SVG’s pitfall with old browsers and using Raphael.js to serve the graphic as an alternative solution. In this post we will take a look at this matter further.

The idea is simple, we will still be using SVG elements as the primary way to deliver graphics on our webpage, but at the same time we will also provide a fallback function so that it can be still rendered in unsupported browsers.

A Look into: Scalable Vector Graphics (SVG)

A Look into: Scalable Vector Graphics (SVG)

Vector graphics have been widely applied in print media. In a website, we can also add vector graphics... Read more

Sure, there are many paths we can take, but we will only look into to two solutions that I think is a better generic solution. So, let’s see how we can do it.

Using Object Element

Apart from putting it directly into an HTML document, there are several ways to embed SVG. For instance, if we store the graphic in .svg file, we can use the <object> element.

 <object data='images/apple.svg'></object>

For the demonstration purpose, we have added an Apple logo with SVG to our webpage. However, the unsupported browsers will remain empty. To solve the problem, we can serve a Bitmap graphic, as follows;

 <object data='images/apple.svg'>
 <img src='images/apple.png'/>

This way, supported browsers will still be taking the .svg, while the unsupported browsers will carry the Bitmap graphic. We have added the “png” mark below the Apple logo to track which graphic is being delivered.

svg apple logo

However, as we have mentioned in the other post, Bitmap graphics are not as flexible and scalable as SVG. So, let’s take a look at another solution.

Using Modernizr

Another method we can use is by using Modernizr. For those of you who are not familiar with this JavaScript library, don’t worry we will have a dedicated post to cover about it. For now, just keep up with us.

First of all, let’s prepare some required JavaScript libraries, Modernizr.js and Raphael.js. Then, we also need to convert our .svg file into a Raphael-supported format with this tool, ReadySetRaphael.js, and save the output in a separate .js file; let’s name it svg.js.

Include the Modernzr.js in the HTML document, like so:

 <script type="text/javascript" src="scripts/modernizr.js"></script>

And for the other two files, raphael.js and svg.js, we will load it conditionally, only when it is viewed in unsupported browsers.

With Modernizr we can detect the browser capability, in this case we will detect whether the browser is capable of rendering SVG, and if it is not we will serve the script:

 if (!Modernizr.inlinesvg) {
 '<script type="text/javascript" src="scripts/raphael.js"><\/script>', 
 '<script type="text/javascript" src="scripts/svg.js"><\/script>'

Now we only need to add the HTML markups, as follows;

 <svg version="1.1" xmlns="" xmlns:xlink="" x="0" y="0" width="500" height="280" viewBox="0 0 500 280" enable-background="new 0 0 500 280" xml:space="preserve">
 <path fill="#333333" d="M296.908,120.622c-8.77,6.201-13.158,13.676-13.158,22.41c0,10.458,5.425,18.479,16.262,24.076
 C282.777,109.183,290.814,112.996,296.908,120.622z M273.238,82.575c0.108,1.344,0.167,2.378,0.167,3.102
 <div id="applelogo"></div>

In the code snippet above, we have added the SVG directly into the HTML document and a div to accommodate the Raphael code. Once again to be able to notice which graphic is being delivered easily, we have also put some text below the logo inside the SVG element.

 <text x="210" y="250">This is SVG</text>

So, here is what we will get in the browsers.

svg apple

To see it more clearly, you can view the demo from the links below and make sure you try it in different browsers; we would suggest IE8/7 and Google Chrome.

Final Thought

Alright, these are just a few examples, in particular cases they might not be that effective. But in general, these should do a favor to solve the problem on serving SVG in unsupported browsers. Lastly, if you have something to add that we’ve missed in this post, feel free to share it in the comment box below.

Thanks for reading this post and we hope you enjoyed it.


You may also be interesting in the following posts: