Iwd Unit 4
Iwd Unit 4
Iwd Unit 4
UNIT 4
Shalini Shukla
JAVASCRIPT
JavaScript is a lightweight, interpreted programming language.
It is designed for creating network-centric applications.
Knows all about your page Applets are dimly aware of your Web
page.
Requires a client who trusts the server enough to run the code
the server provides
JavaScript has some protection in place but can still cause
security problems for clients
Remember JavaScript was invented in 1995 and web
browsers have changed a lot since then
USING JAVASCRIPT IN YOUR HTML
JavaScript can be inserted into documents by
using the SCRIPT tag
<html>
<head>
<title>Hello World in JavaScript</title>
</head>
<body>
<script type= "text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
WAY OF USING JAVASCRIPT
There are three places to put the JavaScript code.
Between the <body> </body> tag of html (Inline JavaScript)
Inline JavaScript
When java script was written within the html element using attributes
related to events of the element then it is called as inline java script.
Example of Inline JavaScript
<html>
<body>
<input type="button" value="Click"
<script type= "text/javascript">
onclick="alert('Button Clicked')"/> <!.... Method called..>
</script>
</body>
</html>
INTERNAL JAVASCRIPT
When java script was written within the section using element then it is called as
internal java script.
Example of Internal JavaScript
<html>
<head>
<script>
function msg()
{
alert("Welcome in JavaScript"); <!... Internal js…>
}
</script>
</head>
<form>
<input type="button" value="Click" onclick="msg()"/>
</form>
</html>
EXTERNAL JAVASCRIPT
Writing java script in a separate file with extension .js is
called as external java script.
For adding the reference of an External JavaScript file to
your html page, use tag with src attribute as follows
<script type="text/javascript" src="filename.js"/>
function msg()
{
alert("Welcome in JavaScript");
}
CREATE A HTML PAGE AND USE THE FILE FUNCTIONS.JS AS
FOLLOWS
<html>
<head>
<script type="text/javascript" src=" functions.js ">
</script>
</head>
<body>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
VARIABLE DECLARATION
Java script did not provide any data types for declaring variables and a variable
in java script can store any type of value.
Hence java script is loosely typed language. We can use a variable directly
without declaring it.
Only var keyword are use before variable name to declare any variable.
Syntax
var x;
Types of Variable in JavaScript
Local Variable
Global Variable
Local Variable
A variable which is declared inside block or function is called local variable. It
is accessible within the function or block only.
Global Variable
An important part of JavaScript is the ability to create new functions within <script> and </script>
tag. Declare a function in JavaScript using function keyword. The keyword function precedes the
name of the function.
Syntax
function functionName(parameter or not)
{.........
.........}
Example of JavaScript using Function
<html>
<head>
<script>
function getname()
{name=prompt("Enter Your Name");
alert("Welcome Mr/Mrs " + name); }
</script>
</head>
<form>
<input type="button" value="Click" onclick="getname()"/>
</form>
</html>
JAVASCRIPT ARRAYS
• JavaScript has arrays that are indexed starting at 0
<script type= "text/javascript">
var colors = new Array();
colors[0] = "red";
colors[1] = "green";
colors[2] = "blue";
colors[3] = "orange";
colors[4] = "magenta";
colors[5] = "cyan";
for (var i in colors) {
document.write("<div style=\"background-color:"
+ colors[i] + ";\">"
+ colors[i] + "</div>\n");
}
</script>
JAVASCRIPT DIALOG BOX
All JavaScript dialog box is a predefined function which is used for to
perform different-different task. Some function are given below;
Alert()
Alert function of java script is used to give an alert message to the user.
prompt()
Prompt function of java script is used to get input from the user.
confirm()
confirm function of java script is used to get confirmation from user
before executing some task.
ALERT FUNCTION EXAMPLE
<!doctype html>
<html>
<head>
<script>
function alertmsg()
{
alert("Alert function call");
}
</script>
</head>
<form>
<input type="button" value="Click Me“ onclick="alertmsg()"/>
</form>
</html>
JAVASCRIPT - DOCUMENT OBJECT MODEL OR DOM
Method Description
JavaScriptprovides facility to validate the form on the client-side so data processing will be faster
than server-side validation.
Through JavaScript, we can validate name, password, email, date, mobile numbers and more
fields.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>Please input a number between 1 and 10:</p>
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
// Get the value of the input field with id="numb"
let x = document.getElementById("numb").value;
// If x is Not a Number or less than one or greater than 10
let text;
if (isNaN(x) || x < 1 || x > 10) {
text = "Input not valid";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}</script></body></html>
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
STATEMENTS AND DECLARATIONS
JavaScript applications consist of statements with an appropriate syntax. A single
statement may span multiple lines. Multiple statements may occur on a single line if
each statement is separated by a semicolon. This isn't a keyword, but a group of
keywords.
Statements and declarations by category
Control flow
Block A block statement is used to group zero or more statements. The block is
delimited by a pair of curly brackets.
Break Terminates the current loop, switch, or label statement and transfers program
control to the statement following the terminated statement.
continue Terminates execution of the statements in the current iteration of the
current or labeled loop, and continues execution of the loop with the next iteration.
Empty An empty statement is used to provide no statement, although the JavaScript
syntax would expect one.
if...else Executes a statement if a specified condition is true. If the condition is false,
another statement can be executed.
switch Evaluates an expression, matching the expression's value to a case clause,
and executes statements associated with that case.
throw Throws a user-defined exception.
try...catch Marks a block of statements to try, and specifies a response, should an
exception be thrown.
DECLARATIONS
var Declares a variable, optionally initializing it to a value.
let Declares a block scope local variable, optionally
initializing it to a value.
const Declares a read-only named constant.
object={property1:value1,property2:value2.....propertyN:valueN}
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
2) BY CREATING INSTANCE OF OBJECT
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
3) BY USING AN OBJECT CONSTRUCTOR
Here, you need to create function with arguments.
Each argument value can be assigned in the current object by using this
keyword.
The this keyword refers to the current object.
<script>
function emp ( id, name , salary){
this.id=id;
this.name=name;
this.salary=salary;
}
var e=new emp(103,"Vimal Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>
JAVASCRIPT EVENTS
HTML events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript
can "react" on these events.
For example, when a user clicks over the browser, add js
code, which will execute the task to be performed on the
event.
Example
<button onclick="document.getElementById('demo').innerHTML =
Date()">The time is?</button>
COMMON HTML EVENTS
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
Keyboard events:
Key down & Key up
onkeydown & onkeyup events When the user press and then
release the key
FORM EVENTS:
concat() It returns a new array object that contains two or more merged arrays.
find() It returns the value of the first element in the given array that satisfies the
specified condition.
findIndex() It returns the index value of the first element in the given array that
satisfies the specified condition.
indexOf() It searches the specified element in the given array and returns the index
of the first match.
pop() It removes and returns the last element of an array.
slice() It returns a new array containing the copy of the part of the given array.