java script 22
java script 22
Introduction
What is JavaScript
This is a scripting language that adds programmatic capabilities on the client
end. It is an Object Based language unlike its counterpart the normal Java
Object oriented language.
It solely employs the use of objects, properties of objects and method in
programming.
Similar to other scripting languages Java Script is light weight. It does not
require a compiler because it is an interpreted language.
JavaScript's may be embedded directly into HTML or linked as an external
file.
Executing JavaScript's
Java scripts usually execute in the context of a webpage on a client's
computer.
They require a web browser that is java enabled to interpret java scripts
embedded in the web page. JavaScript's are renown for altering the
appearance of the document, control the display, validate and manipulate
form elements, animation of graphics, storing browsing histories and perform
general computational tasks.
Having learnt JavaScript however its capabilities will only be limited by your
creativity.
The following are some of the capabilities of
• JavaScript gives HTML designers a programming tool
HTML authors are normally not programmers, but JavaScript is a scripting
language with a very simple syntax! Almost anyone can put small "snippets"
of code into their HTML pages.
• JavaScript can put dynamic text into an HTML page
A JavaScript statement like this: document.write("<h1>+ name + "</h1>")
can write a variable text into an HTML page.
• JavaScript can react to events
A JavaScript can be set to execute when something happens, like when a
page
has finished loading or when a user clicks on an HTML element.
• JavaScript can read and write HTML elements
A JavaScript can read and change the content of an HTML element.
• JavaScript can be used to validate data
A JavaScript can be used to validate form data before it is submitted to a
server. This saves the server from extra processing.
• JavaScript can be used to detect the visitor's browser
A JavaScript can be used to detect the visitor's browser, and - depending on
the browser load another page specifically designed for that browser.
• JavaScript can be used to create cookies
A JavaScript can be used to store and retrieve information on the visitor's
computer
In the following example we are using the length property of the String
object to return the number of characters in a string which in this case is 24
counting all the characters in the string:
Similarly Objects are associated to methods that represent the functions the
object will perform. Consider another example of a script that is making use
of two methods
In the following example we are using the toUpperCase() method of the
String object to display a text in uppercase letters and the write method of
the document to display a web output.
<script type="text/javascript">
varstr="Welcome to JavaScript Programming!"; document.write(st</script>
The output of the code above will be:
WELCOME TO JAVASCRIPT PROGRAMMING!
JavaScripts in a page will be executed immediately while the page loads into
the browser. Sometimes we want to execute a script when a page loads,
other times when a user triggers an event.
Scripts in <head>
Scripts to be executed when triggered, go in the head section.
You can create some form of a trigger in the body section in the form of
controls that when activated will call the scripts in the head. It is common
practice to have functions in this section.
When the page is loaded in memory the script will also load but will not
execute until a trigger initiates it.
<html>
<head>
<script type="text/JavaScript">
function message()
{
alert("This alert box was called with an onload event residing in the head
section as a function message");}
</script>
</head>
<body onload="message()">
</body>
</html>
JavaScript Objects
Because most of the JavaScript code that you write involves objects,
properties, methods, and event handlers, understanding what these object-
oriented terms mean is essential for folks planning to write their own scripts.
You can think of it this way:
• Objects are always nouns.
• Properties are adjectives.
• Methods are verbs.
• Event handlers are verbs with on tacked to their fronts
Variables
• JavaScript variables are used to hold values or expressions.
• A variable can have a short name, like x, or a more descriptive name, like
carname.
Rules for JavaScript variable names:
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character
• Note: Because JavaScript is case-sensitive, variable names are case-
sensitive.
• Creating variables in JavaScript is most often referred to as "declaring"
variables.
• You can declare JavaScript variables with the var statement:
var x;
varcarname;
After the declaration shown above, the variables are empty (they have no
values yet).
However, you can also assign values to the variables when you declare
them:
var x=5;
varcarname="Volvo";
Example 2
<html>
<head>
<script type="text/javascript">
function product(a,b)
{ return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
Javascript Pop up Boxes
The hallmark of JavaScript lies in the ability of javascript pop ups
_ Alert Box
• An alert box is often used if you want to make sure information comes
through to the user.
• When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
alert("sometext"); i.e.
<html>
<head>
<script type="text/javascript">
function show alert()
{ alert("I am analertbox!");
}
</script>
</head>
<body>
<input type="button" onclick="show alert()" value="Show
alert box" />
</body>
</html>
Confirm box
• A confirm box is often used if you want the user to verify or accept
something.
• When a confirm box pops up, the user will have to click either "OK" or
"Cancel" to proceed.
• If the user clicks "OK", the box returns true. If the user clicks "Cancel", the
box returns false.
Syntax
confirm("sometext"); i.e.
<html>
<head>
<script type="text/javascript">
function show confirm()
{
var r=confirm("Press a button");
if (r==true)
{
document.write("You pressed OK!");
}
else f document.write("You pressed Cancel!");
}}
</script>
</head>
<body>
<input type="button"onclick="show confirm()"value="Show confirm box" />
</body>
</html>
_ Prompt box
A prompt box is often used if you want the user to input a value before
entering a page. When a prompt box pops up, the user will have to click
either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.
Syntax
prompt("sometext","defaultvalue");
Revision Questions
Example _. Write a JavaScript statement to accomplish each of the following
tasks
i. Declare variables sum and x.
ii. Assign 1 to variable x.
iii. Assign 0 to variable sum
iv. Add variable x to variable sum and assign the result to variable sum
v. Print \The sum is" followed by the value of variable sum
Solution:
i. Declare variables sum and x. var x; var sum;
ii. Assign 1 to variable x. x=1 ; or var x = 1;
iii. Assign 0 to variable sum. sum =0 ; or var sum =0;
iv. Add variable x to variable sum and assign the result to variable sum. sum
= xAssignments + sum;
v. Print \The sum is" followed by the value of variable sum.document.write(\
The sum is"+sum);