-
-
Save tarciozemel/1884583 to your computer and use it in GitHub Desktop.
JavaScript: incluir arquivos js/css dinamicamente
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
/* | |
* Ex: | |
* include("http://mysite.com/bookmarklet.css"); | |
* include("http://mysite.com/bookmarklet.js"); | |
*/ | |
let include = ( src, callback ) => { | |
let ext = src.split( /[\#\?]/ )[ 0 ].split( '.' ).pop().toLowerCase(); | |
let inc; | |
if ( ext === 'css' ) { | |
inc = document.createElement( 'link' ); | |
inc.rel = 'stylesheet'; | |
inc.type = 'text/css'; | |
inc.media = 'screen'; | |
inc.href = src; | |
if ( callback ) { | |
callback(); | |
} | |
} else { | |
inc = document.createElement( 'script' ); | |
inc.src = src; | |
inc.async = true; | |
inc.onload = inc.onreadystatechange = callback; | |
} | |
document.querySelector( 'head' ).appendChild( inc ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment