script Element
Description
The script
element can include scripting in your pages,
either defined inline in the document or referenced to an external file.
The most commonly used type of script is JavaScript.
You use one script
element for each script.
The script
element has local attributes: type, src, defer, async, charset
.
A start and end tag for script
element are required;
self-closing tags are not permitted, even when referencing an external JavaScript library
The type
attribute is optional in HTML5.
The async
and defer
attributes have been added in HTML5.
- type - Specifies the type of the script that is references or defined. This attribute can be omitted for JavaScript scripts.
- src - Specifies the URL for an external script file.
- defer - Specifies how the script will be executed.
These attributes can only be used in conjunction with the
src
attribute. - async - Specifies how the script will be executed.
These attributes can only be used in conjunction with the
src
attribute. - charset - Specifies the character encoding of an external script file.
This attribute can only be used in conjunction with the
src
attribute.
Inline Script
An inline script is included the JavaScript statements in the HTML page.
<!DOCTYPE HTML>
<html>
<head>
<script>
document.write("This is from the script");
</script>
</head>
<body>
<p>This is a test.</p>
<a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
By default, scripts are executed as soon as they are encountered in the page.
You usually use the script
element inside the head
element,
but you may use it anywhere in an HTML
document.