To the table of contents
The script element
You can also integrate several scripts with this method. To do this, place several elements one below the other.
Execution of script elements
With the element you can embed scripts in the document header as well as in the document body. The execution of the script code follows certain rules, which we will consider below.
As soon as the browser receives the HTML document from the web server, it begins to process the source text and transfer it to an internal memory structure, the Document Object Model (DOM). The browser module responsible for this is called itself and the processing process.
Scripts have access to the objects of previously integrated scripts
The example integrates three scripts, the first two as external files, the third directly in the HTML code. The browser executes the scripts in the order in which they are integrated. Therefore, later scripts can use the objects, functions and variables that were defined by the previously integrated scripts.
In the example it is first integrated, downloaded and executed. The subsequent script from the file can use the functions noted in it. Finally, the third script can use a function that was defined in.
External scripts delay the structure of the document
That the browser does not have the integrated scripts first to, but already while of reading in the HTML code has advantages and disadvantages.
On the one hand, scripts are executed as quickly as possible and it is guaranteed that an external script is executed before a subsequent internal script is processed.
On the other hand, page loading slows down when large external script files are downloaded from the web server.
Scripts can add to the document while it is being loaded
With this method, a script can directly influence the document while it is being loaded and set the course. accepts HTML code as a string. It inserts the HTML code into the document where the associated element is.<!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8"> <title>Zugriff auf das DOM während dem Parsen des Dokuments</title> <script> document.write (""); </script> </head> <body> <script> document.write ("
A script has access to the elements in front of the associated element
As already mentioned, a script is executed while the HTML document is being read. At this point in time it does not yet have access to the entire DOM element tree. Instead, it can only access a sub-tree, namely the elements that in front the associated element and have therefore already been read.<head> <title> Access to the DOM while the document is being parsed <body> <h1 id = "heading"> Example heading