Unit Iv Web Design
Unit Iv Web Design
UNIT IV:
▪ Client side scripting is a process in which the code along with HTML
web page is sent to the client by the server.
▪ Here, the code refers to the script. client side scripting is a process in
which scripts are executed by browsers without connecting the server.
▪ The code executes on the browser of client’s computer either during the
loading of web page or after the web page has been loaded.
▪ Client side scripting is mainly used for dynamic user interface elements,
such as pull-down menus, navigation tools, animation buttons, data
validation purpose, etc.
▪ JavaScript and jQuery are by far the most important client-
side scripting languages or web scripting languages and widely used
to create a dynamic and responsive webpage and websites.
▪ The browser (temporarily) downloads the code in the local computer
and starts processing it without the server. Therefore, the client side
scripting is browser dependent.
▪ A client-side script is a small program (or set of instructions) that is
embedded (or inserted) into a web page. It is processed within the client
browser instead of the web server.
1
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
▪ The client side script downloads at the client end from the server along
with the HTML web page it is embedded in. The web browser interprets
and executes the code and then displays the results on the monitor.
▪ The script that executes on the user’s computer system is called client.
It is embedded (or inserted) within the HTML document or can be
stored in an external separate file (known as external script).
▪ The script files are sent to the client machine from the web server (or
severs) when they are requested. The client’s web browser executes the
script, then displays the web page, including any visible output from
the script.
2
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
SERVER-SIDESCRIPTING :
▪ It can also access the file system residing at the webserver. A server-
side environment that runs on a scripting language is a web server.
3
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
4
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
JAVASCRIPT
JavaScript was first known as LiveScript, but Netscape changed its name to
JavaScript, possibly because of the excitement being generated by Java. JavaScript
made its first appearance in Netscape 2.0 in 1995 with the name LiveScript. The
general-purpose core of the language has been embedded in Netscape, Internet
Explorer, and other web browsers.
5
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
FEATURES OF JAVASCRIPT
• JavaScript runs directly in the browser, reducing the need for server
communication.
7
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
6. Cross-Browser Compatibility
• Works across all modern browsers (Chrome, Firefox, Safari, Edge, etc.)..
• Ideal for real-time applications like chat apps and online gaming.
8
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<script type="text/javascript">
</script>
9
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
OUTPUT
To call function, you need to work on event. Here we are using onclick event
to call msg() function.
<html>
<head>
<script type="text/javascript">
function msg()
{
alert("EXAMPLE FOR JS FUNCTION");
}
</script>
</head>
<body>
<p>Welcome to JS using Function</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
10
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
OUTPUT
JAVASCRIPT COMMENT
o To avoid the unnecessary code It can also be used to avoid the code
being executed. Sometimes, we add the code to perform some action.
But after sometime, there may be need to disable the code. In such case,
it is better to use comments.
1. Single-line Comment
2. Multi-line Comment
11
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Example
<script>
// It is single line comment
document.write("hello javascript");
</script>
Example
<script>
var a=10;
var b=20;
var c=a+b;//It adds values of a and b variable
document.write(c);//prints sum of 10 and 20
</script>
Example
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
JAVASCRIPT VARIABLE
They can be used in places where the values they represent are unknown
when the code is written.
✓ Variables as Code Clarifies
They can make the purpose of your code cleaner.
There are some rules while declaring a JavaScript variable (also known as
identifiers).
Declaring Variables:
13
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
To declare text as a variable, you can use the "var" or "let" keyword. The
Following syntax is used for declaring a variable in JavaScript:
Syntax:
Var variable_name;
For Example:
var total_amount;
For assigning a value to a variable, you can use the JavaScript assignment operator
(=).
Syntax:
For Example:
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Output
30
Types of Variables:
1. Local variable
2. Global variable
1.Local variable
<script>
function abc()
{
var x=10;//local variable
}
</script>
Or,
15
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<script>
If(10<13)
{
var y=20;//JavaScript local variable
}
</script>
2.Global variable
<script>
var data=200;//global variable
function a()
{
document.writeln(data);
}
function b()
{
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
Output
200 200
16
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Example:
<script type="text/javascript">
function funl()
{
window.value=100;//declaring global variable by window object
}
function fun2()
{
funl();
alert("Value accessed at fun2 ="+window.value);// accessing global variable from
other function
}
fun2();
</script>
OUTPUT
JAVASCRIPT LITERALS
Fixed values are called literals. Numbers are written with or without decimals
like 23.4, 234 while Strings are written within double or single quotes like "Rama",
"Rama'.
Syntax:
17
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Variables created by the const keyword are immutable (i.e.) we are unable to
reassign them to different values else will rise error message.
Example:
const PI = 3.14
Example:
18
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
S.No. Datatype
1. String
Can contain groups of character as single value. It is represented in double
quotes.E.g. var x= “tutorial”.
2. Numbers
Contains the numbers with or without decimal. E.g. var x=44, y=44.56;
3. Booleans
Contain only two values either true or false. E.g. var x=true, y= false.
4. Undefined
Variable with no value is called Undefined. E.g. var x;
5. Null
If we assign null to a variable, it becomes empty. E.g. var x=null;
EXAMPLE
1.String
Output
Hello There
19
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
let n1 = 2;
console.log(n1)
let n2 = 1.3;
console.log(n2)
let n3 = Infinity;
console.log(n3)
2
1.3
Infinity
NaN
3.Booleans
let b1 = true;
console.log(b1);
let b2 = false;
console.log(b2);
Output
true
false
4.Undefined
let a;
console.log(a);
Output
Undefined
20
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
5. Null
let age = null;
console.log(age)
Output
Null
2. Non-primitive (reference) data type
1. Array
Can contain groups of values of same type. E.g. var x={1,2,3,55};
2. Objects
Objects are stored in property and value pair. E.g. var rectangle = { length: 5,
breadth: 3};
1.ARRAYS
let a1 = [1, 2, 3, 4, 5];
console.log(a1);
let a2 = [1, "two", { name: "Object" }, [3, 4, 5]];
console.log(a2);
2.OBJECTS
let gfg = {
type: "Company",
location: "Noida"
}
console.log(gfg.type)
OUTPUT
Company
21
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
JAVASCRIPT OPERATORS
Operators are used to perform operation on one, two or more operands. Operator
is represented by a symbol such as +, =, *, % etc. Following are the operators
supported by java script
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
• Bitwise Operators
1.Arithmatic Operators
22
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Example
<script>
let x = 5;
// addition operator
document.write("Addition: x + 3 = ", x + 3);
// subtraction operator
document.write("Subtraction: x - 3 =", x - 3);
// multiplication operator
document.write("Multiplication: x * 3 =", x * 3);
// division operator
document.write("Division: x / 3 =", x / 3);
// remainder operator
document.write("Remainder: x % 3 =", x % 3);
// increment operator
document.write("Increment: ++x =", ++x);
23
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
// decrement operator
document.write("Decrement: --x =", --x);
// exponentiation operator
document.write("Exponentiation: x ** 3 =", x ** 3);
</script>
OUTPUT
Addition: x + 3 = 8
Subtraction: x - 3 = 2
Multiplication: x * 3 = 15
Division: x / 3 = 1.6666666666666667
Remainder: x % 3 = 2
Increment: ++x = 6
Decrement: --x = 5
Exponentiation: x ** 3 = 125
This operatos are used to compares the values of two given opearands and gives the
result as Boolean(true/false)
24
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Example:
// equal to operator
console.log("Equal to: 2 == 2 is", 2 == 2);
OUTPUT
3.Logical Operators
This operatos are used to compares the values of two given relational expression and
gives the result as Boolean(true/false). It also used to combine more than one
relational expression.
26
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
&& Logical AND operator returns true if both operands 10 && 10 will give
are non zero. true.
! Logical NOT operator complements the logical ! (10 && 10) will
state of its operand. give false.
let x = 3;
// logical AND
console.log((x < 5) && (x > 0)); // true
console.log((x < 5) && (x > 6)); // false
// logical OR
console.log((x > 2) || (x > 5)); // true
console.log((x > 3) || (x < 0)); // false
// logical NOT
console.log(!(x == 3)); // false
console.log(!(x < 2)); // true
27
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
4.Assignment Operators
28
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
// assignment operator
let a = 7;
console.log("Assignment: a = 7, a =", a);
OUTPUT
29
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Assignment: a = 7, a = 7
Addition Assignment: a += 5, a = 12
Subtraction Assignment: a -= 5, a = 7
Multiplication Assignment: a *= 2, a = 14
Division Assignment: a /= 2, a = 7
Remainder Assignment: a %= 2, a = 1
Exponentiation Assignment: a **= 7, a = 1
EXAMPLE
OUTPUT
pass
30
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
6.BITWISE OPERATORS
&(Bitwise AND) :Performs Boolean AND operation on each bit of its operands.
^ (Bitwise XOR) :Performs Boolean XOR operation on each bit of its operands.
<<(Left Shift) : It moves all the bits in its first operand to the left by the number of
places specified as the second operand. Empty bits are filled with zeros
>> (Right Shift) :It moves all the bits in its first operand to the right by the number
of places specified as the second operand Empty bits are filled with zeros.
31
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
FUNCTIONS IN JAVASCRIPT
1.Number Methods
The Number object contains only the default methods that are part of every
object's definition.
Method & Description
constructor()
Returns the function that created this object's instance.
By default this is the Number object.
toPrecision()
toString()
Returns the string representation of the number's value.
valueOf()
Returns the number's value.
2.STRING METHODS
concat()
Combines the text of two strings and returns a new string.
indexOf()
Returns the index within the calling String object of the first
occurrence of the specified value, or -1 if not found.
32
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
lastIndexOf()
Returns the index within the calling String object of the last
occurrence of the specified value, or -1 if not found.
length()
Returns the length of the string.
replace()
Used to find a match between a regular expression and a string, and to replace
the matched substring with a new substring.
earch()
Executes the search for a match between a regular expression and a specified string.
substr()
Returns the characters in a string beginning at the specified location
through the specified number of characters.
substring()
Returns the characters in a string between two indexes into the string.
toLocaleLowerCase()
The characters within a string are converted to lower case while respecting the current
locale.
toLowerCase()
Returns the calling string value converted to lower case.
toString()
Returns a string representing the specified object.
33
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
toUpperCase()
Returns the calling string value converted to uppercase.
3.Array Methods
concat()
1 Returns a new array comprised of this array joined with other array(s) and/or
value(s).
every()
2
Returns true if every element in this array satisfies the provided testing function.
filter()
3 Creates a new array with all of the elements of this array for which the provided
filtering function returns true.
forEach()
4
Calls a function for each element in the array.
indexOf()
5 Returns the first (least) index of an element within the array equal
to the specified value, or -1 if none is found.
join()
6
Joins all elements of an array into a string.
4.Date Methods
Date()
1
Returns today's date and time
2 getDate()
34
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Returns the day of the month for the specified date according to local time.
getDay()
3
Returns the day of the week for the specified date according to local time.
getFullYear()
4
Returns the year of the specified date according to local time.
getHours()
5
Returns the hour in the specified date according to local time.
5.Math Methods
abs()
1
Returns the absolute value of a number.
acos()
2
Returns the arccosine (in radians) of a number.
asin()
3
Returns the arcsine (in radians) of a number.
atan()
4
Returns the arctangent (in radians) of a number.
atan2()
5
Returns the arctangent of the quotient of its arguments.
ceil()
6
Returns the smallest integer greater than or equal to a number.
cos()
7
Returns the cosine of a number.
exp()
8 Returns EN, where N is the argument, and E is Euler's constant, the base of the
natural logarithm.
35
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
floor()
9
Returns the largest integer less than or equal to a number.
log()
10
Returns the natural logarithm (base E) of a number.
<script>
function sum(x, y)
{
return x + y;
}
document.write(sum(6, 9));
</script>
OUTPUT
15
36
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Function Parameters
Parameters are input passed to a function. In the above example, sum() takes
two parameters, x and y.
Calling Functions
After defining a function, the next step is to call them to make use of the
function. We can call a function by using the function name separated by the value
of parameters enclosed between the parenthesis.
<script>
// Function Definition
function welcomeMsg(name)
{
return ("Hello " + name + " welcome to Functions");
}
</script>
OUTPUT
37
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<script>
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>
OUTPUT 7
38
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
1.JavaScript If-else
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
SYNTAX
if(expression)
{
//content to be evaluated
}
EXAMPLE
<script>
var a=20;
if(a>10)
{
document.write("value of a is greater than 10");
}
</script>
OUTPUT
If...else Statement
SYNTAX
if(expression)
{
//content to be evaluated if condition is true
}
Else
{
//content to be evaluated if condition is false
}
EXAMPLE
<script>
var a=20;
if(a%2==0)
{
document.write("a is even number");
}
Else
{
document.write("a is odd number");
}
</script>
OUTPUT
a is even number
If...else if statement
It evaluates the content only if expression is true from several expressions.
The signature of JavaScript if else if statement is given below.
SYNTAX
if(expression1)
{
//content to be evaluated if expression1 is true
40
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
}
else if(expression2)
{
//content to be evaluated if expression2 is true
}
else if(expression3)
{
//content to be evaluated if expression3 is true
}
Else
{
//content to be evaluated if no expression is true
}
EXAMPLE
<script>
var a=20;
if(a==10){
document.write("a is equal to 10");
}
else if(a==15){
document.write("a is equal to 15");
}
else if(a==20){
document.write("a is equal to 20");
}
else{
document.write("a is not equal to 10, 15 or 20");
}
</script>
OUTPUT
a is equal to 20
41
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
SWITCH CASE
switch case is a conditional statement that is used so it can provide a way to
execute the statements based on their conditions or different conditions.
Syntax
switch (expression)
{
case value1:
Code to be executed;
break;
Case value2:
code to be executed;
break;
.........
default:
Code to be executed if the above values are not matched;
}
EXAMPLE
OUTPUT
42
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Break Statement
A break keyword is used within the switch case statement this statement
indicates the end of a particular case of the switch statement
Default Statement
The JavaScript loops are used to iterate the piece of code using for, while, do
while or for-in loops. It makes the code compact. It is mostly used in array. It is also
called iterative statements.
1. For loop
2. while loop
3. do while loop
4. for in loop
1.For loop
The JavaScript for loop iterates the elements for the fixed number of times. It
should be used if number of iteration is known. It executes a block of code until a
specified condition is true.
SYNTAX
43
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
EXAMPLE
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
OUTPUT
1
2
3
4
5
2.while loop
The JavaScript while loop iterates the elements for the infinite number of
times. It should be used if number of iteration is not known. In the while loop, the
condition is first checked and if it evaluates to TRUE, the statements inside the curly
braces are executed.
44
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
while (condition)
code to be executed
EXAMPLE
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
OUTPUT
11
12
13
14
15
SYNTAX
do
{
45
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
code to be executed
}while (condition);
EXAMPLE
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
OUTPUT
21
22
23
24
25
4.For in loop
syntax
EXAMPLE
<html>
<body>
<h1>JavaScript Arrays</h1>
46
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<h2>For In Loops</h2>
<p>The for in statement can loops over array values:</p>
<p id="demo"></p>
<script>
const numbers = [45, 4, 9, 16, 25];
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
OUTPUT
JavaScript Arrays
For In Loops
45
4
9
16
25
BREAK
<html>
<body>
<h2>JavaScript Loops</h2>
<p>A loop with a <b>break</b> statement.</p>
47
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<p id="demo"></p>
<script>
let text = "";
for (let i = 0; i < 10; i++)
{
if (i === 3)
{
break;
}
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
OUTPUT
JavaScript Loops
A loop with a break statement.
The number is 0
The number is 1
The number is 2
CONTINUE STATEMENT
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
<html>
<body>
<h2>JavaScript Loops</h2>
<p>A loop with a <b>continue</b> statement.</p>
<p>A loop which will skip the step where i = 3.</p>
<p id="demo"></p>
<script>
let text = "";
48
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
OUTPUT
JavaScript Loops
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
ADVANCE SCRIPT
BUILT IN OBJECTS
WINDOW OBJECT
Property Description
49
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
defaultStatus Deprecated.
Method Description
If you want to access any element in an HTML page, you always start with
accessing the document object.
50
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Method Description
document.createElement(element) Create an
HTML
element
document.removeChild(element) Remove an
HTML
element
document.appendChild(element) Add an
HTML
element
51
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Property Description
52
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Property Description
Property Description
53
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
Property Description
BROWSER OBJECT
NUMBER OBJECT
Method Description
54
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
JavaScript is template based not class based. Here, we don't create class to
get the object. But, we direct create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
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>
Output
55
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
Output
Here, you need to create function with arguments. Each argument value can
be assigned in the current object by using this keyword.
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
56
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
}
e=new emp(103,"Vimal Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>
Output
<script>
function emp(id,name,salary)
{
this.id=id;
this.name=name;
this.salary=salary;
this.changeSalary=changeSalary;
function changeSalary(otherSalary)
{
this.salary=otherSalary;
}
}
e=new emp(103,"Sonoo Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
e.changeSalary(45000);
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>
Output
57
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
JavaScript interacts with web pages through the DOM (Document Object
Model) and various Web APIs provided by the browser.
DOM
The Document Object Model (DOM) represents an HTML document as a
structured tree of objects, where each HTML element is a node that JavaScript can
manipulate.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1 id="heading">Hello, DOM!</h1>
<p class="text">This is a paragraph.</p>
</body>
</html>
This structure is represented as:
Document
├── <html>
│ ├── <head>
│ │ ├── <title>Page Title</title>
58
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
│ ├── <body>
│ │ ├── <h1 id="heading">Hello, DOM!</h1>
│ │ ├── <p class="text">This is a paragraph.</p>
2. Manipulating the DOM with JavaScript
The browser provides built-in objects and APIs to interact with webpages
beyond the DOM.
JavaScript validations help ensure that user inputs meet the expected format
before being processed. This improves user experience and prevents invalid or
harmful data submission.
60
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
function validateRequired() {
let name = document.getElementById("name").value;
if (name.trim() === "") {
alert("Name is required!");
return false;
}
function validateLength() {
let username = document.getElementById("username").value;
if (username.length < 5 || username.length > 15) {
alert("Username must be between 5 and 15 characters.");
return false;
}
return true;
}
61
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
3.Email Validation
function validateEmail() {
let email = document.getElementById("email").value;
let regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!regex.test(email)) {
alert("Invalid email format!");
return false;
}
return true;
}
function validatePhone() {
let phone = document.getElementById("phone").value;
let regex = /^\d{10}$/; // 10-digit phone number
if (!regex.test(phone)) {
alert("Invalid phone number! Must be 10 digits.");
return false;
}
return true;
Ensures the password contains at least one uppercase letter, one lowercase letter,
one digit, and one special character.
function validatePassword() {
let password = document.getElementById("password").value;
let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-
z\d@$!%*?&]{8,}$/;
if (!regex.test(password)) {
alert("Password must be at least 8 characters long and include an uppercase,
lowercase, digit, and special character.");
62
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
return false;
}
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
return validateRequired() && validateEmail() && validatePassword();
}
function validateRequired() {
let name = document.getElementById("name").value;
if (name.trim() === "") {
alert("Name is required!");
return false;
}
return true;
}
function validateEmail() {
let email = document.getElementById("email").value;
let regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!regex.test(email)) {
alert("Invalid email format!");
return false;
}
return true;
}
function validatePassword() {
let password = document.getElementById("password").value;
let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-
z\d@$!%*?&]{8,}$/;
63
APOLLO ARTS AND SCIENCE COLLEGE CHENNAI
UNIT IV WEB DESIGN AND DEVELOPMENT
if (!regex.test(password)) {
alert("Password must contain an uppercase, lowercase, digit, and special
character.");
return false;
}
return true;
}
</script>
</head>
<body>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name"><br>
<label for="email">Email:</label>
<input type="email" id="email"><br>
<label for="password">Password:</label>
<input type="password" id="password"><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
64