HTML5 <script> Tag – Usage, Attributes and Examples

The HTML <script> tag is used to embed or reference an executable script.

The only content permitted inside this element is dynamic script such as text/javascript. Both the starting and ending tag are mandatory for this element. The parent of the <script> tag can be any element that accepts metadata content, or any element that accepts phrasing content.

Scripts without async or defer attributes, as well as inline scripts, are fetched and executed immediately, before the browser continues to parse the page. The script should be served with the text/javascript MIME type, but browsers are lenient and only block them if the script is served with an image type (image/*), a video type (video/*), an audio (audio/*) type, or text/csv. If the script is blocked, an error is sent to the element, if not a success event is sent.

Here is an example of using this tag:

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


<script>
  function calculate(number) {
    var result = 0;
    if (number < 10)
      result *= 10;
    else
      result /= 5;

    return result;
  }
</script>

Attributes

async (HTML5)

Set this Boolean attribute to indicate that the browser should, if possible, execute the script asynchronously. It has no effect on inline scripts (i.e., scripts that don’t have the src attribute).

integrity

Contains inline metadata that a user agent can use to verify that a fetched resource has been delivered free of unexpected manipulation.

src

This attribute specifies the URI of an external script. This can be used as an alternative to embedding a script directly within a document. If a script element has a src attribute specified, it should not have a script embedded inside its tags.

type

This attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.

If the MIME type specified is not a JavaScript type the content embedded within its tags is treated as a data block which won’t be processed by the browser. If the type specified is module the code is treated as a JavaScript module.

text

Like the textContent attribute, this attribute sets the text content of the element. Unlike the textContent attribute, however, this attribute is evaluated as executable code after the node is inserted into the DOM.

defer

This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded. The defer attribute should only be used on external scripts.

crossorigin

Normal script elements pass minimal information to the window.onerror for scripts which do not pass the standard CORS checks. To allow error logging for sites which use a separate domain for static media, use this attribute.

Further Reading

  1. Read more about the HTML <script> element on MDN.
  2. You can also read the HTML <script> tag reference on W3C.

Rate this post —

Author: wD94ZrHL

Leave a Reply

Your email address will not be published. Required fields are marked *

0%