HTML Script Tag

The <script> tag is used to define a client-side script (JavaScript).

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

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

The HTML <script> tag is used for declaring a script (such as JavaScript) within your HTML document.

Example:

<!DOCTYPE html>
<html>
<head>
<title> HTML script Tag Example</title>
</head>
<body>
<script type="text/javascript">
    document.write("You're visiting Tips n Track Tutorial..!");
</script>
</body>
</html>
Try it with the Editor

The <script> tag has two purposes:

1. It identifies a block of script in the page.

2. It loads a script file.

Which it does depends on the presence of the src attribute. A </script> close tag is required in either case.

A script tag can contain these attributes:

src="url"

The src attribute is optional. If it is present, then its value is a url which identifies a .js file. The loading and processing of the page pauses while the browser fetches, compiles, and executes the file. The content between the <script src="url"> and the </script> should be blank.

If the src attribute is not present, then the content text between the <script> and the </script> is compiled and executed. The script should not contain the sequence

</

because it could be confused with the </script>. Inserting a backslash between < and / in strings avoids the problem.

<\/

The backslash will be ignored by the JavaScript compiler.

Do not use the <!-- //--> hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years. <!-- //--> is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include --, so a script that decrements has an HTML error.

language="javascript"

This attribute has been deprecated. It was used to select other programming languages and specific versions of JavaScript. You don't need it. Don't use it.

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

defer

This attribute was intended to alter the timing of component loading in pages. It is not well supported and should not be used.

for="name" event="name"

This attribute is a Microsoft feature for declaring event handlers. It is not standard. Don't use it.

document.write

The document.write method provides a way of incorporating strings into the HTML content of the page. There are better ways to do that, such as .innerHTML and .createElement or HTML cloning patterns. Use of document.write should be avoided.

document.write is recklessly dependent on timing. If document.write is called before the onload event, it appends or inserts text into the page. If it is called after onload, it completely replaces the page, destroying what came before.

document.write encourages bad structure, in which script and markup are intermingled. A cleaner structure has minimal interaction between markup and script.