• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 4. How JavaScript Programs Work > Which Script Runs First?

Which Script Runs First?

As you learned in Hour 1, you can actually have several scripts within a Web document: one or more sets of <script> tags, and any number of event handlers. With all of these scripts, you might wonder how the browser knows which to execute first. Fortunately, this is done in a logical fashion:

  • Sets of <script> tags within the <head> section of an HTML document are handled first. Since these scripts cannot create output in the Web page, it's a good place to define functions for use later.

  • Sets of <script> tags within the <body> section of the HTML document are executed after those in the <head> section, while the Web page loads and displays. If there is more than one script in the body, they are executed in order.

  • Event handlers are executed when their events happen. For example, the onLoad event handler is executed when the body of a Web page loads. Since the <head> section is loaded before any events, you can define functions there and use them in event handlers.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint