function loadScript( url, callback ) {
var script = document.createElement( "script" )
script.type = "text/javascript";
script.src = url;
script.onload = function() {
callback()
};
document.head.appendChild(script);
}
// call the function...
loadScript("js/myscript.js", function() {
alert('script ready!');
});
var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);
<script type="module">
import { hello } from './hello.mjs'; // Or it could be simply `hello.js`
hello('world');
</script>
// hello.mjs -- or it could be simply `hello.js`
export function hello(text) {
const div = document.createElement('div');
div.textContent = `Hello ${text}`;
document.body.appendChild(div);
}