I am creating a webpage which needs to pull in a Javascript library which is indispensable but also breaks other code by modifying the prototypes of built-in classes. I have sandboxed it in an iframe and set up cooperative data sharing between the two.
When the iframe’s body.onload fires, my code will modify the parent document. In theory this code will be correct regardless of whether the parent or child’s body.onload fires first. In practice, I can’t test one code path because the iframe always seems to load before the parent, even if I inject artificial delays in the web server.
Is the parent body guaranteed to always fire onload only after child documents have loaded, or is this just a quirk of Firefox?
If not, how can I, for the sake of testing, force the child to load later when sleep(5) in child.php doesn’t give me this test case?
Finally, when the child Javascript modifies its Javascript environment, is this guaranteed to be separate from the parent frame, or is this just a quirk of Firefox that makes it work?
Portability matters. Thanks.
If portabilty matters for you, then convinience should matter too. Your whole concept sounds clunky and unreasonable by reading your post (dealing with iframes,
sleep, etc.).However, to answer you questions I’m not 100% sure about
document.body.onload, but you can be sure that any handler bound towindow.onloadwill fire after all iframes were loaded, that is very very cross-browser compatible. Also the fact that an iframe is a “sandboxed” environment with its own DOM is very cross-browser. So you don’t need to mind about the parent document or other iframes and conflicts.