/

HTML / CSS / JavaScript Tutorial

HTML Element: <script>

[this page | pdf | back links]

The HTML <script> element indicates client-side script / programming code. Usually this is written in JavaScript. The <script> element either contains this code or points to an external file via its src attribute (if the src attribute is present then the <script> element must be empty). The contents of an noscript element indicates what happens for users who have disabled scripts in their browser or whose browser does not support client-side scripting.

 

The way in which a script executes is driven by the async and defer attributes. If neither are present then the script is fetched and executed immediately the browser reaches the element (before the browser continues parsing the page).

 

The attributes it can take (other than HTML global attributes and HTML event attributes) include:

 

Attribute

Description

More

async

Indicates if script to be executed asyncronously

Here. Only for external scripts

charset

Specifies character encoding

Here

defer

Script to be executed only when page has finished parsing

Here. Only for external scripts

src

URL of resource

Here

type

Type of element

Here

 

It used to support the xml:space attribute, but this is no longer supported by HTML 5.

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods, and additional properties with the same name and meaning as the attributes of the underlying HTML element referred to above. It also supports the following additional properties:

 

Property

Description

More

crossOrigin

Sets / returns the CORS settings for the script

Here

text

Sets / returns contents of all child text nodes of the script

Here

 

The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using HTML:<br>
<em>text inserted into an element using HTML</em>

<br><br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var x = document.createElement("EM");
x.textContent = "element created by a script element";
document.getElementById("element").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile