Created
May 9, 2016 00:13
-
-
Save pazguille/d702b9014d5d840af0e56afd973a5e0f to your computer and use it in GitHub Desktop.
Asynchronous script loading with initialization
This file contains hidden or 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
// 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); | |
} |
This file contains hidden or 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
<!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