JavaScript syntax is embedded into HTML file. A Browser reads HTML files and interprets HTML tags. Since all JavaScripts need to be included as an integral part of an HTML document when required, the browser needs to be informed that specific sections of HTML code is JavaScript. The Browser will then use its built-in JavaScript engine to interpret this code.
The Browser is given this information using HTML tags <SCRIPT>....</SCRIPT>. The <SCRIPT> tag marks the beginning of a snippet of scripting code. The paired </SCRIPT> marks the end of a snippet of script code.
The <SCRIPT> tag takes an optional attribute, as listed below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type ="text/javascript" >
document.write("Hello World!");
</script>
</body>
</html>
The Browser is given this information using HTML tags <SCRIPT>....</SCRIPT>. The <SCRIPT> tag marks the beginning of a snippet of scripting code. The paired </SCRIPT> marks the end of a snippet of script code.
The <SCRIPT> tag takes an optional attribute, as listed below:
Attributes
|
Description
|
Language
|
Indicates the scripting language used for writing the snippet
scripting code. It can take following values:
1. JavaScript
2. VBScript
|
Syntax:
<script language="javascript">
<!-- JavaScript statements
// -->
</script>
For example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type ="text/javascript" >
document.write("Hello World!");
</script>
</body>
</html>
The code above will produce this output on an HTML page:
To insert a JavaScript into an HTML page, we use the <script> tag. Inside the <script> tag we use the type attribute to define scripting language.
So, the <script type="text/javascript"> and </script> tells where a JavaScript starts and ends.
The word document.write is a standard JavaScript command for writing output to a page.
By entering the document.write command between the <script> and </script> tags. the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page.