You are on page 1of 22

Learning

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
do:

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

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
1.
Java was developed by Sun Microsystems and is
a full fledged, standalone, object oriented
language
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
2.

Java is both a compiled as well as interpreted
language

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

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

JavaScript is a loosely typed language as we do
not have declare data types of variables before
using them.
JavaScript V/s Java
5.
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
code.

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
ways:

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

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

// javascript code

</script>
</head>
.
.

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

// javascript code

</script>
</body>
.
.

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>
section.
External JavaScript
<html>
<head>
<script type=text/javascript src=javascript
file >
</script>
</head>
.
.

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

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

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
page.
Comments In JavaScript
Comments in JavaScript can be given in 2 ways:

1. Single line comment
Syntax:
// javascript code

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