function loadFileSync(path) { const xhr = new XMLHttpRequest(); xhr.open('get', path, false); xhr.send(); return xhr.responseText; } function appendModule(path) { const el = document.createElement('script'); el.defer = el.async = false; el.type = "module"; el.src = path; document.head.appendChild(el); } const msgs = []; let flushed = false; function log(text) { // document.body not always available in firefox in modules flushed ? append(text) : msgs.push(text); } function flush() { flushed = true; msgs.forEach(append); } function append(text) { const p = document.createElement('p'); p.textContent = text; document.body.appendChild(p); } window.onload = flush; appendModule('first.js'); appendModule('second.js'); // Comment out this next line and second will never load. // With this next line it SOMETIMES loads. loadFileSync('file.txt');