script tag (XHTML 1.1)
script statementsTopics
The script tag is used for embedding pieces of ClientSide script in to your pages. Back in the old days, it could be either JavaScript or Microsoft's VBScript, but VBScript is pretty much dead now, and while you could include other kinds of scripting languages in theory, the reality is that JavaScript is the only ClientSide scripting language available in all the major browsers.
The script tag may also be used to contain ServerSide code, like PHP or ASP.NET, but in those cases, the code is to be interpreted by the server and not the browser.
Normally, script blocks are contained within the head section of the page, but they may be used in the body part as well. You can have as many script blocks on your page as you prefer.
The language attribute
The language attribute was previously used to declare which language was to be contained within a script block. Today, the attribute is deprecated, and developers are encouraged to use the type attribute instead.
The type attribute
The type attribute is required by the script tag, and allows you to specify the type of code contained in the script block. The most common type is "text/javascript", stating that the code in the script tags are to be interpreted by the browsers JavaScript interpreter.
Example
Here's an example where we incorporate a piece of JavaScript within the page:
<script type="text/javascript">
alert('Hello world, from JavaScript!');
</script>
The src attribute
The src attribute of the script tags allow you to include an external file and have it interpreted along with the current document. This allows you to have large quantities of scripting code placed elsewhere, so that it doesn't clutter up your HTML files. Here's an example, where we include a JavaScript file from the same directory, into our page:
<script type="text/javascript" src="javascript.js"></script>
You can refer to variables, functions etc. from the external file as if they were written directly in the script block.
Attributes
| Attribute | Deprecated | Required | Description |
|---|---|---|---|
| charset | char encoding of linked resource | ||
| class | space-separated list of classes | ||
| defer | UA may defer execution of script | ||
| dir | direction for weak/neutral text | ||
| id | document-wide unique id | ||
| lang | language code | ||
| language | X | predefined script language name | |
| src | URI for an external script | ||
| style | associated style info | ||
| title | advisory title | ||
| type | X | content type of script language |
Events
| Event | Description |
|---|---|
| onclick | a pointer button was clicked |
| ondblclick | a pointer button was double clicked |
| onkeydown | a key was pressed down |
| onkeypress | a key was pressed and released |
| onkeyup | a key was released |
| onmousedown | a pointer button was pressed down |
| onmousemove | a pointer was moved within |
| onmouseout | a pointer was moved away |
| onmouseover | a pointer was moved onto |
| onmouseup | a pointer button was released |