Skip to content

Instantly share code, notes, and snippets.

@tarciozemel
Forked from adrienjoly/include.js
Last active June 22, 2016 16:52
Show Gist options
  • Save tarciozemel/1884583 to your computer and use it in GitHub Desktop.
Save tarciozemel/1884583 to your computer and use it in GitHub Desktop.
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