Created
March 7, 2018 16:45
-
-
Save carolina-vallejo/0f84cfb8f32882930ae7307b284854f3 to your computer and use it in GitHub Desktop.
load svg safe as xml and inject it in html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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