objects what is an object? definition properties methods events

31
OBJECTS

Upload: tamsin-patrick

Post on 21-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OBJECTS What is an Object? Definition Properties Methods Events

OBJECTS

Page 2: OBJECTS What is an Object? Definition Properties Methods Events

What is an Object?

• Definition

• Properties

• Methods

• Events

Page 3: OBJECTS What is an Object? Definition Properties Methods Events

Object Definition

A self-contained module of data and its associated functions.

Examples: window, frame, form, image, button, text, etc.

Page 4: OBJECTS What is an Object? Definition Properties Methods Events

Object Properties

Settable and/or gettable characteristics of the object.

Examples: window.status, frame.top, form.name, image.height, button.value, text.type, etc.

Page 5: OBJECTS What is an Object? Definition Properties Methods Events

Object Methods

A procedure or function performed by the object.

Examples: window.open( ), frame.focus( ), form.submit( ), , button.click( ), text.select( ), etc.

Page 6: OBJECTS What is an Object? Definition Properties Methods Events

Object Events

Actions (triggers) related to objects and produced by users or running programs. In JavaSript, functions that manage events are referred to as Event Handlers.

Examples: window: onLoad( ), frame: onFocus( ), form.onSubmit( ), button.onClick( ), text.onChange( ), etc.

Page 7: OBJECTS What is an Object? Definition Properties Methods Events

What is an Object Model?

Window Parent, Top, Self, Frames

HistoryDocument Location

LinksImagesFormsAreasAnchors Applets

TextButton Radio

Submit

TextareaCheckbox

PasswordReset

Select

Navigator

Hidden

Page 8: OBJECTS What is an Object? Definition Properties Methods Events

Navigator

Properties

appNameappVersionappCodeNameuserAgentmimeTypes[ ]plunging[ ]cookeEnabled

Methods

javaEnabled( )taintEnabled( )

Page 9: OBJECTS What is an Object? Definition Properties Methods Events

<HTML>

<HEAD><TITLE>Navigator Properties</TITLE>

</HEAD>

<BODY>

<PRE>

<SCRIPT LANGUAGE = "JavaScript">

for(prop in navigator) {

document.writeln( prop + " : " + navigator [prop]);

}

</SCRIPT>

</PRE>

</BODY>

</HTML>

An Example Code for getting Navigator Properties

Page 10: OBJECTS What is an Object? Definition Properties Methods Events

WindowProperties

defaultStatusframes[] nameonerror opener parentself status topwindow

Methods

alert( )confirm( )prompt( )open( )close( )focus( )blur( )scroll( )setTimeout( )clearTimeout( )

Event Handlers

onLoad=onUnload=onBlur=onFocus=

<FRAMESET COLS= ROWS= BORDER= FRAMEBORDER=Yes|NoBORDERCOLOR=onBlur= onFocus=onLoad= onUnload=>

<FRAME SRC= NAME= BORDER=BORDERCOLOR= > …

</FRAMESET>

Page 11: OBJECTS What is an Object? Definition Properties Methods Events

DocumentProperties

alinkColor anchors[ ]appletes[ ] bgColorcookie domainembeds[ ] fgColorforms[ ] images[ ]lastModified links[ ]linkColor locationplugins[ ] referrertitle URLvlinkColor

Methods

clear( )close( )eval( )open( ) toString( )valueOf( )write( )writeln( )

<BODY BACKGROUND= BGCOLOR= FGCOLOR= LINK= ALINK=

VLINK= onBlur= onFocus=onLoad= onUnload= >…

</BODY>

Page 12: OBJECTS What is an Object? Definition Properties Methods Events

<HTML>

<HEAD>

<TITLE>Defining and Accessing Variables</TITLE>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">

year = 2004;

site = "ECP3086 HTML and JavaScript";

URL = "www.mmu.edu.my";

document.writeln(site);

document.writeln(URL);

document.writeln(year);

document.writeln(x);

</SCRIPT>

</BODY>

</HTML>

An Example code for using Document Object

Page 13: OBJECTS What is an Object? Definition Properties Methods Events

Form

Methods

eval()reset( )submit( )toString( )valueOf( )

Properties

action elements[ ]encodinglengthmethodnametarget

Event Handlers

onReset=onSubmit=

<FORM NAME= TARGET= ACTION= METHOD= ENCTYPE onReset= onSubmit= >

</FORM>

Page 14: OBJECTS What is an Object? Definition Properties Methods Events

Button

Methods

blur( )click( )eval( )focus( )toString( )valueOf( )

Properties

formnamevaluetype

Event Handlers

onBlur=onClick=onFocus=

<INPUT TYPE=button NAME= VALUE= onBlur= onClick= onFocus= >

Page 15: OBJECTS What is an Object? Definition Properties Methods Events

Reset

Methods

blur( )click( )eval( )focus( )toString( )valueOf( )

Properties

formnametypevalue

Event Handlers

onBlur=onClick=onFocus=

<INPUT TYPE=reset NAME= VALUE= onBlur= onClick= onFocus= >

Page 16: OBJECTS What is an Object? Definition Properties Methods Events

Submit

Methods

blur( )click( )eval( )focus( )toString( )valueOf( )

Properties

formnamevaluetype

Event Handlers

onBlur=onClick=onFocus=

<INPUT TYPE=submit NAME= VALUE= onBlur= onClick= onFocus= >

Page 17: OBJECTS What is an Object? Definition Properties Methods Events

CheckBox

Methods

blur( )click( )eval( )focus( )toString( )valueOf( )

Properties

checkeddefaultCheckedformnametypevalue

Event Handlers

onBlur=onClick=onFocus=

<INPUT TYPE=checkbox NAME= VALUE= CHECKED onBlur= onClick= onFocus= label>

Page 18: OBJECTS What is an Object? Definition Properties Methods Events

Radio

Methods

blur( )click( )eval( )focus( )toString( )valueOf( )

Properties

checkeddefaultCheckedformlengthnametypevalue

Event Handlers

onBlur=onClick=onFocus=

<INPUT TYPE=radio NAME= VALUE= CHECKED onBlur= onClick= onFocus= label>

Page 19: OBJECTS What is an Object? Definition Properties Methods Events

Select

Methods

blur( )eval( )focus( )toString( )valueOf( )

Properties

formlengthnameoptions(i)options(i).defaultSelected options(i).indexoptions(i).selected options(i).textoptions(i).valueselected Index type

Event Handlers

onBlur=onClick=onFocus=

<SELECT NAME= SIZE= MULTIPLE onBlur= onChange= onClick= onFocus= >

<OPTION SELECTED VALUE=>item …

</SELECT>

Page 20: OBJECTS What is an Object? Definition Properties Methods Events

TextArea

Methods

blur( )eval( )focus( )select( )toString( )valueOf( )

Properties

defaultValueformnametypevalue

Event Handlers

onBlur=onChange=onFocus=onSelect=

<TEXTAREA NAME= ROWS= COLS=WRAP= off|virtual|physical onBlur= onChange= onSelect= onFocus= >

text …

</TEXTAREA>

Page 21: OBJECTS What is an Object? Definition Properties Methods Events

Text

Methods

blur( )eval( )focus( )select( )toString( )valueOf( )

Properties

defaultValueformnametypevalue

Event Handlers

onBlur=onChange onFocus=onSelect=

<INPUT TYPE=text NAME= VALUE= SIZE= MAXLENGTH= onBlur= onChange= onFocus= onSelect= >

Page 22: OBJECTS What is an Object? Definition Properties Methods Events

Password

Methods

blur( )eval( )focus( )select( )toString( )valueOf( )

Properties

defaultValueformnametypevalue

Event Handlers

onBlur=onFocus=onSelect=

<INPUT TYPE=password NAME= VALUE= SIZE= MAXLENGTH= onBlur= onFocus= onSelect= >

Page 23: OBJECTS What is an Object? Definition Properties Methods Events

Hidden

Methods

eval( )toString( )valueOf( )

Properties

formnametypevalue

<INPUT TYPE=hidden NAME= VALUE= >

Page 24: OBJECTS What is an Object? Definition Properties Methods Events

Image

Methods

eval( )toString( )valueOf( )

Properties

bordercompleteheighthspacelowsrcnamesrcvspacewidth

Event Handlers

onAbort=onError=onLoad=

<IMG SRC= NAME= LOWSRC= WIDTH= HEIGHT=HSPACE= VSPACE= BORDER= ALIGN= ISMAP USEMAP=# onLoad= onAbort= onError= >

Page 25: OBJECTS What is an Object? Definition Properties Methods Events

Link

Methods

eval( )toString( )valueOf( )

Properties

hashhosthostnamehreflengthpathnameportprotocolsearchtarget

Event Handlers

onClick=onMouseOut=onMouseOver=

<A HREF= | NAME= TARGET= onClick= onMouseOut= onMouseOver= >

link text or image

</A>

Page 26: OBJECTS What is an Object? Definition Properties Methods Events

What Are JavaScript Objects?

• String

• Array

• Math

• Date

Page 27: OBJECTS What is an Object? Definition Properties Methods Events

String

Methods

toLowerCase( ) toUpperCase( )indexOf(subStr[,startCharPos])lastIndexOf(subStr[,startCharPos])substring(startCharPos, endCharPos) charAt(charPos)split(“separator”) anchor(“name”)blink( ) fontsize(1…7) fixed( )sup( ) sub( ) small( )big( ) strike( ) italics( )bold( ) link(“URL”) fontcolor(value)

Properties length prototype

Object Creation var varName = new String(["stringExpression"])

Page 28: OBJECTS What is an Object? Definition Properties Methods Events

Array

Methodsjoin("separator")reverse()sort(compareFunc)

Properties length prototype

Object Creation

var arrayName = new Array([integer] | [val1 [,val2 ... [,valn]]])

Page 29: OBJECTS What is an Object? Definition Properties Methods Events

Math

Methods abs(x) acos(x) asin(x)atan(x) atan2(x1, x2) ceil(x)cos(x) exp(x) floor(x)log(x) max(x1, x2) min(x1, x2)pow(x1, x2) random( ) round(x)sin(x) sqrt(x) tan(x)

Properties E LN2 LN10 LOG2ELOG10E PI SQRT1_2 SQRT2

Page 30: OBJECTS What is an Object? Definition Properties Methods Events

Date

MethodsgetTime( ) getYear( ) getMonth( )getDate( ) getDay( ) getHours( )getMinutes( ) getSeconds( ) setYear(val)setMonth(val) setDate(val) setDay(val)setHours(val) setMinutes(val)setSeconds(val) getTimezoneOffset( )toGMTString( ) toLocaleString( )Date.UTC(date values)

Properties prototype

Object Creation new Date( ) new Date("Month dd, yyy hh:mm:ss") new Date("Month dd, yyyy") new Date(yy,mm,dd,hh,mm,ss)

new Date(yy,mm,dd)

Page 31: OBJECTS What is an Object? Definition Properties Methods Events

<HTML>

<HEAD><TITLE>Time Example</TITLE>

<SCRIPT LANGUAGE = "JavaScript">

function getTimes() {

var current = new Date();

var out = "Day: " + current.getDay()+"\n";

out = out.concat("Month: " + current.getMonth() + "\n");

out = out.concat("Year: " + current.getFullYear() + "\n");

out = out.concat("GMT Time: " + current.toUTCString() + "\n");

out = out.concat("Time: " + current.toString() + "\n");

timesForm.output.value = out;

}

</SCRIPT>

<BODY>

<FORM NAME="timesForm">

<P>

<INPUT NAME = "getTimeButton" TYPE = "button" VALUE = "Get Time" ONCLICK = "getTimes()">

<P>

<TEXTAREA NAME = "output" ROWS ="10" COLS="42">

</TEXTAREA>

</FORM>

</BODY>

</HTML>

Use of Date Object to display Date and Time in a Form