What is JavaScript ?
Javascript is a dynamic computer programming language.
It is lightweight, and most commonly used as a part of web
pages, whose implementations allow client-side script to
interact with the user and make dynamic pages. It is an
interpreted programming language with object-oriented
capabilities.
JavaScript was first known as LiveScript, but Netscape
changed its name to JavaScript, possibly because of the
excitement being generated by Java. JavaScript made its
first appearance in Netscape 2.0 in 1995 with the
name LiveScript. The general-purpose core of the
language has been embedded in Netscape, Internet
Explorer, and other web browsers.
Client-side JavaScript
Client-side JavaScript is the most common form of the
language. It means that a web page need not be a static
HTML, but can include programs that interact with the user,
control the browser, and dynamically create HTML content.
For example, you might use JavaScript to check if the user
has entered a valid e-mail address in a form field.
The JavaScript code is executed when the user submits the
form, and only if all the entries are valid, they would be
submitted to the Web Server.
JavaScript can be used to trap user-initiated events such as
button clicks, link navigation, and other actions that the
user initiates.
Advantages of JavaScript
The merits of using JavaScript are −
Less server interaction − You can validate user input before
sending the page off to the server. This saves server traffic, which
means less load on your server.
Immediate feedback to the visitors − They don't have to wait
for a page reload to see if they have forgotten to enter something.
Increased interactivity − You can create interfaces that react
when the user hovers over them with a mouse or activates them via
the keyboard.
Richer interfaces − You can use JavaScript to include such items
as drag-and-drop components and sliders to give a Rich Interface to
your site visitors.
Limitations of JavaScript
We cannot treat JavaScript as a full-fledged programming
language. It lacks the following important features −
Client-side JavaScript does not allow the reading or writing of files.
This has been kept for security reason.
JavaScript cannot be used for networking applications because there
is no such support available.
JavaScript doesn't have any multithreading or multiprocessor
capabilities.
Inserting Java Script
JavaScript can be implemented using JavaScript statements
that are placed within the <script>... </script> HTML
tags in a web page.
There is a flexibility given to include JavaScript code
anywhere in an HTML document. However the most
preferred ways to include JavaScript in an HTML file are as
follows −
Script in <head>...</head> section.
Script in <body>...</body> section.
Script in <body>...</body> and <head>...</head> sections.
Script in an external file and then include in <head>...</head>
section.
The <script> tag alerts the browser program to start
interpreting all the text between these tags as a script. A
simple syntax of your JavaScript will appear as follows.
<script language="javascript" type="text/javascript">
JavaScript code
</script>
First JavaScript Script
<html>
<body>
<script language="javascript" type="text/javascript">
[Link]("Hello World!")
</script>
</body>
</html>
This code will produce the following result −
Hello World!
Case Sensitivity
JavaScript is a case-sensitive language. This means that
the language keywords, variables, function names, and any
other identifiers must always be typed with a consistent
capitalization of letters.
So the identifiers Time and TIME will convey different
meanings in JavaScript.
First JavaScript Program
<html>
<head>
<script type="text/javascript">
function sayHello() {
alert("Hello World")
}
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>
We use Java Script Function, Alert, [Link] in this class
The [Link]() method writes HTML expressions or JavaScript code to a document.
First JavaScript Program
<html>
<head>
<script type="text/javascript">
function sayHello() {
alert("Hello World")
}
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>
<html>
<body>
<script type="text/javascript">
<!--
var a = 33;
var b = 10;
var c = "Test";
var linebreak = "<br />";
[Link]("a + b = ");
result = a + b;
[Link](result);
[Link](linebreak);
[Link]("a - b = ");
result = a - b;
[Link](result);
[Link](linebreak);
[Link]("a / b = ");
result = a / b;
[Link](result);
[Link](linebreak);
[Link]("a % b = ");
result = a % b;
[Link](result);
[Link](linebreak);
[Link]("a + b + c = ");
result = a + b + c;
[Link](result);
[Link](linebreak);
//-->
</script>
Set the variables to different values and then try...
</body>
</html>
Output
a + b = 43
a - b = 23
a / b = 3.3
a % b = 3
a + b + c = 43
Set the variables to different values and then try...