html tutorial - <script> Tag in HTML - html5 - html code - html form



script  tag

Learn html - html tutorial - script tag - html examples - html programs

  • The <script> tag is used to embed an executable script (JavaScript).
  • The <script> element either holds inline scripting statements, or it fetch an outside script file through the src attribute.
  • Image manipulation, form validation, and dynamic changes of content are made by JavaScript.
  • <script> tag belongs to Metadata content, Flow content and Phrasing content.
learn html -  html tutorial -  html development tutorial -  html examples -  html  -  html script -  html program -  html download -  html samples  -  html scripts

Syntax for <script> tag:

<script>content</script>

Differences between HTML 4.0.1 and HTML 5 for <script> tag in HTML:

HTML 4.0.1

  • The "type" attribute is required.
  • The "async" attribute not supported.
  • "xml:space" is supported.

HTML 5

  • The "type" attribute is optional.
  • The "async" attribute is new to HTML5.
  • "xml:space" is not supported.

Sample Coding for <script> tag:

Tryit<!DOCTYPE html>
<html>
    <head>
        <title>Wikitechy HTML script Tag</title>
    </head>
    <body>
        <p id="para"></p>
        <script>
          document.getElementById("para").innerHTML = "Hello Wikitechy!";
        </script>
    </body>
</html>

Code Explanation for <script> tag:

<script> Tag Code Explanation

  1. <p> tag id value has been defined as “para”.
  2. <script> tag is used to embed the executable script.
  3. The text Hello Wikitechy! will be included inside <p> tag which has the id as para.

Output for <script> tag:

<script> Tag Output

  1. The text “Hello Wikitechy!” has been included inside <p> tag by using <script> tag.

Attributes of <script> tag:

Attribute Value HTML4 HTML5 Description
async async No Yes Defines that the script is executed only for external scripts.
charset charset Yes Yes Defines the character encoding used in an external script file.
defer defer Yes Yes Defines that the script is executed though the page has been finished parsing.
src URL Yes Yes Defines the URL of an external script file.
type media_type Yes Yes Defines the media type of the script.
xml:space preserve Yes No Defines whether whitespace in code have been preserved.

Browser Support for <script> tag in HTML:

Yes Yes Yes Yes Yes

Tips and Notes

  • If the "src" attribute is present, the <script> element must be empty.
  • There are many ways an external script should be executed:
    • async="async": The script can be executed while the page continues the parsing.
    • defer="defer": The script is executed when the page has finished parsing.
    • If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page.

Related Searches to <script> tag in html