Guest User

Untitled

a guest
Oct 29th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. (function($) {
  2. $(':not(.logo) img').filter(function() { return this.src.match(/.*\.svg$/); }).each(function(){
  3. var imgSVG = $(this);
  4. var imgURL = $(this).attr('src');
  5. var imgAlt = $(this).attr('alt');
  6. var imgTitle = $(this).attr('title');
  7. var imgClass = $(this).attr('class');
  8. $.get(imgURL, function(data) {
  9. var svg = $(data).find('svg');
  10.  
  11. // Remove invalid XML tags - mostly that is usefull to preserve so it is commented out here
  12. // svg = svg.removeAttr('xmlns xmlns:xlink');
  13.  
  14. // add replaced image's alt tag to the inline SVG
  15. typeof imgAlt === 'undefined' || imgAlt === '' ? (svg = svg.attr('alt', 'Replaced Image')) : (svg = svg.attr('alt', imgAlt)) ;
  16. // add replaced image's Title tag to the inline SVG
  17. typeof imgTitle === 'undefined' || imgTitle === '' ? (svg = svg.attr('title', 'A SVG Image replaced by its inline code')) : (svg = svg.attr('title', imgTitle));
  18. // Add replaced image's classes to the new SVG and add replaced-svg as new class
  19. typeof imgClass === 'undefined' || imgClass === '' ? (svg = svg.attr('class', 'replaced-svg')) : (svg = svg.attr('class', imgClass+' replaced-svg'));
  20. // Replace image with inline SVG Code
  21. imgSVG.replaceWith(svg);
  22. }, 'xml');
  23. });
  24. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment