Unit-2 - Java Script_xml
Unit-2 - Java Script_xml
Engineering &Technology
Dr. M.Gangappa
Associate Professor
Email: gangappa_m@vnrvjiet
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 1
Client-side Scripting: Introduction to JavaScript, JavaScript language –
declaring variables, scope of variables, Operators, Conditional Statements,
JavaScript Loops, Functions, Array, Array functions, String object, Date
object, Regular expression object, Event handlers (Keyboard Events,
Mouse events, Form Events, Window events), Document Object Model
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 2
Agenda
1. JavaScript 10. Event and Event handling in Java script
2. JavaScript Comment 11. Pop – up boxes in Java script
3. JavaScript Variable 12. Form validation in Java script
4. JavaScript Data Types 13. Java script InnerHTML
5. Primitive data types 14. DHTML
6. Conditional statements
7. JavaScript Functions
8. JavaScript object
9. Implicit objects
1. JavaScript array
2. Math object
3. String Object
4. Regular Expression Object
5. Date object
6. Number Object
7. Window object
8. Document object
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 3
❑ JavaScript is a light weight, interpreted and dynamic programming
language.
❑ It allows client-side script to interact with the user and make the
dynamic pages.
❑ JavaScript can be implemented with JavaScript statements that
are placed within the <script>…</script> HTML tags in a web page
❑ It is a case-sensitive language.
❑ JavaScript is supportable in several operating systems including,
Windows, macOS, etc.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 4
Java script was started in the year 1995 to fulfil the dynamic nature of web pages for the client side applications.
Now JS has been widely using in different variations.
1) Client side web development
JavaScript
JQUERY --- library/API
Angular JS
React JS etc.
2) Server side Web development
Node Js is the library of Javascript
Express JS
3) Mobile APP
4) Browser plugins/patches/Extensions
5) Machine Learning
TensorFlow JS
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 5
JavaScript provides 3 places to put the JavaScript code:
➢ within body tag
➢ within head tag
➢ external JavaScript file.
code between the body tag
<html>
<body>
<script type="text/javascript">
alert("Hello Javat-point");
</script>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 6
code between the head tag
<html>
<head>
<script type="text/javascript">
function msg(){
alert("Hello Javatpoint");
}
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 7
External JavaScript file
➢ It provides code re-usability because single JavaScript file can be
used in several html pages.
➢ An external JavaScript file must be saved by .js extension.
➢ It increases the speed of the webpage.
index.html
message.js <html>
<head>
function msg(){ <script type="text/javascript" src="message.js"></script>
alert("Hello VNR VJIET");
} </head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 8
JavaScript Comment
1.<script> 1.<script>
2.// It is single line comment 2./* It is multi line comment.
3.document.write("hello javascript"); 3.It will not be displayed */
4.</script> 4.document.write("example of javascript multiline
comment");
5.</script>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 9
JavaScript Variable
A JavaScript variable is simply a name of storage location. There are
two types of variables in JavaScript :
➢ local variable .
➢ global variable.
Correct JavaScript variables JavaScript local variable
1.var x = 10; A JavaScript local variable is declared
2.var _value=“CSE"; inside block or function.
<script>
function abc(){
let x=10;//local variable
}
</script>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 10
Variable declaration in java script
Parameters Var Let
Introduction in JavaScript Var has been a part of Let was introduced in the
JavaScript since its ES 2015 (ES6) version of
inception. JavaScript.
Scope Var is globally scoped. Let is block-scoped.
Access and Declaration Var can be declared and Let can be declared
accessed globally. globally, but its access is
limited to the block in
which it is declared.
Redeclaration Variables declared using Variables declared with let
var can be re-declared and can be updated but not re-
updated within the same declared within the same
scope. scope.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 11
JavaScript global variable
A JavaScript global variable is accessible from any function.
<html>
<body>
<script>
var data=200; //global variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();
//calling JavaScript function
b();
</script>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 12
JavaScript Data Types
JavaScript provides different data types to hold different types
of values. There are two types of data types in JavaScript.
1.Primitive data type
2.Non-primitive (reference) data type
JavaScript is a dynamic type language, means we don't
need to specify type of the variable because it is dynamically
used by JavaScript engine.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 13
Data Type Description
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 14
JavaScript Data Types
JavaScript - String
String is a primitive data type in JavaScript. It must be enclosed in single
or double quotation marks.
<script>
var str = 'Hello World';
document.getElementById("p1").innerHTML =
document.getElementById("p1").innerHTML + str[i];
</script>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 16
String-Concatenation
String can be concatenated using plus (+) operator in JavaScript.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 17
String- quotes
JavaScript - Number
The Number type represents both integer and floating-point numbers
and has 3 symbolic values:
❑ +Infinity
❑ -Infinity
❑ NaN (Not-a-Number)
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 18
Boolean
The Boolean type represents a logical value that has two possible
values: true and false.
Null
The Null datatype in JavaScript only represents null
value ( non-existent or invalid object or address )
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 20
Operators are used to assign values, compare values, perform arithmetic operations, and
more.
❑ Arithmetic Operators
❑ Assignment Operators
❑ Comparison Operators
❑ Logical Operators
❑ Conditional Operators
❑ Type Operators
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 21
Arithmetic Operators
Operator Description Example
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
* Multiplication 10*20 = 200
/ Division 20/10 = 2
% Modulus (Remainder) 20%10 = 0
++ Increment var a=10; a++; Now a = 11
-- Decrement var a=10; a--; Now a = 9
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 22
Comparison Operators
Operator Description Example
== Is equal to 10==20 = false
!= Not equal to 10!=20 = true
!== Not Identical 20!==20 = false
> Greater than 20>10 = true
>= Greater than or equal to 20>=10 = true
< Less than 20<10 = false
<= Less than or equal to 20<=10 = false
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 23
Assignment Operators
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 24
Logical Operators
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 25
typeof operator
The type of a variable is determined by the type of the value assigned to it. JavaScript has
a special operator called typeof which lets you get the type of any value.
The typeof operator takes only one operand . It evaluates the type of the operand and
returns the result as a string.
Syntax:
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 26
Typeof operator and its result
typeof 0; //'number'
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 27
Examples
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
Adding two numbers, will return the sum, but adding a number and a string will
return a string:
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 28
JavaScript supports the following forms of if..else statement −
❑ if statement
❑ if...else statement
❑ if...else if... statement.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 29
check if the number is positive.
if(expression){
//content to be evaluated let number = parseInt(prompt("Enter a number: "));
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 30
if (condition) {
// block of code if condition is true
} else { check if the number is positive.
// block of code if condition is false
}
let number = parseInt(prompt("Enter a number: "));
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 31
if (expression 1) {
Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true
} else {
Statement(s) to be executed if no expression is true
}
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 32
check if the number if positive, negative or zero
const number = prompt("Enter a number: ");
// check if number is greater than 0
if (number > 0) {
console.log("The number is positive");
}
// check if number is 0
else if (number == 0) {
console.log("The number is 0");
}
// if number is neither greater than 0, nor zero
else {
console.log("The number is negative");
}
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 33
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-
in loops.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 34
The syntax of for loop in JavaScript is as follows −
<script>
var i=11;
do{ while (i<=15)
code to be executed {
document.write(i + "<br/>");
}while (condition); i++;
}
</script>
Write JS program to find the sum of positive numbers if the user enters negative
number, the loop terminates.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 36
The JavaScript for-in loop is used to cycle through an object's properties.
const salaries= {
Jack : 24000,
Paul : 34000,
}
Jack : $24000,
// using for...in
Paul : $34000,
for ( let i in salaries) {
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 37
Write a JavaScript program to:
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 38
A JavaScript function is a block of code designed to perform a particular task.
function functionName(parameters) {
// code to be executed
}
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 39
JavaScript object
❑ Any real world entity is called an object. Example: Table is an object.
❑ the primitive data-types all store a single value each (depending on their
types).
❑ Object is a non-primitive data type in JavaScript. It is like any other
variable, the only difference is that an object holds multiple values in
terms of properties and methods.
▪ Properties can hold values of primitive data types and methods are
functions.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 40
JavaScript object
❑ In JavaScript, an object is a standalone entity because there is no class
in JavaScript.
❑ In JavaScript, an object can be created in two ways:
1.Object literal
2.Object constructor
object={property1:value1,property2:value2.....propertyN:valueN}
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 41
JavaScript object
var emptyObject = {}; // object with no properties or methods
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 42
JavaScript object
// object with properties & method
var person = {
firstName: “VNR",
lastName: “VJIET",
getFullName: function () {
return this.firstName + ' ' + this.lastName
}
};
Note : You must specify key-value pair in object for properties or methods.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 43
Access JavaScript Object Properties & Methods
1. objectName.propertyName
2.objectName["propertyName"]
Demo
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 44
Object Constructor
The second way to create an object is with Object Constructor
using new keyword.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 45
JavaScript array
Array constructor
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 46
Array Literals
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 47
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 48
Return
The original array elements in reverse order.
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 49
Math object
All methods and properties can be used without creating a Math object first.
The syntax for Math any methods is : Math.method.(number)
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 50
Questions
Loop
If condition
1. Find the biggest of three numbers
2. Print the personality based on age
3. Print the student grade based on score in a test
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 51
String Object
1) By string literal
var stringName = "string value";
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 52
String Object
String Properties :
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 53
Examples
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 54
Regular Expression Object
/pattern/attributes;
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 55
Syntax
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 56
Metacharacters
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 57
Metacharacters
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 58
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 59
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 60
❑ Why DOM?
❑ HTML is used to structure the web pages and JavaScript is used to add behavior to our
web pages. When an HTML file is loaded into the browser, the javascript can not
understand the HTML document directly. So it interprets and interacts with the
Document Object Model (DOM), which is created by the browser based on the HTML
document.
❑ What is DOM?
❑ The Document Object Model (DOM) is an application programming interface (API) for
manipulating HTML documents.
❑ The DOM represents an HTML document as a tree of nodes. The DOM provides
functions that allow you to add, remove, and modify parts of the document effectively.
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 61
Consider the following HTML document:
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 62
DOM
Document Object has properties
methods
Using this, we can
❑ Select HTML elements
❑ Modify HTML elements
❑ Remove/delete HTML elements
❑ Add/remove styles to HTML elements
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 63
Methods to select HTML elements:
3. document.getElementsByTagName(“name”);
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 64
❑ Get the parent element
❑ Get child elements
❑ Get siblings of an element
To get the parent node of a specified node in the DOM tree, you use the parentNode
property:
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 65
<body>
<div id="main"> Output
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 66
To get the first child element of a specified element, you use the firstChild property of the
element:
let firstChild = parentElement.firstElementChild;
Example:
Output:
<li class="first">Home</li>
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 67
let lastChild = parentElement.lastElementChild;
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 68
1.document.createElement(“Node”)
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 69
3. innerHTML – get and set the HTML content of an element.
To get the HTML markup contained within an element, you use the following syntax:
let content = element.innerHTML;
Example:
get the content of the <ul> element:
<ul id="menu">
<li>Home</li> let menu = document.getElementById('menu');
<li>Services</li>
</ul> console.log(menu.innerHTML);
Output:
<li>Home</li>
<li>Services</li>
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 70
❑ An HTML web page has finished loading
❑ An HTML button was clicked.
The above statements are actions on web page.
These actions are called an events.
An HTML event can be something the browser does, or something a user does.
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 71
1. Mouse Events
Event Performed Event Handler Description
click onclick When mouse click on an element
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 72
2. KeyBoard Events
Event Performed Event Handler Description
Keydown onkeydown When the user release the key
Keyup onkeyup When the user press the key
3.Window/Document events
Event Performed Event Handler Description
load onload When the browser finishes the loading of the
page
unload onunload When the visitor leaves the current webpage, the
browser unloads it
resize onresize When the visitor resizes the window of the
browser
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 73
4. Form events
Dr.M.Gangappa,Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 74
Data validation
<form >
<input type = “text” name=“name” value=“”>
<input type = “text” name=“age” value=“”>
<input type =“submit” value=“submit”>
</form>
<script>
var name = document.forms[0].elements[0].value; // extract the value
var age= document.forms[0].elements[1].value; // data for age
</script>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 75
Date object
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 76
Date object
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 77
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 78
Questions
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 79
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 80
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 81
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 82
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 83
Window object methods
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 84
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 85
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 86
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 87
Object hierarchy
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 88
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 89
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 90
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 91
<script type="text/javascript">
function getcube(){
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 92
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 93
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 94
EVENTS
Examples of events
➢An HTML web page has finished loading
➢An HTML input field was changed
➢An HTML button was clicked
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 95
Syntax
HTML allows event handler attributes, with JavaScript code.
Demo
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 96
Event types:
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 97
1.Inline event handling:
node.event = () => {
// handling statements
}
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 98
Example:
App.js
Index.html
// accessing btn1
let btn1 = document.querySelector("#btn1"); <body>
<button id="btn1">click me!</button>
<script src="App.js"></script>
</body>
btn1.onclick=()=>{
console.log("button1 was clicked");
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 99
Event Listeners in JavaScript
The addEventListener() method is used to attach an event handler to a particular
element.
element.addEventListener(event, function);
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 100
END of JavaScript
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 101
Practice Questions
1. Print the output generated by the script below:
<html>
<body>
<button id="btn1">click me!</button>
<script>
let btn1 = document.querySelector("#btn1");
btn1.onclick = () => { document.write("button event handler 1 is performed"); }
btn1.onclick= () => { document.write("button event handler 2 is performed"); }
</script>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 102
Practice Questions
2.Determine the colors applied for the text "Good morning":
<html > <head> <style>
.red { background-color: red; }
.blue { background-color: blue; }
.orange { background-color: orange; }
.green { background-color: orange; }
</style>
</head>
<body>
<h1 class="blue green orange red ">Good morning</h1>
</body>
</html>
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 103
XML
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 104
What is XML
➢ XML stands for eXtensible Markup Language that you can use
to create your own tags.
➢ A markup language is used to provide information about a
document.
➢ HTML tags tell a browser how to display the document.
➢ XML tags give a reader some idea what some of the data
means.
What is a DTD?
DTD stands for Document Type Definition that defines the structure and
the legal elements and attributes of an XML document.
DTD Types:
1. Internal DTD
2. External DTD
Dr Malige Gangappa CSE dept VNR VJIET
When a DTD is declared within the file it is called Internal DTD and if
it is declared in a separate file it is called External DTD.
External DTD: You can put the DTD in a separate file from the XML
file, and refer to it using a <!DOCTYPE ...> line at the beginning of
the XML file.
Internal DTD: You can put the DTD inside the <!DOCTYPE ...>
declaration at the front of the XML file.
Syntax for internal DTD
<?xml version='1.0’?>
<!DOCTYPE root-element-name [
dtd-declarations ...
Dr Malige Gangappa CSE dept VNR VJIET
]>
XML Document Internal DTD
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE note [
<note> <!ELEMENT note (to,from,heading,body)>
<to>Ravi</to> <!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<from>Krishna</from>
<!ELEMENT heading (#PCDATA)>
<heading>Reminder</heading>
<!ELEMENT body (#PCDATA)>
<body> ]>
Don't forget me this weekend!
</body> <note>
</note> <to>Ravi</to>
<from>Krishna</from>
<heading>Reminder</heading>
<body>
Xml document Don't forget me this weekend!
validation </body>
</note>
e.g.,
<first> vnr vjiet </first>
Dr Malige Gangappa CSE dept VNR VJIET
mixed: The content may contain character data and/or child elements.
<!ELEMENT mix_element (#PCDATA | first | last)>
If these XML fragments were added together, there would be a name conflict. Both contain
a <table> element, but the elements have different content and meaning.
In the example above, there will be no conflict because the two <table> elements have
different names.
Dr Malige Gangappa CSE dept VNR VJIET
XML Namespaces - The xmlns Attribute
The namespace can be defined by an xmlns attribute in the start tag of an element.
The namespace declaration has the following syntax xmlns:prefix="URI".
xmlns:xs="http://www.w3.org/2001/XMLSchema"
It specifies that the elements and data types that come from the
"http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:
XSD - declaration
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<shoesize country="france">35</shoesize>
XSD-declaration
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Solution:
Order indicators:
•All Sequence Indicator
•Choice The <sequence> indicator specifies that the
•Sequence child elements must appear in a specific
order:
Occurrence indicators:
•maxOccurs <xs:element name="person">
•minOccurs <xs:complexType>
<xs:sequence>
Group indicators: <xs:element name="firstname" type="xs:string"/>
•Group name
<xs:element name="lastname" type="xs:string"/>
•attributeGroup name
</xs:sequence>
</xs:complexType>
Dr Malige Gangappa CSE dept VNR VJIET
Write the xml schema definition to validate the following xml document?
<?xml version="1.0"?>
<beginners book>
<to>My Readers</to>
<from>Chaitanya</from>
<subject>A Message to my readers</subject>
<message>Welcome to beginnersbook.com</message>
</beginnersbook>
<xs:element name="beginnersbook">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Syntax
node.getElementsByTagName("tagname");
The following example returns all <title> elements under the x element:
example x.getElementsByTagName("title");
Dr Malige Gangappa CSE dept VNR VJIET
Navigating Node Relationships
var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];
// display the information
document.getElementById("demo").innerHTML = x.nodeValue;
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 159
AJAX Introduction
❑ A synchronous request blocks the client until operation completes i.e.
browser is unresponsive. In such case, javascript engine of the browser
is blocked.
❑ An asynchronous request doesn’t block the client i.e. browser is
responsive. In such case, javascript engine of the browser is not
blocked.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 160
❑ A synchronous request blocks the client until operation completes i.e.
browser is unresponsive. In such case, javascript engine of the browser is
blocked.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 161
❑ An asynchronous request doesn’t block the client i.e. browser is
responsive. In such case, javascript engine of the browser is not
blocked.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 162
❑ AJAX stands for Asynchronous JavaScript and XML.
❑ It is not a programming language but it is a web browser technology for
creating better, faster, and more interactive web applications with the
help of XML, HTML, CSS, and Java Script.
❑ AJAX applications might use XML to transport data.
❑ AJAX allows to send and receive data asynchronously without reloading
the web page. So it is fast.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 163
Examples of AJAX
❑ Google Maps
A user can drag an entire map by using the mouse, rather than clicking on a button.
❑ Gmail
Gmail is a webmail built on the idea that emails can be more efficient and useful.
❑ Yahoo Maps
Now it's even easier and more fun to get where you're going!
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 164
How AJAX Works
AJAX communicates with the server using XMLHttpRequest object.
XMLHttpRequest object plays a important role.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 165
How AJAX Works
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 166
AJAX - The Object
The XMLHttpRequest object can be used to exchange data with a server
behind the scenes.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 167
XMLHttpRequest Object Methods
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
open(method,url,async,user,psw) Specifies the request
method: the request type GET or POST
url: the file location
async: true (asynchronous) or false
(synchronous)
user: optional user name
psw: optional password
send() Sends the request to the server
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 168
XMLHttpRequest Object Properties
Property Description
readyState Holds the status of the XMLHttpRequest.
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
status Returns the status-number of a request
200: "OK"
403: "Forbidden"
404: "Not Found"
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 169
AJAX - Request
To send a request to a server, we use the open() and send() methods of
the XMLHttpRequest object:
Method Description
open(method, url, async) Specifies the type of request
method: the type of request: GET or POST
url: the server file location
async: true (asynchronous) or false (synchronous)
send() Sends the request to the server
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 170
AJAX - Response
❑ The readyState property holds the status of the XMLHttpRequest.
❑ The status property and the statusText property holds the status of
the XMLHttpRequest object.
Property Description
readyState Holds the status of the XMLHttpRequest.
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
status 200: "OK"
403: "Forbidden"
404: "Page not found"
statusText Returns the status-text (e.g. "OK" or "Not Found")
Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 171
THANK YOU
Prepared by M.Gangappa , Department of Computer Science & Engineering, VNRVJIET, Hyderabad February 22, 2025 172