I want to use SVG objects along my application HTML code using syntax:
<svg viewBox="0 0 100 100" class="icon shape-cart" style="width: 100px;height: 100px;">
<use xlink:href="#shape-cart"></use>
</svg>
Accoring to this article http://css-tricks.com/svg-sprites-use-better-icon-fonts/
to use svg in such a way one need to inject the SVG data into HTML body.
So my question is what is the best way to perform such injection while runtime? (not inject in HTML on server)
is embeds svg in separate document, so you can not access it and use using
My current solution using jquery:
$.ajax('/assets/icons.svg.txt').then(function(svgData){
$(function(){
$('body').prepend($(svgData).css('display', 'none'))
})
})
It uses text version of the svg file (just addes .txt extension to svg file), because otherwise *.svg will be loaded by juery and converted to HTML document element. Css display none is set to make element not mess with the DOM.
Maybe there is more standard way to accomplish this task?