1

I'm using svg animations on my website, but it also have to run in older browsers, which do not support svg. I could solve this with a redirect to a static page.

But is it even possible to detect if the Browser supports svg's with the help of javascript?

Any help would be appreciated.

p2or
  • 339
  • 8
  • 27

2 Answers2

8

I did a lot of searching on this myself a while back, when I was building the $.supports() section of my bolster augmentation library. Here is what i settled on:

canSvg = !!(document.createElementNS && document.createElementNS('http://www.w3.org/2000/svg','svg').createSVGRect);

Tested on IE8+, works just fine.

EDIT

@SinaSadrzadeh makes an excellent point, and Modernizr is a fantastic resource. I recommend you use Modernizr for your project if you aren't already, but especially if SVG is not the only thing to test. If SVG is the only thing you are testing, bringing in an entire library is not necessary for that, but it doesn't allow for a lot of extensibility.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
  • that's write, but you can customize what you want to be in modernizr package here [modernizr download builder](http://modernizr.com/download/) – sadrzadehsina Dec 19 '14 at 14:42
  • 1
    @SinaSadrzadeh - but again, if all he needs is to test for a single item (which is an unlikely scenario, but still) ... explain to me how a separate file can ever be faster than the single line of JS I've provided. – PlantTheIdea Dec 19 '14 at 14:43
  • It's ok man :), I'm agree with you. this is just another aspect of modernizr. – sadrzadehsina Dec 19 '14 at 14:45
  • @PlantTheIdea After a while this is the optimal solution, it's short, easy to use and does what it is supposed. Thanks again! – p2or Jan 10 '15 at 22:23
  • @PlantTheIdea Agree. No need to import an entire library to test only SVG support – Raja Khoury Oct 08 '15 at 20:07
4

You can take advantage of a very useful library called modernizr

After including it simply write something like this:

if (!Modernizr.svg) {
  // svg fallback
}
sadrzadehsina
  • 1,331
  • 13
  • 26
  • 1
    I added some verbiage to my answer, because Modernizr is always a good option. If he only needs to test for SVG, though, then its using a sledgehammer to hammer a nail. – PlantTheIdea Dec 19 '14 at 14:39