WT Ass2
WT Ass2
• It allows us to execute a JavaScript function after a specified period, thereby making it possible to
add a new dimension, time, to our website.
• With the help of the timer, we can run a command at specified intervals, run loops repeatedly at a
predefined time, and synchronize multiple events in a particular time span.
1
document.getElementById("msg").innerHTML="You have 5 sec to stop Time Interval messge";
i = setInterval("alert('Interval message')",5000);
}
function clearintervalmsg(){
clearInterval(i);
}
</script>
</head>
<body>
<h1>Demonstrate calling function with timer</h1>
<h3><p id="msg"></p></h3>
<button onclick="timedmsg()">Set Timed Alert Box</button><br>
<button onclick="cleartimedmsg()">Clear Timed Alert Box</button><br>
<button onclick="intervalmsg()">Set Interval Alert Box</button><br>
<button onclick="clearintervalmsg()">Clear Interval Alert Box</button><br>
</body>
</html>
Example:
<html>
<head>
<script>
function keyup()
{
document.write("This is a keyup event");
}
function keydown()
{
document.write("This is a keydown event");
}
function keypress()
{
document.write("This is a keypress event");
}
</script>
</head>
<body>
2
<input type="button" value="Keyevent" onkeyup="keyup()">
<input type="button" value="Keyevent" onkeydown="keydown()">
<input type="button" value="Keyevent" onkeypress="keypress()">
</body>
</html>
Mouse Events:
Attribute Description New in
HTML5
onclick Fires on a mouse click on the element No
ondblclick Fires on a mouse double-click on the element No
onmousedown Fires when a mouse button is pressed down on an element No
onmouseup Fires when a mouse button is released over an element No
onmouseover Fires when the mouse pointer moves over an element No
onmouseout Fires when the mouse pointer moves out of an element No
onmousemove Fires when the mouse pointer is moving while it is over an element No
onmousewheel Deprecated. Use the onwheel attribute instead Yes
onwheel Fires when the mouse wheel rolls up or down over an element Yes
ondrag Script to be run when an element is dragged Yes
ondragend Script to be run at the end of a drag operation Yes
ondragenter Script to be run when an element has been dragged to a valid drop target Yes
ondragleave Script to be run when an element leaves a valid drop target Yes
ondragover Script to be run when an element is being dragged over a valid drop target Yes
ondragstart Script to be run at the start of a drag operation Yes
ondrop Script to be run when dragged element is being dropped Yes
onscroll Script to be run when an element's scrollbar is being scrolled Yes
Example:
<html>
<head>
<script>
function mousemove()
{
document.write("This is a mouse move event");
}
function mouseclick()
{
document.write("This is a mouse click event");
}
function mouseup()
{
document.write("This is a mouse up event");
}
function mousedown()
{
document.write("This is a mouse down event");
}
3
function mousedblclick()
{
document.write("This is a mouse dblclick event");
}
function mouseout()
{
document.getElementById("myP").style.color = "blue";
}
function mouseover()
{
document.getElementById("myP").style.color = "red";
}
</script>
</head>
<body>
<p>Click the text below!</p>
<p id="myP" onmouseover="mouseOver()" onmouseout="mouseOut()">
The mouseOver() function sets the color of this text to red whenever mouse pointer moves
over an element. <br>
The mouseRelease() function sets the color of this text to blue whenever mouse pointer leaves
an element.
</p>
<input type="button" value="Keyevent" onmousemove="mousemove()">
<input type="button" value="Keyevent" onmouseup="mouseup()">
<input type="button" value="Keyevent" onmousedown="mousedown()">
<input type="button" value="Keyevent" onclick="mouseclick()">
<input type="button" value="Keyevent" ondblclick="mousedblclick()">
<input type="button" value="Keyevent" onmouseout="mouseout()">
<input type="button" value="Keyevent" onmouseover="mouseover()">
</body>
4
</html>
5
3. Explain briefly about window & form events with examples.
Attribute Description
onafterprint Script to be run after the document is printed
onbeforeprint Script to be run before the document is printed
onbeforeunload Script to be run when the document is about to be unloaded
onerror Script to be run when an error occurs
onhashchange Script to be run when there has been changes to the anchor part of the a URL
onload Fires after the page is finished loading
onmessage Script to be run when the message is triggered
onoffline Script to be run when the browser starts to work offline
ononline Script to be run when the browser starts to work online
onpagehide Script to be run when a user navigates away from a page
onpageshow Script to be run when a user navigates to a page
onpopstate Script to be run when the window's history changes
onresize Fires when the browser window is resized
onstorage Script to be run when a Web Storage area is updated
onunload Fires once a page has unloaded (or the browser window has been closed)
onundo Triggers at the time of performing the undo action in a document.
onblur Trigger when a window loses focus
onfocus Trigger when a window gets focus
Example:
<html>
<p>When the input field gets focus, a function changes the background-color.</p>
<script>
6
function display()
function myFunction() {
document.getElementById("txt").style.background = "green";
</script>
</body>
</html>
Form Events:
Events triggered by actions inside a HTML form (applies to almost all HTML elements, but is most
used in form elements):
Attribute Description
7
MIN_VALUE Returns the minimum numerical value possible in JavaScript
MAX_VALUE Returns the maximum numerical value possible in JavaScript
NEGATIVE_INFINITY Represents the value of negative infinity
POSITIVE_INFINITY Represents the value of infinity
Constructor Holds the value of constructor function that has created the object.
Prototype Adds property and methods to Number object
• Methods:
Property Description
toExponential(x) Converts a number into an exponential notation.
toFixed(x) Round up a number to x digits after the decimal
toPrecision(x) Round up a number to a length of x digits
toString(x) Returns a string value for the Number object
valueOf() Returns a primitive value for the Number type.
Example:
<html>
<body>
<script>
var a=100;
p=Math.PI;
</script>
8
</body>
</html>
. Math Object:
• The math object is used to perform simple and complex
arithmetic operation.
• JavaScript math object is used to perform mathematical task.
Properties:
Description
Property
Holds approximate value
E Euler’s number 2.718
LN2 natural logarithmic of 2 0.693
LN10 natural logarithmic of 10 2.302
LOG2E base-2 logarithmic of E 1.442
LOG10E base-10 logarithmic of E 0.434
PI Number value of PI 3.142
SQRT1_2 Square root of 1/2 0.707
SQRT2 Square root of 2 1.414
• Methods
Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm of x
max(x1,x2,..xn) Returns the number with the highest value
min(x1,x2,..xn) Returns the number with the lowest value
9
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
Example:
<html>
<body>
<script>
var n=10.2;
var i=-10;
var j=0;
</script>
</body>
10
</html>
Date Object:
• The Date object used to display the date on web page or time stamp in numerical or
mathematical calculations.
• Date objects are created with new Date ().
• There are five ways of instantiating (creating) a date:
var date1 = new Date();
or
var date1 = new Date(yyyy, mm, dd, [, hr, min, sec, millisec]);
properties:
Name Description
C onstructor Returns the function that created the Date object's prototype
P rototype Allows you to add properties and methods to an object
Methods
Name Description
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
Returns the number of milliseconds since midnight Jan 1 1970, and a specified
getTime()
date
Returns the time difference between UTC time and local time, in minutes
getTimezoneOffset()
11
getUTCDate() Returns the day of the month, according to universal time (from 1-31)
getUTCDay() Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear() Returns the year, according to universal time
getUTCHours() Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
getUTCMonth() Returns the month, according to universal time (from 0-11)
getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
now() Returns the number of milliseconds since midnight Jan 1, 1970
Parses a date string and returns the number of milliseconds since January 1
parse()
1970
setDate() Sets the day of the month of a date object
setFullYear() Sets the year of a date object
setHours() Sets the hour of a date object
setMilliseconds() Sets the milliseconds of a date object
setMinutes() Set the minutes of a date object
setMonth() Sets the month of a date object
setSeconds() Sets the seconds of a date object
Sets a date to a specified number of milliseconds after/before January 1, 1970
setTime()
setUTCDate() Sets the day of the month of a date object, according to universal time
setUTCFullYear() Sets the year of a date object, according to universal time
setUTCHours() Sets the hour of a date object, according to universal time
setUTCMilliseconds() Sets the milliseconds of a date object, according to universal time
setUTCMinutes() Set the minutes of a date object, according to universal time
setUTCMonth() Sets the month of a date object, according to universal time
setUTCSeconds() Set the seconds of a date object, according to universal time
setYear() Deprecated. Use the setFullYear() method instead
toDateString() Converts the date portion of a Date object into a readable string
Returns the date portion of a Date object as a string, using locale conventions
toLocaleDateString()
Returns the time portion of a Date object as a string, using locale conventions
toLocaleTimeString()
12
Returns the number of milliseconds in a date since midnight of January 1, 1970
UTC()
according to UTC time
valueOf() Returns the primitive value of a Date object
Example:
<html>
<body>
<script>
</script>
</body>
</html>
13
var s = new String(string);
• String object properties
Properties Description
Length It returns the length of the string.
Prototype It allows you to add properties and methods to an object.
Constructor It returns the reference to the String function that created the object.
• String object methods
Methods Description
charCodeAt() It returns the ASCII code of the character at the specified position.
concat() It combines the text of two strings and returns a new string.
Example:
<html>
<body>
<h1>String Methods</h1>
<script>
14
var str1=new String("Web Technologies");
15
document.write("<br><br>trim() Function : "+ str4.trimEnd().length);
</script>
</body>
</html>
Array object:
• An Array is used to store a number of values (called as elements) in order with a single
variable.
• An array object can created in two ways Using array constructor:
• Creates an empty array: var ar = new Array() Creates an array with given
size: var ar = new Array(size)
• Creates an array based on number of elements: var ar = new Array(element0, element1,
..., elementN)
16
Use to add one or more elements to the beginning of an array and returns the new length of
unshift()
the array.
valueOf() Returns the primitive value of an array.
Example:
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>Array Methods</h2>
UnShift operation (adds new elements to array and returns new length)<p id="s6"></p>
to String (string is formed with coma separated elements of array) <p id="s8"></p>
<script>
17
document.getElementById("c1").innerHTML = carray.concat(arr1,arr2);
document.getElementById("p1").innerHTML = arr1.pop();
document.getElementById("p2").innerHTML = arr1;
document.getElementById("p3").innerHTML = arr1.push(100,200);
document.getElementById("p4").innerHTML = arr1;
document.getElementById("r").innerHTML = arr1.reverse();
document.getElementById("s1").innerHTML = arr1.shift();
document.getElementById("s2").innerHTML = arr1;
document.getElementById("s3").innerHTML = arr1.slice(1,4);
document.getElementById("s4").innerHTML = arr2;
document.getElementById("s5").innerHTML = arr2.sort();
document.getElementById("s6").innerHTML = arr2.unshift(60,70);
document.getElementById("s7").innerHTML = arr2;
document.getElementById("s8").innerHTML = arr3.toString();
document.getElementById("li").innerHTML = arr3.lastIndexOf("welcome");
</script>
</body>
</html>
18