Session 3
Session 3
1
Math Object
●
The math object provides you properties and methods for
mathematical constants and functions. Unlike other global
objects, Math is not a constructor. All the properties and
methods of Math are static and can be called by using
Math as an object without creating it.
<p id="demo"></p>
<script>
Var radius=3;
var circleArea = Math.PI * radius * radius;
document.getElementById("demo").innerHTML = Math.PI;
</script>
2
Math Object(cont.)
●
Math Object Constants
3
Math Object(cont.)
●
Math Object Methods
4
Math Object(cont.)
●
Math Class Methods(cont.)
5
Date Object
●
To obtain and manipulate the date and time in a script.
●
Syntax:
<script>
var myDate= new Date(PI)) // holds current date
6
Date Object(cont.)
●
Date Object Number Conventions:
7
Date Object(cont.)
●
The Date object methods fall into these broad categories:
●
get" methods
●
for getting date and time values from date objects
●
"set" methods
●
for setting date and time values in date objects
●
"to" methods
●
for returning string values from date objects.
8
Date Object(cont.)
●
get Methods
var now = new Date ( "November 25,2006 11:13:55");
9
Date Object(cont.)
●
Get Date Example:
<p id="demo"></p>
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>
10
Date Object(cont.)
●
set Methods
var someDate = new Date ();
var now = new Date ( "November 25,2006 11:13:55");
Name Example
setDate(number) someDate.setDate(25)
setHours(number) someDate.setHours(14)
setMinutes(number) someDate.setMinutes(50)
setMonth(number) someDate.setMonth(7)
setSeconds(number) someDate.setSeconds(7)
setTime(TimeString) someDate.setTime(now.getTime())
setYear(number) someDate.setYear(88)
11
Date Object(cont.)
●
Set Date example:
<p id="demo"></p>
<script>
var d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
12
Date Object(cont.)
●
to Methods
●
var now = new Date ( "November 25,2006 11:13:00");
13
Date Object(cont.)
●
to Methods Example:
<p id="demo"></p>
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
14
DOM Object
●
A Document object represents the HTML document that is displayed in
that window. The Document object has various properties that refer to
other objects which allow access to and modification of document content.
●
The way a document content is accessed and modified is called the
Document Object Model, or DOM. The Objects are organized in a
hierarchy. This hierarchical structure applies to the organization of objects
in a Web document.
●
Document object − Each HTML document that gets loaded into a window
becomes a document object. The document contains the contents of the
page.
●
In other words: The HTML DOM is a standard for how to get, change, add,
or delete HTML elements.
15
DOM Object (cont.)
●
Here is a simple hierarchy of a few important objects
16
DOM Object (cont.)
●
The document object represents your web page.
●
If you want to access any element in an HTML page, you
always start with accessing the document object.
●
HTML DOM methods are actions you can perform (on
HTML Elements).
●
HTML DOM properties are values (of HTML Elements) that
you can set or change.
17
DOM Object (cont.)
●
Properties:
Property Description
bgColor A string that specifies the background color.
fgColor A string that specifies the text color.
linkColor The color of text for a link that the user has not yet visited
(PI)read/write).
vlinkColor The color of text for a link that the user has already visited
(PI)read/write).
alinkColor The color of text for a link that the user clicks (PI)read/write).
lastModified A string that specifies the date the document was last modified.
title A string that specifies the contents of the TITLE tag.
18
DOM Object (cont.)
●
Methods for accessing document elements
Method Description
getElementById("ID") For accessing any element on the
page via its ID attribute
getElementsByName(“NAME”) Returns a collection of objects with
the specified name
19
Image element
●
The Image object is an image on an HTML form, created
by using the HTML 'IMG' tag.
●
Any images created in a document are then stored in an
array in the document.images property.
●
Image Object has its own set of Properties, Collections,
Methods & Event handlers.
20
Image element
●
Object Model reference:
[window.]document.imageName
[window.]document.imageID
[window.]document.images[i]
document.img1.
src=“img1.jpg”
document.image
s[0].src=“img1.j
<html>
pg” <body>
…..
<img name=“img1” src=“img1.jpg”>
document.img2.s <img name=“img2” src=“img2.jpg”>
rc=“img2.jpg” …
document.image </body>
s[1].src=“img2.j </html>
pg” 21
Image element
●
Properties:
●
Event handlers:
onabort onload onerror onclick ondblclick onmouseover
22
Form element
●
By using the form you have at your disposal information
●
about the elements in a form and their values.
●
A separate instance of the form object is created for each
●
form in a document.
●
Objects within a form can be referred to by a numeric
●
index or be referred to by name.
●
Object Model Reference:
[window.]document.formname
[window.]document.forms[i]
[window.]document.forms[“formNAME”]
[window.]document.forms[“formID”]
23
Form element
●
Properties:
Property Description
elements[] An array containing all of the elements of the form. Use it to
loop through form easily.
length The number of elements in the form.
name The name of the form.
id The id of the form.
target The name of the target window form is to be submitted to.
24
Form element
●
Methods:
Method Description
reset() Resets the form.
Clicking the reset button clears all contents that
the user has made.
submit() Submits a form.
Clicking the submit button submits the content
of the form to the server
25
Form element
form
form
button
button checkbox
checkbox Text
Text radio
radio reset
reset select
select submit
submit textarea
textarea
26
Button
●
Methods:
Method Description
blur() Removes focus from the field.
27
Button
●
Properties:
properties Description
value Sets or returns the text that is displayed on the
button
28
Button
●
Event Handlers:
Event Handler Event
The mouse is clicked and released with the
onclick
cursor positioned over the button.
The mouse is double-clicked with the
ondblclick
cursor positioned over the button.
The mouse is pressed down with the cursor
onmousedown
positioned over the button.
onmouseout The mouse cursor is moved off the button.
The mouse cursor is moved on top of the
onmouseover
button.
The mouse button is released with the
onmouseup
cursor positioned over the button.
29
Example
●
Example for DOM form
<!DOCTYPE html>
<html>
<body>
</form>
<button onclick="myFunction()">Show</button>
<p id="test"></p>
30
Example(cont.)
<script>
function myFunction() {
var x = document.forms["frm1"];
var text = "";
var i;
for (i = 0; i < x.length ;i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("test").innerHTML = text;
}
</script>
</body>
</html>
31
Questions?
32