What is the call flow for this window.onload scenario in javascript -
index.html
<!doctype html> <html> <head> <title></title> <script src="script1.js"></script> <script src="script2.js"></script> </head> <body> </body> </html>
script1.js
var main; window.onload = main;
script2.js
function main() { alert("foo"); }
if throw breakpoint @ var main;
, step through code in webstorm, appears to:
- load script1.js.
- load script2.js.
- call
main()
.
however doesn't execute statement alert("foo")
in function. explain what's going on in more detail?
- note: realize should avoid using
onload
. - note: realize re-order scripts , show alert.
- note: if omit statement
var main;
, step 3 above not occur.
bonus: in webstorm, shows value of window.onload
field null
, value of main
void
. difference between value of null
, void
?
because main
empty variable
@ point in script 1. window.onload
set undefined
. window.onload
expects callback function
so...
var main = function() { my_main_function() }
Comments
Post a Comment