It’s easy-peasy-lemon-squeezy with HTML5
The async attribute added to HTML5 makes the job all too simple:
This tells the browser to execute the script at the first opportunity after it has finished downloading, while the page continues parsing and before the window load event.
I’m writing an embeddable widget at the moment and unfortunately, can’t rely on browser support for the async attribute. This makes me sad, as I really can’t block the page rendering.
There is a defer attribute, with pretty good support that will execute when the page has finished parsing. Not as good as executing while the page continues parsing though, like with the async attribute.
Here’s the general gist (untested):
It basically just delays adding the script tag to the DOM. Maybe there is a way to test for async or defer support and fall back to the window load event hack? I can’t wait for all these old browsers to disappear.