Chapter Three: Javascript
Chapter Three: Javascript
JavaScript
Introduction
• JavaScript is used in millions of Web pages to improve the design, validate
forms, detect browsers, create cookies, …
• It is the most popular scripting language on the internet, and works in all
major browsers
• Scripting languages are usually interpreted rather than compiled
• Programs written in interpreted languages must be translated into machine
code every time they are run, they are typically slower than compiled
programs.
• Being interpreted does have its advantages:
• platform independence: the interpreter is built into Web browsers.
• often loosely typed and more forgiving than compiled languages
Purposes of JavaScript
• used to come out of a loop without reaching at its bottom or to skip a part of the code
block and want to start next iteration of the look when needed by users or programmers.
• To handle all such situations, JavaScript provides break, label and continue statements.
• These statements are used to immediately come out of any loop or to start the next
iteration of any loop.
• The predefined Math object has properties and methods for mathematical constants
and functions.
• For example, the Math object’s PI property has the value of pi (3.141...), which we would use in
an application as Math.PI.
• new Date(yy,mm,dd,hh,mm,ss)
• new Date(yy,mm,dd)
Managing Events
• Events are user and browser activities to which we may respond dynamically
with a scripting language like JavaScript.
• The event that we used can be summarized as :-
• A mouse click
• Moussing over a hot spot on the web page
• Selecting an input box in an HTML form
• Submitting an HTML form
• A keystroke
• Example events:
• onabort - Loading of an image is interrupted
• onblur - An element loses focus
• ondblclick - Mouse double-clicks an object
• onclick - Mouse clicks an object
<head>
<script language="JavaScript">
function adder(num1, num2){
var sum = 0;
sum = num1 + num2;
document.write("The sum is " + sum);
}
function subtractor(num1, num2){
var difference = 0;
difference = num1 - num2;
document.write("The difference is " + difference);
}
</script>
</head>
<body>
<form name="event_example">
<input type="button" value="add" name="add" onClick="adder(10,30)">
<input type="button" value="subtract" name="subtract"
onClick="subtractor(20,50)">
</form>
</body>
Form Processing and Validation
• Forms are widely used on the Internet.
• The form input is often sent back to the server or mailed to a certain e-
mail account.
• With the help of JavaScript, the form input can easily be checked
before being sent.
• It is sent only if the input is valid
• Form data that typically are checked by a JavaScript could be:
• has the user left required fields empty?
• has the user entered a valid e-mail address?
• has the user entered a valid date?
• has the user entered text in a numeric field?
Validation example
<script>
function check (formName){
if (formName.urname.value == "")
alert("Please enter a string as your name!")
else
alert("Hi " + formName.urname.value + "! Name ok!");
if(formName.age.value < 0 || formName.age.value=="")
alert("Age should be number and greater than 0");
else
alert("Age ok");
}
</script>
<form name="first">
Enter your name: <input type="text" name="urname"> <br>
Enter your age: <input type="text" name="age"> <br>
<input type="button" name="validate" value="CheckInput “onClick ="check(this.form)">
Validation ….
• Basic Validation - First of all, the form must be checked to make sure data was entered into
each form field that required it.
• This would need just loop through each field in the form and check for data.
• Data Format Validation - Secondly, the data that is entered must be checked for correct
form and value.
• Example:
• Email validation: check presence of @ and period (.) in the input