Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript: incluir arquivos js/css dinamicamente
/*
* 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