Skip to content

Instantly share code, notes, and snippets.

@carolina-vallejo
Created March 7, 2018 16:45
Show Gist options
  • Save carolina-vallejo/0f84cfb8f32882930ae7307b284854f3 to your computer and use it in GitHub Desktop.
Save carolina-vallejo/0f84cfb8f32882930ae7307b284854f3 to your computer and use it in GitHub Desktop.
load svg safe as xml and inject it in html
function loadSvg(selector, url) {
console.log('kshd')
var target = document.getElementById(selector);
// If SVG is supported
if (typeof SVGRect != "undefined") {
// Request the SVG file
var ajax = new XMLHttpRequest();
ajax.open("GET", url + ".svg", true);
ajax.send();
// Append the SVG to the target
ajax.onload = function(e) {
target.innerHTML = ajax.responseText;
}
} else {
// Fallback to png
target.innerHTML = "<img src='" + url + ".svg' />";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment