var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "url to the script file here");
document.getElementsByTagName("head")[0].appendChild(script);
I looked for "onload" with scripts before, I don't remember finding such event. I ended up writing a timer-based js code to frequently check for an object that is expected to be defined in the script that is being downloaded. So, it's either that you do that, or: why not simply not do anything from within the code that inserted the script tag - instead, you have a perfectly timed moment when your script is ready: add "unwrapped" code to the script itself (e.g. alert("I, the script, loaded and I'm ready with these functions that I provide");.
And, there is no "on error" case here: the script will either download or it will not (for whatever reason: unavailability, connectivity, server-error, etc). If it does [download], any errors that may be occurring while that script is being executed/used are not really transport-related (those errors should be treated the same way you treat any other script error), so your intention with "onerror" here isn't a fit (IMO). +1 :)
EDIT: If it doesn't download, then it will not execute, and you will not be able to use it. If your client code is really waiting, then you'll have to write some kind of timer-based timeout logic; for example, after the "add to head" line above, do something like this:
window.setTimeout(function()
{
// object/variable "newScriptObject" is defined in the new script
if(!newScript)
{
// script timed out; let's proceed with plan B
}
else
{
// script ready, proceed as planned;
// although, like I said, I think it's more precise if you execute this code in your myscript.js instead - as maybe that code will be ready before the 5 sec assumed here
// alternatively, use window.setInterval to check for objects defined this script every so milliseconds, in which case, when you find the script, don't forget to stop that timer (window.clearInterval)
}
}, 5000); // wait 5 sec for the new script
Finally, there is no such thing as "partial download" in this context. A sane browser will not start executing a script that hasn't been fully, not only downloaded, but also parsed (interpreted). The "interpreted" part is why you'll see JS errors that often point to unrelated location when you miss a brace somewhere or due to other syntax errors. Missing a brace, at least, is in this category, as such syntax error practically "shifts" (kind of) code blocks completely, making it impossible to figure out what's what. Sorry for going a little bit off-topic here.