Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: How to detect SVG in HTML img element support?

javascript: How to detect SVG in HTML img element support?

I tried this, but it doesn't work:

x = new Image();
x.onload = function(){
  if (x.width) {
   alert('svg in img tag supported!');
  }
};
x.src = 'test.svg';
like image 226
Yaffle Avatar asked Feb 15 '11 10:02

Yaffle


People also ask

Does HTML IMG support SVG?

SVG images can be used as an image format, in a number of contexts. Browsers support SVG images in: HTML <img> or <svg> elements.

How do I reference an SVG file in HTML?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document. If you did everything correctly, your webpage should look exactly like the demo below.

Why is SVG not showing in HTML?

If you are trying to use SVG like <img src="image. svg"> or as a CSS background-image , and the file is linked to correctly and everything seems right, but the browser isn't displaying it, it might be because your server is serving it with an incorrect content-type.

Does SVG integrate with JavaScript?

SVG works together with HTML, CSS and JavaScript.


1 Answers

A good discussion/comparison of methods is here: http://www.voormedia.nl/blog/2012/10/displaying-and-detecting-support-for-svg-images

Based on that page, I wound up using this:

svgsupport = document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1")
like image 194
Julian Avatar answered Oct 27 '22 00:10

Julian