Javascript Introduction:
 
What is javascriptYour First Javascript ScriptSpecifying JS version
Specifying fileHiding script 

What is Javascript?
JavaScript is an object-oriented scripting language developed by Netscape that allows you to create web pages. Javascript is a client-side scripting language. This means the user's browser will be running the script. This is opposite to client-side is server-side, which occurs in a language like PHP. These PHP scripts are run by the web hosting server.

JavaScript is not Java.

JavaScript and Java are similar in some ways but fundamentally different in others. Java is a compiled programming language, similar to languages like C, C++, or Pascal.

JavaScript is a scripting language, meant  for small programs used only from within web browsers. A JavaScript script can't run alone, without the browser, the way a Java program does. JScript is Microsoft's version of JavaScript.

Top




Embedding JavaScript into a HTML-page(Your First Javascript Script)

JavaScript code is embedded directly into a HTML-page using <script>  and </script> tags. It is possible to  embed  many scripts into a single document , using multiple SCRIPT tags.

A typical format for a javascript embedded into a HTML file would look like :
<script ="JavaScript">

<!--

Javascript statements
.
.
//-->

</script>

JavaScript is case sensitive:
Unlike HTML, Javascript is case-sensitive.

Top




Specifying the JavaScript Version

As mentioned above, there are several versions of JavaScript supported by certain browsers and browser  versions. You can specify the javascript version using  the LANGUAGE attribute.
Eg:

<script language ="JavaScript1.1">

<!--

  Javascript statements
//-->

</script>

Statements within a <SCRIPT> tag are ignored if the browser does not support the javascript version specified in the LANGUAGE attribute. In other words, you can specify that a section of  code only be executed by browsers which support a particular version of JavaScript.

Eg:

    1)Navigator 2.0 executes code within the <SCRIPT LANGUAGE="JavaScript"> tag; it ignores
     code within the <SCRIPT LANGUAGE="JavaScript1.1"> and <SCRIPT

     LANGUAGE="JavaScript1.2"> tags.

    2) Navigator 3.0 executes code within the <SCRIPT LANGUAGE="JavaScript"> and <SCRIPT

     LANGUAGE="JavaScript1.1"> tags; it ignores code within the <SCRIPT

     LANGUAGE="JavaScript1.2"> tag.

    3) Navigator 4.0 executes code within the <SCRIPT LANGUAGE="JavaScript">, <SCRIPT

     LANGUAGE="JavaScript1.1">, and <SCRIPT LANGUAGE="JavaScript1.2">

Top




Specifying a file for Javascript Code

You can include an external file containing JavaScript code into a HTML file. SRC, another attribute of the SCRIPT  tag is used for this purpose.  This feature is helpful when you want to hide your javascript code from others (HTML Source code of all web pages can be seen from the borwser. Using this feature you can hide your javascript code from others)

For example: You can have all your javascript statements in a file called 'myjavascript.js' and can include into your HTML file as shown below:

<HEAD>
<TITLE>My Javascript Page</TITLE>

<SCRIPT SRC="myjavascript.js">

</SCRIPT>

</HEAD>

<BODY>

HTML Code

.

.

.

<BODY>

The external file is simply a text file containing only JavaScript statements and function definitions  and filename ends with  the extension ".js". The SRC attribute can specify any URL, relative or absolute. All JavaScript statements within a <SCRIPT> tag with a SRC attribute are ignored (unless there was an error in file inclusion).

Top




Hiding Scripts within Comment Tags

Only Netscape Navigator versions 2.0 and later recognize JavaScript. If a browser does not recognize javascript code, the whole code in displayed when you see the HTML document. To ensure that your JavaScript code is not displayed by old browsers, place Javascript inside comment fields. The markup to begin a comment  field is <!-- while you close a comment field using  //-->.

Eg:
<SCRIPT>

<!-- Hide script contents from old browsers that does not javascript.

Include JavaScript statements...

// Finish hiding here. -->

</SCRIPT>

Top