You are on page 1of 22


JavaScript Programming
Chapter 1
Java Script
(Client Side Scripting)
What Is Java Script ?

JavaScript is a weakly -typed, interpreted
programming language with object-oriented
capabilities that allows us to build interactive
web page.

What Java Script Can Do ?
Following are the tasks for which JavaScript is
mainly used:

1. User Interaction
2. Form Validation
3. Image Swaps/Rollovers

What Java Script Cant Do ?
Following are the tasks which JavaScript cannot

1. JavaScript cannot access Database(without
2. JavaScript cannot access files on client

JavaScript V/s Java

JavaScript and Java share two very similar names
but they are completely different languages that
have very few things in common.

JavaScript V/s Java
Java was developed by Sun Microsystems and is
a full fledged, standalone, object oriented
JavaScript was created by Netscape and is a
scripting language which placed inside an HTML
page and provides interactivity to the page.
JavaScript V/s Java

Java is both a compiled as well as interpreted

JavaScript is purely an interpreted language.
JavaScript V/s Java
Java runs inside JVM.

JavaScript runs inside web browser
JavaScript V/s Java
Java is a strongly typed language as it is
compulsory to declare every variables data

JavaScript is a loosely typed language as we do
not have declare data types of variables before
using them.
JavaScript V/s Java
Java has been designed with security as its top
priority. Every thing in java runs under the
control of JVM that handles execution of a Java

JavaScript is a much less secure language.
JavaScript History
Developed By : Brendan Eich
Original Name : Mocha, LiveScript
Company : Netscape
Launching Year : 1996
First Version : 1.0
Latest Version : 1.8
JavaScript Embedding
JavaScript can be embedded in HTML page in 2

1. Locally
2. Externally
Local JavaScript Embedding
To insert a JavaScript into an HTML page, use the
<script> tag.
The <script> and </script> tells where the
JavaScript starts and ends.

<script type=text/javascript>

// javascript code

Local JavaScript Examples
<script type=text/javascript>

// javascript code


Local JavaScript Examples
<head>. . . </head>
<script type=text/javascript>

// javascript code


Where to embed <script> tag ?
Although we can embed a <script> in both
<head> or <body> sections but a general
guideline is:
1. If we want to run the <script> before page
loads then it is kept in the <head> section.
2. If we want to run the <script> after the page
loads so that it can interact with HTML
elements then it is kept in the <body>
External JavaScript
<script type=text/javascript src=javascript
file >

<script type=text/javascript src=myfile.js >

Writing To The Document
One of the most basic JavaScript commands
is document.write. This simply prints the
specified text to the page.

document.write(text to display);
Special Note
Any document.write( ) statement that runs
after the page finishes loading will create a
new page and overwrite all of the content of
the current page.
Embedding HTML
The text outputted using document.write( )
statement can contain HTML tags also.

The browser processes them in the same way
like it processes html tags in a regular HTML
Comments In JavaScript
Comments in JavaScript can be given in 2 ways:

1. Single line comment
// javascript code

2. Multi line comment
/* javascript code
javascript code */