-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.inlinesvg.js
53 lines (41 loc) · 1.44 KB
/
jquery.inlinesvg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(function($) {
// Modernizr 2.7.2
supportsInlineSVG = function() {
var div = document.createElement('div');
div.innerHTML = '<svg/>';
return (div.firstChild && div.firstChild.namespaceURI) == 'http://www.w3.org/2000/svg';
};
// Modernizr 2.7.2
supportsSVG = function () {
return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect;
};
$.fn.inlinesvg = function (options) {
if (supportsInlineSVG()) {
this.each(function(index){
var $img = $(this),
imgID = $img.attr('id'),
imgClass = $img.attr('class'),
imgURL = $img.attr('src');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Copy properties from IMG tag to SVG tag
$.each($img.prop('attributes'), function() {
$svg.attr(this.name, this.value);
});
// Remove any invalid XML tags as per http://validator.w3.org
$svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
});
});
return;
}
// No SVG Support? Replace with PNGs.
if (!supportsSVG()) {
this.attr('src', function() {
return $(this).attr('src').replace('.svg', '.png');
}).removeClass('svg');
}
}
}(jQuery));