totn HTML

HTML: <script> tag

This HTML tutorial explains how to use the HTML element called the <script> tag with syntax and examples.

Description

The HTML <script> tag is used to embed or reference a client-side script such as JavaScript. This tag is also commonly referred to as the <script> element.

Syntax

There are two ways that you can use the <script> tag. You can either embed the code within the <script> tags or you can reference a file that includes the code.

Embedded Code

In HTML, the syntax for the <script> tag that has embedded code within the <script> tag is:

<script type="text/javascript">
  document.write("HTML5 Script Tag Example");
</script>

Reference a File

In HTML, the syntax for the <script> tag that references a javascript file is:

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

Attributes

In addition to the Global Attributes, the following is a list of attributes that are specific to the <script> tag:

Attribute Description HTML Compatibility
async Boolean value to indicate whether browser should execute script asynchronously HTML5
src URI of the external script HTML 4.01, HTML5
type Scripting language of the code. It can be one of the following values:
  • text/javascript
  • text/ecmascript
  • application/javascript
  • application/ecmascript
HTML 4.01 (Not required in HTML5)
language Scripting language. Use type instead. Deprecated
defer Boolean value to indicate whether script is executed after document has been parsed HTML 4.01, HTML5

Note

Browser Compatibility

The <script> tag has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • Edge Mobile
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the <script> tag below, exploring examples of how to use the <script> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.