WT-Module-2 Part-1
WT-Module-2 Part-1
Dontha Madhusudhana
Rao
Department of Computer Science &
Engineering
2
User Interface
Design
JavaScript
What is JavaScript ?
Brendan Eich first developed JavaScript in May
1995. The language, formerly known as
Mocha, later modified to LiveScript, and is
now known simply as JavaScript, was created
to be used on the client-side of websites,
enabling the addition of dynamic and
interactive components to static HTML texts.
making it responsive to events like the
movement of the mouse, mouse click on a
certain HTML element, a button click, etc.,
using which we can improve the user
experience.
JavaScript is also being used widely in game
development and Mobile application
development.
/*
This is a multiline comment and everything written
inside this block will not be executed.
*/
JavaScript Statements
</script>
</body>
</html>
<html>
<head>
<title>JavaScript</title>
</head>
<body>
<script>
window.alert("This is an alert message.") ;
</script>
</body>
</html>
<html>
<head>
<title>JavaScript</title>
<script>
console.log(“Madhu");
</script>
</head>
<body>
</body>
</html>
JavaScript Variables
Variables
1. Using var
2. Using let
3. Using const
Rules for Variable name
Variable names cannot contain spaces.
const price1 = 5;
const price2 = 6;
let total = price1 + price2;
JavaScript Data Types
JavaScript Primitive Data
Types
var str = "Ferari F60";
let str1 = 'Volvo S60';
String Data Type
let x = true;
var y = false;
Boolean Data Type
var x = 45;
var y = 45.90;
Number Data Type var z = -10;
JavaScript Composite Data types
Operator Description
&& Logical
AND
|| Logical OR
! Logical Not
Bitwise Operators
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
String Operators
Example:
let result = (10 > 0) ? true : false;
JavaScript Control Statements
<html>
<body>
<script>
let a = 8;
if(a < 8)
{
document.write("Number is less than 8");
}
else if (a = 8)
{
document.write("Number is equal to 8");
}
else {
document.write("Number is greater than 8");
}
</script>
</body>
</html>
<html>
<body>
<script>
for(let a = 0; a < 10; a++)
{
document.write("Current value : " + a);
document.write("<br />");
}
</script>
</body>
</html>
<html>
<head>
<script>
let a = 9;
while (a <= 10)
{
document.write("while executed <br/>");
a++;
}
</script>
</head>
<body>
</body>
</html>
<html>
<body>
<script>
let a = 10;
do
{
document.write("do executed <br/>");
a++;
}
while(a < 12)
</script>
</body>
</html>
<html>
<head>
<script>
let a = 1;
switch (a)
{
case 1: alert('case 1 has been executed');
break;
case 2:
alert("case 2 has been executed");
break;
default:
alert("default case executed");
}
</script>
</head>
</html>
Write a JavaScript program to determine if a
number is even or odd
var fruits = [ ];
fruits[0] = "apple";
fruits[1] = "banana";
fruits[3] = "orange";
<html>
<head>
<script>
document.write(fruits.length);
</script>
</head>
</html>
<html>
<head>
<script>
var fruits = [ ];
fruits[0] = "apple";
fruits[1] = "banana";
fruits[3] = "orange";
alert(fruits.length);
</script>
</head>
</html>
<html>
<head>
<script>
var fruits = [ ];
fruits[0] = "apple";
fruits[1] = "banana";
fruits[3] = "orange";
document.write (fruits[2]);
</script>
</head>
</html>
<html>
<head>
<script>
var fruits = [ ];
fruits[0] = "apple";
fruits[1] = "banana";
fruits[3] = "orange";
document.write (fruits);
</script>
</head>
</html>
JavaScript Functions
JavaScript Functions, just like in any other language,
is a set of statements that are used to perform a
specific task, like adding two numbers, finding the
square of a number, or any user-defined task.
Structure of a JavaScript function
function keyword
function name
return output;
} giving output using return keyword
<html>
<head>
<script>
function add(a, b)
{
document.write(a+b);
}
add(8,8);
</script>
</head>
</html>
<html>
<head>
<script>
function add(a, b)
{
document.write(a+b);
}
add(7004,“Madhu”);
</script>
</head>
</html>
<html>
<head>
<script>
function add(a, b)
{
return a+b;
}
let result = add(8,8);
document.write(result);
</script>
</head>
</html>
<html>
<head>
<script>
function add(a, b)
{
document.write(a+b);
}
let a = parseInt(prompt("Enter number 1"));
let b = parseInt(prompt("Enter number 2"));
add(a,b);
</script>
</head>
</html>
<html>
<head>
<script>
function msg()
{
alert("Web Technologies");
}
</script>
</head>
<body>
<form>
<input type="button" onclick="msg()" value="call function">
</form>
</body>
</html>
Write a JavaScript conditional statement to find
the sign of product of three numbers using
functions
Sample Output :
"0 is even"
"1 is odd"
"2 is even“
“3 is odd“
……............
“15 is odd”
Write a JavaScript program which iterates the
integers from 1 to 100.
<form name="addition">
Number 1:<input type="text" id="one" name="num1"><br>
Number 2:<input type="text" id="two" name="num2">
<br><br>
<input type="button" onclick="add()" value="Calculate">
</form>
<head>
<script>
function add()
{
var a = parseInt(document.getElementById("one").value);
var b = parseInt(document.getElementById("two").value);
alert(a+b);
}
</script>
</head>
<head>
<script>
function add()
{
var a = parseInt(document.getElementById("one").value);
var b = parseInt(document.getElementById("two").value);
var result = a+b;
document.getElementById("details").innerHTML = result;
}
</script>
</head>
<body>
<p id="details"></p>
</body>
getElementsByName( )
<body>
<form name="details">
<p>First Name: <input type="text" name="fname"></p>
<p>Last Name: <input type="text" name="fname"></p>
<input type="button" onclick="read()" value="Display">
</form>
<br>
<p id="details"></p>
</body>
<head>
<script>
function read()
{
let elements = document.getElementsByName("fname");
document.getElementById("details").innerHTML = elements[0].value;
}
</script>
</head>
getElementsByTagName( )
<body>
<form name="addition">
Number 1:<input type="text" name="num1"><br>
Number 2:<input type="text" name="num2"> <br><br>
<input type="button" onclick="add()" value="Display">
</form>
<p id="details"></p>
</body>
<head>
<script>
function add()
{
let elements = document.getElementsByTagName("input");
document.getElementById("details").innerHTML = elements[0].value;
}
</script>
</head>
getElementsByClassName( )
<body>
<form name="addition">
Number 1:<input type="text" class="one" name="num1"><br>
Number 2:<input type="text" class="one" name="num2">
<br><br>
<input type="button" onclick="add()" value=“Calculate">
</form>
</body>
<script>
function add()
{
let elements = document.getElementsByClassName("one");
let a = parseInt(elements[0].value);
let b = parseInt(elements[1].value);
alert(a+b);
}
</script>
HTML Form Validations
JavaScript Validations
<script>
let text = “Amrita University";
let pattern = /Amrita/;
let result = text.match(pattern);
document.getElementById(“value").innerHTML = result;
</script>
regular expression for Email
/[a-zA-Z0-9._-] +@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
/[a-zA-Z0-9._-] +@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
</body>
function validateEmail(inputtext)
{
var mailformat = /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
if(inputtext.value.match(mailformat))
{
alert("Valid email address!");
return true;
}
else
{
alert("You have entered an invalid email address!");
return false;
}
JavaScript Events
The change in the state of an object is
known as an Event.
Mouse Events
Keyboard Events
Form Events
Mouse Events
Event Event Handler Description
Performed
click onclick When mouse click on an element