load and execute order of scripts

后端 未结 4 1957
我在风中等你
我在风中等你 2020-11-21 11:34

There are so many different ways to include JavaScript in a html page. I know about the following options:

  • inline code or loaded from external URI
  • inc
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 12:13

    The browser will execute the scripts in the order it finds them. If you call an external script, it will block the page until the script has been loaded and executed.

    To test this fact:

    // file: test.php
    sleep(10);
    die("alert('Done!');");
    
    // HTML file:
    
    

    Dynamically added scripts are executed as soon as they are appended to the document.

    To test this fact:

    
    
    
        Test
    
    
        
        
    
    
    

    Order of alerts is "appended" -> "hello!" -> "final"

    If in a script you attempt to access an element that hasn't been reached yet (example:

    ) then you will get an error.

    Overall, yes you can include external scripts and then access their functions and variables, but only if you exit the current

提交回复
热议问题