I want to load a script that loads another script based on condition and this script is adding a variable to the global window.
<head>
<script src="load-something.js"></script>
<script>conosle.log(window.someVariable)</script>
</head>
load-something.js
function loadScript( path ) {
const head = document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script.src = path;
head.append(script);
}
if(condition) {
loadScript('pathToJsFile.js');
}
pathToJsFile.js
window.someVariable = ...
My problem is that someVariable
is undefined. Is it possible to force the script to block?