JavaScript Tutorial


Introduction to JavaScript


JavaScript is a cross-platform, object-oriented, case-sensitive scripting language that was first invented by Netscape. JavaScript is used in the web page to provide the interactivity to the web page. For example It can be used to provide the validation to your online form, to create the cookies, can detect the browser type and version, can tell the web page to react to the certain event at the user end like mouse click or mouse over….in other words JavaScript is used to add dynamicity to the static web page.

Core JavaScript contains set of objects, such as Array, Date, and Math, and a set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes; for example:

Client-side JavaScript is the script that is executed on the client side i.e. the user's browser. For example client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation.

Server-side JavaScript is the script that is executed on the server. For example, server-side script allow an application to communicate with a relational database by storing the information received in the online form from the user in the database.

Now, let's have a look at the history of the JavaScript language, JavaScript was created in 1995 by Netscape and was originally to be named as LiveScript, but it finally got renamed as JavaScript. Many people get confused between Java and JavaScript, though the two has very little in common.


How to use the JavaScript in the Web Page
The html element that recognises the script is <SCRIPT> </SCRIPT>. All the script comes under this element.

For example

<HTML>
<BODY>
<SCRIPT type="text/javascript"> // The type attribute of the script element is                                      used to specify the scripting language
document.write("JavaScript Tutorial")
</SCRIPT>
</BODY>
</HTML>

On executing this page it will display JavaScript Tutorial on the browser.


For the older version of the browser that does not support the JavaScript (as there are people who stick to the older versions of the browsers for their own reasons) the script in the above example will be displayed as the page content. To prevent the web page by doing so, HTML comment tag are used.


For example

<HTML>
<BODY>
<SCRIPT type="text/javascript">
<!--
document.write("JavaScript Tutorial")
//-->
</SCRIPT>
</BODY>
</HTML>

On executing this page it will display JavaScript Tutorial on the browser.


Note : In case your script is not able to execute on the web browser that does not support the JavaScript or fails for some scripting reasons it should have something so that the page is accessible to the user, in that case the code in the <NOSCRIPT> html tag will be executed.

For example

<HTML>
<BODY>
<SCRIPT type="text/javascript">
<!--
document.write("JavaScript Tutorial")
//-->
</SCRIPT>
<NOSCRIPT>
<p>This will not be displayed if JavaScript is enabled</p> </noscript>
</BODY>
</HTML>

On executing this page if the script fails then the code in the noscript tag is displayed


Where to place the <SCRIPT></SCRIPT> element in the html document
The script can be placed many number of times in HEAD or BODY section of html document. Certain guidelines can be followed while writing the script.

If we want the script to be executed at the certain event then it should be placed under the HEAD section

If we want the script to be executed as the page loads then it should be placed under the BODY section

Keep most of the script under the HEAD section as this helps to separate them from the actual page content.


Using the external file as the scripting file
When the script is to be used in more than one web page or is quite long then it is always good to write the script as the separate file and use that file in the web page where that script is required to be used.

For example

<HTML>
<HEAD>
<SCRIPT src="javascript1.js"></script>
</HEAD>
<BODY>
</BODY>
</HTML>

The above page uses the external script file javascript1.js as the external scripting file



Back - Tutorials Next - JavaScript Tutorial : Variable in JavaScript