Skip to content

Instantly share code, notes, and snippets.

@pazguille
Created May 9, 2016 00:13
Show Gist options
  • Save pazguille/d702b9014d5d840af0e56afd973a5e0f to your computer and use it in GitHub Desktop.
Save pazguille/d702b9014d5d840af0e56afd973a5e0f to your computer and use it in GitHub Desktop.
Asynchronous script loading with initialization
// At the end of the foo.js library we should have something like this:
const foo = document.querySelector('#foo');
if (foo !== null) {
const script = document.createElement('script');
script.innerHTML = foo.innerHTML;
foo.innerHTML = '';
document.body.appendChild(script);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>foo.js implementation</title>
</head>
<body>
<!-- Asynchronous script loading with initialization -->
<script id="foo" async src="https://foo.js.org/v1/foo.js">
// This code is executed after foo.js has been loaded
var instance = new Foo();
console.log(instance);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment