A container for JavaScript in an HTML page.

HTML syntax:<SCRIPT> someCode </SCRIPT>
Argument list:someCodeSome script source text

The <SCRIPT> tag is how the JavaScript code is embedded into a web page. There are several ways to do this. Note that the <SCRIPT> tag must have an associated closing </SCRIPT> tag at the end of the script source text. The following attributes may be useful:

You can place the <SCRIPT> tag in the <HEAD> or <BODY> section of the page.

In the <HEAD> context, it is expected to provide some support to the rest of the page so you might place useful functions and event handlers here. You can also put some global code here and it will get executed inline, probably initializing some global variables. The intent is for the <HEAD> block to contain only meta-information about the document. This implies you should not place a document.write() method into the <HEAD> area such that it will be executed during page loading. However, both the Netscape and MSIE browsers will allow the document.write() if it is called during page loading even if it is located in the <HEAD> block.

It makes some sense to be sure that if you do a document.write() in the <HEAD> block, that you make sure it writes something sensible. For example, you can write the document title from a script that is executed inline. If you just write some textual output, the browsers are at least smart enough to place it into the page body.

Because JavaScript is interpreted, it needs to have any functions declared before they are called. However, this means they must be declared chronologically before they are called. This is not the same as positionally defining them before they are called because the page content may be traversed several times. For example, an event handler can probably be placed anywhere because very few events happen before the </BODY> closure happens. Nevertheless, it is still probably good practice to locate any functions that you can in <SCRIPT> blocks placed in the <HEAD> area.

You can place <SCRIPT> tags throughout the <BODY> of the document. These might also contain functions but are more likely to contain inline code to be executed as the page is loading. Although they are in different <SCRIPT> tag blocks, they are all conceptually part of the same script.

If your inline script code is going to do any document.write() calls to modify the HTML as the page is loaded, then this is the optimum place to put the code. It's quite sensible to break the code into smaller <SCRIPT> blocks and place them appropriately throughout the document. If the code starts to become complicated, then factor some of it into functions placed in the <HEAD> area and then call it as needed from the <BODY> area.

If you are using a frame-set, you can put the <SCRIPT> block after the <HEAD> tag but before the <FRAMESET> tag. You could write the entire <FRAMESET> description at this point using document.write() methods. You could do that with the entire <BODY> content too.

You can break your script code into smaller blocks, each one associated with a different <SCRIPT></SCRIPT> area and, if necessary, each can be executed in a different version of JavaScript. They will each have a different execution context and the scope chain may be affected, although global variables should be reachable from anywhere.

When the browser encounters a <SCRIPT> tag, it pauses the processing of the HTML page description and executes the <SCRIPT> tag's source code. That may affect subsequent HTML output anyway, and may generate some HTML to be placed into the page at the point where the <SCRIPT> block appears. Any lengthy script evaluation is going to slow down the display of your page. You should defer any lengthy processing until you can use some sleight of hand to hide it. For example, perhaps you can wait until the <BODY> tag is closed and then activate some processing with a <BODY ONLOAD="..."> handler. This may be an issue if you are using included .js files since they will need to be requested and fetched from a web server.

You can build event handlers and associate them with the event by means of the <SCRIPT> tag attributes, but this only works in MSIE.


Example code:

   myResult = 100;
   myExpr = myResult %= 1000;

See also:</SCRIPT>, <META>, <NOSCRIPT>, <SCRIPT ARCHIVE="...">, <SCRIPT EVENT="...">, <SCRIPT LANGUAGE="...">, <SCRIPT SRC="...">, <SCRIPT TYPE="...">, <STYLE TYPE="...">, Adding JavaScript to HTML, Document.scripts[], Host environment, HTML file, String, Web browser


Wrox Instant JavaScript - page 42