Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Last active March 14, 2019 10:11
Show Gist options
  • Save AlekVolsk/953c12ebc31209be620aaac39c8d0ed0 to your computer and use it in GitHub Desktop.
Save AlekVolsk/953c12ebc31209be620aaac39c8d0ed0 to your computer and use it in GitHub Desktop.
Set href to svg-use (for svg-sprite)
function svgHref() {
var
uses = document.querySelectorAll('use'),
lhref = location.href.split("#")[0];
for (var i = 0; i < uses.length; i++) {
var href = uses[i].getAttribute('xlink:href');
if (href.indexOf('#') == 0) {
uses[i].setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', lhref + href);
}
}
}
document.addEventListener('DOMContentLoaded', function(){
svgHref();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment