I have a JS question, which is either tricky or stupid, not sure.
So, let's say I need to execute a function when an external JS script on the page finishes loading completely, because otherwise i'll get an error.
case 1:
var s = document.createElement('script');
s.src = "http://somedomain/somescript.js"
function foo() {
doSomething;
}
s.onload = foo;
in this case the function itself assigned to onload, and should run when the script finishes loading.
case 2:
s.onload = foo();
In this case, according to What is the different between window.onload = init(); and window.onload = init;, the return result of the function is assigned when the load finishes, so, the function can start executing in the beginning of the load, and not when the element finishes loading, depends on the browser.
case 3:
s.onload = function() {
doSomething;
}
This is the tricky part. I think that this syntax should execute like case 2, but i can't substantiate this. Am I just defining the function or both defining and executing it at the same time?