Go to main content
Bypass navigation bar HTML Reference HTML Tags HTML Tutorials
Bypass language selection
Bypass location bar

Location: Home > HTML Reference > HTML Tags > HTML script tag

Bypass main content

HTML script tag

Note: If you don't know what an element/tag is and/or how you must use it we recommend you to read our HTML tags and attributes tutorial that you can find in our HTML tutorials section.

Bypass main content

Description

The HTML script element is used to insert client side script code into a document. This can be achieved in two ways: inserting the code directly as content of this element or referring to an external file containing script code with the "src" attribute. In the second case, user agents should ignore the content of the element.

There are many client side languages  that can be inserted in an HTML document, and this must be specified in the "type" attribute.

Examples

In the first example, a pice of script is inserted directly in the document.

<script type="text/javascript">
function addtext() {
 document.write('This text has been added by a script.');
}
</script>

In the second example, an external file containing script code is referred using the "src" attribute.

<script type="text/javascript" src="addtext.js"></script>

Attributes

type (content-type)

This attribute specifies the scripting language of the code as a content-type. This attribute is intended to replace the deprecated attribute "language".

<script type="text/javascript">
var i = 0;
</script>

src (uri)

Points to an external resource where the script code is declared. When this attribute is present, user agents should ignore the content of the element.

<script type="text/javascript" src="functions.js"></script>

charset (charset)

Defines the character encoding of the resource pointed in the "src" attribute. The value of this attribute doesn't refer in any way to the content of the element, which uses the character encoding of the document.

<script type="text/javascript" src="functions.js" charset="utf-8"></script>

defer

When present, this boolean attribute indicates that the script won't add or change the document's content, allowing browser to continue rendering the document while the script is being loaded.

Remember that in XHTML, boolean attributes must take their own names as value (e.g., attr_name="attr_name").

<script type="text/javascript" src="functions.js" defer="defer"></script>

language (cdata)

This attribute has been deprecated in HTML 4.01. Therefore its use is no longer recommended.

Indicates the scripting language of the code as a language identifier. Language identifier values aren't standard.

<script language="javascript" src="functions.js"></script>

Events

This element doesn't support events.

See a complete list and information about events in HTML

Diseño y desarrollo: Latitud29.com

Links and logos|Contact|Beyond HTML|Tools and resources|Sitemap|Webmaster|Donate