Writing Javascript into HTML

April 05, 2015 0 Comments

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:

Indicates the scripting language used for writing the snippet scripting code. It can take following values:
     2. VBScript


<script language="javascript">
<!-- JavaScript statements 
// -->

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">
<script type ="text/javascript" >
    document.write("Hello World!");


The code above will produce this output on an HTML page: 

Writing Javascript into HTML

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.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google