Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. function convertImgToInlineSVG(){
  2. $('img.svg').each(function(){
  3. var $img = $(this);
  4. var imgID = $img.attr('id');
  5. var imgClass = $img.attr('class');
  6. var imgURL = $img.attr('src');
  7.  
  8. jQuery.get(imgURL, function(data) {
  9. // Get the SVG tag, ignore the rest
  10. var $svg = $(data).find('svg');
  11.  
  12. // Add replaced image's ID to the new SVG
  13. if(typeof imgID !== 'undefined') {
  14. $svg = $svg.attr('id', imgID);
  15. }
  16. // Add replaced image's classes to the new SVG
  17. if(typeof imgClass !== 'undefined') {
  18. $svg = $svg.attr('class', imgClass+' replaced-svg');
  19. }
  20.  
  21. // Remove any invalid XML tags as per http://validator.w3.org
  22. $svg = $svg.removeAttr('xmlns:a');
  23.  
  24. // Check if the viewport is set, if the viewport is not set the SVG wont't scale.
  25. if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
  26. $svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
  27. }
  28.  
  29. // Replace image with new SVG
  30. $img.replaceWith($svg);
  31.  
  32. }, 'xml');
  33.  
  34. });
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement