Coming from this answer that says:
You should set the
src
attribute after theonload
event, f.ex:el.onload = function() { //... el.src = script;
You should also append the script to the DOM before attaching the
onload
event:$body.append(el); el.onload = function() { //... el.src = script;
Remember that you need to check
readystate
for IE support. If you are using jQuery, you can also try thegetScript()
method: http://api.jquery.com/jQuery.getScript/
I am skeptical about believing that this would be the correct answer to the question.
So, is the order of setting onload
handler and src
so important? I think they are evaluated by browser instantly so I think that there is no difference between:
el.onload = function() { //...
el.src = script;
and
el.src = script;
el.onload = function() { //...
Am I right?