javascript [removed] tag - code execution before src download

后端 未结 4 1631
南方客
南方客 2020-12-12 08:35

In this snippet


Lets assume th

相关标签:
4条回答
  • 2020-12-12 08:47

    that is because you cant import a script like that and in the same script element run javascript... because the external script will override your written javascript.... put them in different tags... also for a faster js import make a mock js src

    0 讨论(0)
  • 2020-12-12 08:52

    If you had the below code and didn't want your alert('hi') to fire until all scripts have downloaded, you can wait for the document ready event. You can do this using jQuery, or with vanilla JavaScript.

    <script src="takes very long to download" type="text/javascript"></script>
    <script type="text/javascript">
       alert('hi');
    </script>
    

    To defer until all scripts have loaded using jQuery:

    $( document ).ready( function () { 
        alert('hi');
    });
    
    0 讨论(0)
  • 2020-12-12 09:03

    alert('hi') would never fire, as the script tag supports either inline code or external file code..

    Read the specs at http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1

    The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

    0 讨论(0)
  • 2020-12-12 09:06

    No. The presence of a src attribute will cause the descendant nodes of the element to be ignored.

    If you had two script elements, then the second script would still always execute second because script elements are blocking.

    Only if you had two script elements and the first had defer or async attributes could the second execute before the first.

    0 讨论(0)
提交回复
热议问题