Created
November 9, 2015 22:05
-
-
Save oknoway/70f15acaa835703c7406 to your computer and use it in GitHub Desktop.
This file contains 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( url ) { | |
// AJAX-y load the SVG icon | |
var req = new XMLHttpRequest(); | |
req.onload = serializeSVG; | |
req.open( 'get', url, true ); | |
req.send( null ); | |
} | |
function serializeSVG() { | |
// Turn the SVG into a serialized string | |
var ser = new XMLSerializer(); | |
toggleIcon = ser.serializeToString( this.responseXML.documentElement ); | |
insertToggleIcon( toggleIcon ); | |
} | |
function insertToggleIcon( icon ) { | |
// insert the toggle icon | |
var navToggle = document.getElementById( 'nav-toggle' ); | |
navToggle.insertAdjacentHTML( 'afterbegin', '<span class="toggle-icon icon">' + icon + '</span>' ); | |
} | |
// set toggle icon to svg if supported | |
if ( Modernizr.svg ) { | |
loadSVG( wpURLs.stylesheet_directory_uri + '/img/icons/icon_hamburger.svg' ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been using the
<svg>
<use>
trick lately which is so awesome. I've got a grunt workflow that packs all the fontawesome icons i use into a icons.svg sprite. Each icon has an id in the sprite that is referenced using a hash to include just that part of the image.Accessibly Load SVG Icons
SVG PNG Fallback
Only downside is your SVG Sprite can't be loaded from another domain, yet jonathantneal/svg4everybody#16