Created
December 6, 2024 10:53
-
-
Save Dmitri-Sintsov/b32d8460f460c6ef086d2d57ad3a9461 to your computer and use it in GitHub Desktop.
Load global javascript
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
function loadScript(url, onload) { | |
fetch(url) | |
.then(function(response) { | |
if (!response.ok) { | |
throw new Error(`HTTP error. Status: ${response.status}`); | |
} | |
return response.blob(); | |
}) | |
.then(function(blob) { | |
let objectUrl = URL.createObjectURL(blob), | |
el = document.createElement("script"); | |
el.src = objectUrl; | |
el.setAttribute("type", "text/javascript"); | |
el.async = true; | |
el.onload = function(ev) { | |
return onload(ev); | |
}; | |
el.onerror = function(ev) { | |
throw new Error(`Error running ${url}`); | |
}; | |
document.body.appendChild(el); | |
}); | |
} | |
/* | |
loadScript(`/test.js`, function(ev) { | |
console.log(test_var); | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment