I have a single html file where I copy paste the emscripten generated code into it's
var script = document.createElement('script'); script.src = "my_emscripten_script.js"; document.body.appendChild(script);
In Emscripten you don't need to have a
main function at all, that tutorial I link to renames the
mainf, if you export it by passing the command line flag
Module.ccall('mainf', null, null);
If you want to make sure that that isn't called until the Emscripten run-time is ready, the
Module object that Emscripten uses can have a
onRuntimeInitialized method added to it. This function is called when Emscripten is ready, so you can use this for doing your call to
ccall to start the program.
Having said all this, I am surprised that your
main function is being run before the document is ready. Please be aware that some of the emscripten functions in
html5.h (such as the functions for requesting the canvas goes full-screen) are only functional upon user initiated events (for security reasons), so it could be that that is your problem rather than at what point during page load
main is called.