3 ms·
I wish! Using document.write to inject script tags is the most reliable way to insert code to run before another script tag. Here's an example where we might lo
by tantalor 11y ago
I wish! Using document.write to inject script tags is the most reliable way to insert code to run before another script tag. Here's an example where we might load some scripts then run some code before finally calling some main function.
<script>
// Load optimized or debug code.
if (debugMode) {
document.write('<script src="build-debug.js"></script>');
} else {
document.write('<script src="build-opt.js"></script>');
}
// Enable some runtime debug flags.
if (enableTracing) {
// This runs *after* loading the script above and
// before main() is called below.
document.write('<script>ENABLE_TRACING = true;</script>');
}
</script>
<script>
// This runs *after* the script tags injected in the section above.
main();
</script>