Skip to content

Instantly share code, notes, and snippets.

@cointilt
Created July 25, 2014 16:42
Show Gist options
  • Save cointilt/205139b9b59ff4a74b89 to your computer and use it in GitHub Desktop.
Save cointilt/205139b9b59ff4a74b89 to your computer and use it in GitHub Desktop.
Load Script
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', 'script.js');
document.getElementsByTagName('body')[0].appendChild(s);
@cointilt
Copy link
Author

I have also seen it done this way:

var s = document.createElement('script');

s.type = 'text/javascript';
s.src = 'script.js';
s.async = true;

document.getElementByTagName('body')[0].appendChild(s);

@cointilt
Copy link
Author

cointilt commented Sep 9, 2014

Looks like I have my answer:

http://jsperf.com/add-script-element-to-dom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment