2 ms·
By and large we know better than to output javascript with document.write. We have structured templating languages even for use in a web page. Not everyone uses
by lmm 11y ago
By and large we know better than to output javascript with document.write. We have structured templating languages even for use in a web page. Not everyone uses them yet, but the better technologies do exist.
- tantalor 11y agoI 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>