2011 February 11, Updated 2012 May 1
Update 2012 May 1: Updated the snippet and demo to be a bit better. Old version
Just saw Dustin Diaz's tweet regarding $script.js and thought aha, I was about to throw up yet another one! Mine is just a snippet by comparison and the caveats have not been explored, but it was a start.
After a very quick comparison, where I used the first script element to insert the script, Dustin used the head element. I came to using the first script element while trying to fix an issue with the Google Analytics asychronous script, which had had problems with IE 6 in certain rare circumstances. See Google Analytics Asynchronous Snippet for the Head Element for more about it.
A bit of testing and Dustin's approach also avoids the IE6 problem, so a win all around, hurray!
document.getElementsByTagName('head').appendChild(el); but is fine with both:
var head = document.getElementsByTagName("head"); head.insertBefore(el, head.firstChild);
var script = document.getElementsByTagName('script'); script.parentNode.insertBefore(el, script);
Going to check out Dan Webb's loadrunner.js soon, which is looking very fine.
The demo loads jQuery and appends a paragraph. Compatible with IE 6+, Firefox, Safari, Chrome that I've tested so far.
- readyState Property (MSDN)