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:

  1. load script1.js.
  2. load script2.js.
  3. 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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -