WT R16 Unit 2 Java Script PDF
WT R16 Unit 2 Java Script PDF
Script means small piece of code. Scripting languages are two kinds one is client-
side other one is servers-side scripting. In general client-side scripting is used for
verifying simple validation at client side, server-side scripting is used for database
verifications. VBScript, java script and J script are examples for client-side scripting
and ASP, JSP, Servlets, PHP etc. are examples of server-side scripting.
JavaScript (originally known as "LiveScript") is a scripting language that runs
inside the browser to manipulate and enhance the contents of Web pages. Java Script is
designed to add interactivity to HTML pages. Web pages are two types
Static web page
Dynamic webpage
❖ Static web page where there is no specific interaction with the client
❖ Dynamic web page which is having interactions with client and as well as
validations can be added.
Simple HTML script is called static web page, if you add script to HTML page it is called
dynamic page. Netscape navigator developed java script. Microsoft’s version of
JavaScript is Jscript.
❖ Java script code as written between <script>-----</script>tags
❖ All java script statements end with a semicolon
❖ Java script ignores white space
❖ Java script is case sensitive language
❖ Script program can save as either. Js or. html
Benefits of JavaScript
❖ It is widely supported by web browsers;
❖ It gives easy access to the document objects and can manipulate most of them.
❖ Java Script gives interesting animations with long download times associated
with many multimedia data types;
❖ Web surfers don’t need a special plug-in to use your scripts
❖ Java Script relatively secure - you can’t get a virus infection directly from Java
Script.
❖ JavaScript code resembles the code of C Language; the syntax of both
the language is very close to each other. The set of tokens and constructs are
same in both the language.
Comments in JavaScript:
Single line comment- //
Multi-line comment- <!-- comment -->
Operators in JavaScript:
❖ Arithmetic operators (+,-,*,/,%)
❖ Relational operators (<,>,!=,<=,>=)
❖ Logical operators(&&,||,!)
❖ Assignment operator(=)
❖ Increment decrement operators(++,- -)
❖ Conditional/Ternary operator(?:)
❖ Bitwise operators(&,|,!)
Control structures:
❖ If statement
❖ Switch
❖ While
❖ Do-while
❖ For
❖ Break
❖ Continue
Control structures syntax and working as same as java language.
Variables
Variables are like storage units/place holders to hold values. A variable is a memory
location to hold certain different types of data. In Javascript, A variable can store all
kinds of data. It is important to know the proper syntax to which variables must
conform:
❖ They must start with a letter or underscore ("_")
❖ Subsequent characters can also be digits (0-9) or letters (A-Z and/or a-z).
Remember, JavaScript is case-sensitive. (That means that MyVar and myVar are
two different names to JavaScript, because they have different capitalization.)
❖ You cannot use reserved words as variable names.
❖ You cannot use spaces in names.
❖ Names are case-sensitive.
Syntax:
var v_name = value;
Examples of legal variable names are fname, temp99, and _name.
Functions
Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript
procedure -a set of statements that performs a specific task when called. A function definition
has these basic parts:
❖ The function keyword
❖ A function name
❖ A comma-separated list of arguments to the function in parentheses
❖ The statements in the function in curly braces: { }
Defining a Function
Defining the function means, name the function and specifies what to do when the
function is called. You define a function within the <SCRIPT>...</SCRIPT> tags within
the <HEAD> ... </HEAD> tags. While defining a function, you can also declare the
variables which you will be calling in that function. Here's an example of defining a
function:
function msg()
{
window.alert(“This is an alert box.”);
}
Here's an example of a function that takes a parameter:
function welcome(string)
{
window.alert(“Hi”+string);
}
When you call this function, you need to pass a parameter (such as the word that the
user clicked on) into the function.
Calling a Function
Calling the function actually performs the specified actions. When you call a function, this is
usually within the BODY of the HTML page, and you usually pass a parameter into the
function on which the function will act.
Here's an example of calling the same function:
msg();
For the other example, this is how you may call it:
<inupt type="button" name=”welcome” onClick="msg1()"/>
height=pixels, width=pixels : These properties can be used to set the window size.
The following code shows how to open a new window
newWin = open(“first.html”,”newWin”,”status=0,toolbar=0,width=100,height=100”);
Alert box is used to display warning/error messages to user. It displays a text string
with OK button.
Syntax: window. Alert (“Message”);
Confirm Box is useful when submitting form data. This displays a window containing message with two
buttons: OK and Cancel. Selecting Cancel will abort the any pending action, while OK will let the action
proceed.
Syntax
window.confirm(“String”);
Prompt box used for accepting data from user through keyboard. This displays simple
window that contains a prompt and a text field in which user can enter data. This
method has two parameters: a text string to be used as a prompt and a string to use as
the default value. If you don‟t want to display a default then simply use an empty string.
Syntax
Variable=window.prompt(“string”,”default value”);
onReset = “method()”
This event can only be triggered by form itself and occurs when a form is reset.
Example: HTML program that applies a random background color when you
click on button
<html>
<head>
<script language = "javascript">
function change()
{
var clr = document.bgColor=parseInt(Math.random()*999999);
document.f1.color.value=clr;
}
</script>
</head>
<body>
<form name="f1">
<input type="text" name="color"/>
<input type="button" value="Cick me" onclick="change()"/>
</form> </body></html>
Fig.1: On first run background is white Fig.2: After clicking on button”Click me”:
background changed to “black”
The browser Object
The browser is JavaScript object that can be used to know the details of
browser. Some of the properties of the browser object is as follows:
Property Description
It returns the internal name for the browser. For major
navigator.appCodeName
browsers it is Mozilla
It returns the public name of the browser – navigator or
navigator.appName Internet Explorer
navigator.appVersion It returns the version number, platform on which the
browser is running.
navigator.userAgent The strings appCodeName and appVersion concatenated
together
navigator.plugins An array containing details of all installed plug-ins
Navigator.mimeTypes An array of all supported MIME Types
• Event Handling is a software routine that processes actions, such as keystrokes and mouse movements.
• It is the receipt of an event at some event handler from an event producer and subsequent processes.