Web Technologies
Web Technologies
Unit 2
Unit 3
1. What is an array? Explain how to create an array and how to add and access elements in
an array.
Array:
An array is a collection of homogeneous items. In JavaScript an array contains
heterogeneous items. Arrays in JavaScript are also called as associative arrays.
Creating and Accessing the Array Elements:
An array can be declared as follows,
var arr;
Elements can be allocated to this array by the following statement,
arr = new Array(10);
An array is not initialized while it is allocated.
Arrays in JavaScript are considered as objects. They can also be created by making use of
constructors. Constructors in JavaScript are of three types. They are,
➔ Array( );
➔ Array(number of elements);
➔ Array(List of elements separated by commas);
Ex:
var arr = new Array( );
It creates an array of zero length.
arr[3] = 5;
The length of an array is 4. i.e., it has 4 elements. It adds an elements at the location arr[3].
Values to the array elements are assigned by writing them in square brackets by separating
them with commas.
Syntax:
var arr = [element0, element1, element2, ……….., element];
Ex:
var flowers = [“Rose”
,
“Lily”
,
“Jasmine”];
var arr = [0, 1, 2, 3, 4, 5];
An array index starts with 0. The elements of an array are accessed using the index.
Program:
<html>
<head>
<title>Basic Javascript Array Example</title>
</head>
<body>
<script language="javascript">
var empty = [];
var fruits = ['apple', 'orange', 'kiwi'];
alert(fruits[1]);
</script>
</body>
</html>
3. What is an object? Explain various member functions of math object and date object.
Object:
An object is referred as a set of properties and methods. Properties are nothing but the
features and methods are nothing but the actions that are performed on the objects. The
properties can either be primitive data type or objects themselves. They can be added to the
objects even after they are created. Objects belonging to a class can have different
properties and methods.
Types of objects in JavaScript:
1. Math Object:
2. Date Object:
3. String Object:
4. Boolean Object:
5. Number Object:
Math Object:
Numerous mathematical functions are introduced by JavaScript to perform several
mathematical calculations. These mathematical functions or methods are entities of “Math”
object.
Syntax:
Math.methodname(numeric values);
Mathematical functions are:
i) min( ): Displays minimum of two numerical values.
Ex: document.writeln( min(2,5));
ii) max( ): Displays maximum of two numerical values.
Ex: document.writeln(max(2,5));
iii) abs( ): Displays the absolute value of the number entered into it.
Ex: document.writeln(abs(5));
iv) ceil( ): Displays the rounded value of the integer. The values displayed will be greater
than the value supplied.
Ex: document.writeln(ceil(5.2));
v) pow( ): Displays the power of the given number.
Ex: document.writeln(pow(2,3));
vi) round( ): Rounds the value entered to its nearest integer.
Ex: document.writeln(round(5.5));
vii) sqrt( ): Displays the square root of the given value.
Ex: document.writeln(sqrt(4));
viii) sin( ): Displays the sine value of the given numeric value.
Ex: document.writeln(sin(90));
viii) cos( ): Displays the consequent value of the given numeric value.
Ex: document.writeln(cos(0));
ix) tan( ): Displays the tangent value of the given numeric value.
Ex: document.writeln(tan(45));
x) exp( ): Displays exponential value of the number value.
Ex: document.writeln(exp(2));
xi) floor( ): Rounds the numeric value to the largest integer.
Ex: document.writeln(floor(5,2));
xii) log( ): Displays the logarithmic equivalent value for the numeric value.
Ex: document.writeln(log(2.718282));
Program:
<html>
<body>
<script type = "text/javascript">
var value = Math.abs( 4.5 );
document.writeln("First Value : " + value );
var value = Math.floor( 90.45 );
document.writeln("<br>Second Value : " + value );
var value = Math.sin( Math.PI/2 );
document.write("<br>Third Value : " + value );
</script>
</body>
</html>
Date Object:
Date/Time is recognized in two ways i.e., UTC (Universal Coordinated Time or Greenwich
Mean Time) and Local Time. UTC is a standard time followed through out the world. Local
time is the time of the system where the script is currently residing.
Syntax:
var currentdate = new date( );
The date( ) constructor in the above declaration is empty. Different values can e obtained
by passing parameters to this constructor.
Ex:
var currentdate = new date(“month, dd, yyyy”);
It returns a snapshot of system’s month, date and year values of that particular instant.
var currentdate = new date(“month, dd, yyyy, hh:mm:ss”);
It returns system’s month, date and year along with hours, minutes and seconds value of
that particular instant.
var currentdate = new date(yy, mm, dd);
It returns the current year, month and date values of the system.
var currentdate = new date(yy, mm, dd, hh, mm, ss);
It returns current year, month, date, hours, minutes and seconds values of the system.
var currentdate = (GMT milliseconds from 1/1/1970);
It returns the Greenwich Mean Time value.
The following methods are also supported by date object.
1) getFullyear( ) 2) getHours( ) 3) getMilliseconds 4) getDate( )
5) getDay( ) 6) getMonth( ) 7) getMinutes( ) 8) getTime( )
9) getSeconds( ) 10) setDate(1….31) 11) setFullyear(year[ , month, day])
12) setMilliseconds(ms) 13) setMinutes(min[ , secs ,ms]) 14)setSeconds(secs[ , ms])
15) setMonth(month[ , day]) 16) setTime(time) 17)toString( ) 18)toGMTString( )
19) toLocalString( )
Program:
<html>
<head>
<title> Working with Date object </title>
</head>
<body>
<script type="text/javaScript">
var mydate = new Date();
document.write("Today date is: " + mydate.getDate() + "/" + (mydate.getMonth()+1 ) +
"/" + mydate.getFullYear() + "<br/>");
document.write("The time is: " + mydate.getHours() + ":" + mydate.getMinutes() + ":"+
mydate.getSeconds() + "<br/>");
</script>
</body>
</html>
Unit 4
1. List and explain Mouse Events.
The following are the list of Mouse events.
➔ onClick
➔ onDblClick
➔ onDragDrop
➔ onMouseDown
➔ onMouseUp
➔ onMouseOut
➔ onMouseOver
1. onClick:
When an element or mouse is clicked, the onClick event handler calls the JavaScript
function. The specific JavaScript code or function is being executed on the occurrence of
the click event.
Program:
<html>
<head>
<script>
function fun()
{
alert("Welcome");
}
</script>
</head>
<body>
<h3> On Click Event </h3>
<p> Click the following button to see the effect. </p>
<button onclick = "fun()">Click me</button>
</body>
</html>
2. onDblClick:
This event occurs when the user double clicks on an object. onDblClick event is handled by
onDblClick event handler.
Program:
<html>
<head>
<title> On Double Click Event </title>
</head>
<body>
<h1 id = "heading" ondblclick = "fun()"> Hello world :):) </h1>
<h2> Double Click the text "Hello world" to see the effect. </h2>
<script>
function fun()
{
document.getElementById("heading").innerHTML = " Welcome to the javaTpoint.com ";
}
</script>
</body>
</html>
3. onDragDrop:
This event occurs when the user drags and drops the element at a different location.
Program:
<html>
<head>
<style>
#div1 {
width: 350px;
height: 70px;
padding: 10px;
border: 1px solid #aaaaaa;
}
</style>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<p>Drag the image into the rectangle:</p>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="butterfly.jpg" draggable="true" ondragstart="drag(event)"
width="336" height="69"></body>
</html>
4. onMouseDown:
This event occurs when the mouse button is pressed down on an element. This event is
handled by the onMouseDown event handler.
Program:
<html>
<body>
<li> OnMouseDown Eventhandler :
<input type=text value="click mouse key, don't release" size=20
onMouseDown='alert("Mouse Down")'>
</body>
</html>
5. onMouseMove:
This event occurs when the pointer moves when it is on an element. This event is handled
by the onMouseMove event handler.
Program:
<html>
<body>
<li> onMouseMove Eventhandler:
Traverse the mouse through the text
<a href =" " onMouseMove='alert(" mouse traversed")'> Welcome </a>
</body>
</html>
6. onMouseOut:
This event occurs when the cursor leaves the element. The javascript code is called when
the cursor leaves the element.
Program:
<html>
<body>
<li> onMouseOut Eventhandler :
Place the mouse pointer here
<a href=" " onMouseOut='alert("Mouse Out")'> Welcome </a>
</body>
</html>
7. onMouseOver:
This event occurs when the mouse pointer is moved over an element. This event is handled
by the onMouseOver eventhandler.
Program:
<html>
<body>
<li> onMouseOver Eventhandler:
<a href=" " onMouseOver='alert("Mouse Over")'> Welcome </a>
</body>
</html>
Unit 5
Program:
<table>
<tbody>
<tr>
<td>Apple</td>
<td>Mango</td>
</tr>
<tr>
<td>Grapes</td>
<td>Orange</td>
</tbody>
</table>