Unit 2-1 - Web Technology
Unit 2-1 - Web Technology
Unit 2-1 - Web Technology
B.Tech – V Semester
UNIT II
M. Vijayakumar
Assistant Professor (SS)
School of Computing Sciences,
Department of Computer Science and Engineering
INTRODUCTION TO JAVASCRIPT & AJAX
• Java Script: Variables
• Control Statements
• Functions
• Arrays
• Objects, Strings & Manipulations
• Handling Events
• Ajax: The Basics, XMLHttpRequest
• JavaScript and XML
Practical Component:
1. Create a web page with JS to validate age (onblur event) greater than 18 and
count the no of words in the description field (onclick event)
2. Create a web page to display the contents of XML in a tabular format
The getElementById() is a DOM method used to return the element that has the ID
attribute with the specified value. This is one of the most common methods in the
HTML DOM and is used almost every time we want to manipulate an element on our
document. This method returns null if no elements with the specified ID exists.
.innerHTML
The innerHTML property is part of the Document Object Model (DOM) that allows
Javascript code to manipulate a website being displayed. Specifically, it allows reading
and replacing everything within a given DOM element (HTML tag)
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 3
Java Script: Variables
Using var
Using let
Using const
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 5
<!DOCTYPE html>
In this example, price1, price2, <html>
and total, are variables: <body>
<h2>JavaScript Variables</h2>
• In programming, just like in
algebra, we use variables (like <p id="demo"></p>
price1) to hold values.
<script>
• In programming, just like in var price1 = 5;
algebra, we use variables in var price2 = 6;
expressions (total = price1 + var total = price1 + price2;
price2). document.getElementById("demo").innerHTML =
"The total is: " + total;
• From the example above, you can </script>
calculate the total to be 11.
</body>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 6
</html>
JavaScript Identifiers
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
This is different from algebra. The following does not make sense in algebra:
x = x + 5
In JavaScript, however, it makes perfect sense: it assigns the value of x + 5 to x.
(It calculates the value of x + 5 and puts the result into x. The value of x is
incremented by 5.)
• JavaScript variables can hold numbers like 100 and text values like "John Doe".
• JavaScript can handle many types of data, but for now, just think of numbers and strings.
• Strings are written inside double or single quotes. Numbers are written without
quotes.
document.getElementById("demo").innerHTML =
pi + "<br>" + person + "<br>" + answer;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 10
Declaring (Creating) JavaScript Variables
var carName;
After the declaration, the variable has no value (technically it has the value of undefined).
carName = "Volvo";
In the example below, we create a variable called carName and assign the value
"Volvo" to it.
<p id="demo"></p>
<script>
var carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
</script>
</body>
</html>
var person = "John Doe", carName = "Volvo", price = 200; <p id="demo"></p>
OR <script>
var person = "John Doe", carName = "Volvo", price =
200;
var person = "John Doe", document.getElementById("demo").innerHTML =
carName = "Volvo", carName;
price = 200; </script>
</body>
</html>
A variable declared without a value will have the <p>A variable declared without a value will have the value
value undefined. undefined.</p>
<p id="demo"></p>
The variable carName will have the value undefined
after the execution of this statement: <script>
var carName;
document.getElementById("demo").innerHTML = carName;
var carName; </script>
</body>
</html>
The variable carName will still have the value "Volvo" <h2>JavaScript Variables</h2>
after the execution of these statements:
<p>If you re-declare a JavaScript variable, it will not lose its
value.</p>
<p id="demo"></p>
var carName = "Volvo";
var carName; <script>
var carName = "Volvo";
var carName;
document.getElementById("demo").innerHTML = carName;
</script>
</body>
</html>
Since JavaScript treats underscore as a letter, identifiers containing _ are valid variable names:
You cannot accidentally redeclare a variable. // SyntaxError: 'x' has already been
declared
With let you can not do this:
Before ES6 (2015), JavaScript had only Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
Variables declared inside a { } block cannot be accessed from outside the block:
{
let x = 2;
}
// x can NOT be used here
Variables declared inside a { } block can be accessed from outside the block.
{
var x = 2;
}
// x CAN be used here
Redeclaring a variable inside a block will not <h2>Redeclaring a Variable Using let</h2>
redeclare the variable outside the block:
<p id="demo"></p>
// Here x is 10 // Here x is 10
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 24
JavaScript Const
<p id="demo"></p>
<script>
const PI = 3.141592653589793; try {
PI = 3.14; // This will give an error const PI = 3.141592653589793;
PI = 3.14;
PI = PI + 10; // This will also give an error }
catch (err) {
document.getElementById("demo").innerHTML = err;
}
</script>
</body>
</html>
JavaScript const variables must be assigned a value when they are declared:
Correct
const PI = 3.14159265359;
Incorrect
const PI;
PI = 3.14159265359;
As a general rule, always declare a variables with const unless you know that the value will
change.
A new Array
A new Object
A new Function
A new RegExp
JavaScript statements often start with a statement identifier to identify the JavaScript action to be
performed.
Statement identifiers are reserved words and cannot be used as variable names (or any other things).
<script>
<!DOCTYPE html> function myFunction() {
<html> var text = "";
<body> var i;
for (i = 0; i < 5; i++) {
<p>Click the button to do a loop with a break. The loop is if (i === 3) {
supposed to output the numbers 0 to 4, but the break break;
statement exits the loop when the variable i is equal to }
"3".</p> text += "The number is " + i + "<br>";
}
<button onclick="myFunction()">Try it</button> document.getElementById("demo").innerHTML = text;
}
<p id="demo"></p> </script>
</body>
</html>
In this example we use a for loop together with the break statement.
Loop through a block of code, but exit the loop when the variable i is equal to "3":
The break statement exits a switch statement or a loop (for, for ... in, while, do ... while).
When the break statement is used with a switch statement, it breaks out of the switch block. This will stop
the execution of more execution of code and/or case testing inside the block.
When the break statement is used in a loop, it breaks the loop and continues executing the code after the
loop (if any).
The break statement can also be used with an optional label reference, to "jump out" of any JavaScript
code block (see "More Examples" below).
Note: Without a label reference, the break statement can only be used inside a loop or a switch.
Syntax
break;
break labelname;
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 34
JavaScript if/else Statement
The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform
different actions based on different conditions.
if (condition) {
// block of code to be executed if the condition is true
}
The else statement specifies a block of code to be executed if the condition is false:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
<p>Clck the button to display "Good day", only if the time is less than
20:00.</p>
<p id="demo"></p>
<script>
function myFunction() {
var time = new Date().getHours();
if (time < 20) {
document.getElementById("demo").innerHTML = "Good day";
}
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 38
Example
If the time is less than 20:00, create a "Good day" greeting, otherwise "Good evening":
<script>
<!DOCTYPE html> function myFunction() {
<html> var time = new Date().getHours();
<body> var greeting;
if (time < 20) {
<p>Click the button to display a time-based greeting = "Good day";
greeting.</p> } else {
greeting = "Good evening";
<button onclick="myFunction()">Try }
it</button> document.getElementById("demo").innerHTML = greeting;
}
<p id="demo"></p> </script>
</body>
</html>
The loop will continue to run as long as the condition is true. It will only stop when the condition
becomes false.
<p>Click the button to loop through a block of code five <p>Click the button to loop through the indices of an array, in
times.</p> ascending order.</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<p id="demo"></p> <script>
function myFunction() {
<script> var cars = ["BMW", "Volvo", "Saab", "Ford"];
function myFunction() { var text = "";
var text = ""; var i;
var i; for (i = 0; i < cars.length; i++) {
for (i = 0; i < 5; i++) { text += cars[i] + "<br>";
text += "The number is " + i + "<br>"; }
} document.getElementById("demo").innerHTML = text;
document.getElementById("demo").innerHTML = text; }
} </script>
</script> </body>
</body> </html>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 42
The For In Loop
The JavaScript for in statement loops through the properties of an Object:
<p id="demo"></p>
<script>
const person = {fname:"John", lname:"Doe", age:25};
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 44
JavaScript While Loop
he while loop loops through a block of code as long as a specified condition is true.
while (condition) {
// code block to be executed
}
<p id="demo"></p>
<script>
let text = "";
let i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
The do while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
do {
// code block to be executed
}
while (condition);
<p id="demo"></p>
<script>
let text = ""
let i = 0;
do {
text += "<br>The number is " + i;
i++;
}
while (i < 10);
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 48
Comparing For and While <!DOCTYPE html>
<html>
<body>
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<p id="demo"></p>
The loop in this <script>
example uses a const cars = ["BMW", "Volvo", "Saab", "Ford"];
while loop to
let i = 0;
collect the car let text = "";
names from the while (cars[i]) {
cars array: text += cars[i] + "<br>";
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>