Tag <script> HTML Reference

Example

Display "Hello world" with JavaScript:

<script type="text/javascript">
  alert('Hello World!')
</script>

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

1. Definition and Usage

The <script> tag is used to define a client-side script, such as a JavaScript.

The <script> element either contains scripting statements, or it points to an external script file through the src attribute.

The required type attribute specifies the MIME type of the script.

Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.


2. Tips and Notes

Note: If the "src" attribute is present, the <script> element must be empty.

Tip: Also look at the <noscript> element for users that have disabled scripts in their browser, or have a browser that doesn’t support client-side scripting.


3. Differences Between HTML and XHTML

HTML 4 and XHTML deal different with the content inside scripts:

  • In HTML 4, the content type is declared as CDATA, which means that entities will not be parsed.
  • In XHTML, the content type is declared as (#PCDATA), which means that entities will be parsed

This means that in XHTML, all special characters should be encoded or all content should be wrapped inside a CDATA section.

To ensure that a script parses correctly in an XHTML document, use the following syntax:

<script type="text/javascript">
//<![CDATA[
  alert('Hello World!')
//]]></script>

4. Required Attributes

DTD indicates in which HTML 4.01/XHTML 1.0 DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.

Attribute Value Description DTD
type MIME-type Specifies the MIME type of the script STF

5. Optional Attributes

Attribute Value Description DTD
charset charset Specifies the character encoding used in an external script file STF
defer defer Specifies that the script is executed when the page has finished parsing (only for external scripts) STF
src URL Specifies the URL of an external script file STF
xml:space preserve Specifies whether whitespace in code should be preserved  

6. Standard Attributes

The <script> tag does not support any standard attributes.


7. Event Attributes

The <script> tag does not support any event attributes.

Other HTML/XHTML tags:


Relative articles

Overflow CSS – About and How to use it in various conditions

Overflow CSS – About and How to use it in various conditions

Today I will tell you about CSS again. This is will property called ‘overflow’. This property already in CSS since second version (CSS2). All popular browsers like Firefox, IE, Safari, Opera etc supporting this property. I will tell about IE browsers in details a little after.

Overflow property determines the behavior of a block-type element in situations, where its contents exceeds its size. Overflow property can have five values. Each value defines two aspects of the generated element: whether to display the contents outside of the block itself and are we will have scrolling mechanism. If the value of the overflow property is auto, the browser displays the contents of the element according to its settings, plus – it should allow the user to scroll if the content does not fit in the container.
(more…)


Bottom Menu Builder (HTML5)
Bottom Menu Builder (HTML5)

Bottom Menu Builder (HTML5)

Bottom navigation website menu. I am sure that you have seen it many times (at different websites). As usual, this is three-four columns menu with different links. Today I would like to show you a builder, which you can use to build that bottom menu. Main purpose of this builder – prepare static HTML code (as cache file) for embedding into bottom of your website. If you like that idea, you are welcome to play with it, and I will tell you about creation of nice user friendly menu builder. This tutorial is separated into 2 parts, today I will tell you about first side: user interface with drag and drop possibility.
(more…)