|
22 Objects
The various
interface
elements of a form are treated as objects
-
having properties
and methods which JavaScript can access or modify.
See the next tutorial
for
the standard object heirarchy of a browser page.
In code, an
ways to reference a form (window part can be optional)
-
window.document.forms[index]
-
window.document.forms[
"name"
]
-
window.documents.forms.name
. window.document.name
Objects have properties and methods
Variables can be defined as an object type or as a ref
variables are objects
-
have properties and methods
X
=
myobject[
"property_name"
]
=
6
object.property
=
"value"
// literal string or number
var
MyVar
=
window.open(
"hello.html"
,
"html_name"
,
"width=200,height=200"
);
//variable can be made to refer to an object
x
=
object.method();
// shows how to use methods
// note that the parentheses are required, even though the method has no arguments
// the parentheses () provide a visual that a function/method is being used
x
=
x.toUpperCase();
// uses the variable method to change its case
x
=
x.fontcolor(
'red'
);
// uses the variable method to change fontcolor
X
=
document.forms(0).elements(0).name
X
=
document.MyForm.MyText.name
you can add properties or methods at will
-
property
-
simply assign
-
methods
-
create
function
first i.e., such as oldfunction
-
myobj.newmethodname
=
oldfunction
you can create a
new
object of your own creation
:
-
define structure
function
AnyObject (a,b,c)
{
this
.a
this
.b
}
-
create instance X
=
new
AnyObject
Web Page Objects
:
document
table
form
button
image
link
Built
-
in
Objects
-
string
-
math
-
date
-
array
-
option
-
function
How to use objects
in
a window
:
The first image
in
an HTML document is document.images[0]
the second is document.images[1], and so on.
If you want to know how many images are
in
a document, you can
find out by checking the length of the images array
:
document.images.length.
On a web page
with
a form named
"myform"
, access the form
with
:
window.document.myform
On a web page
with
a form named
"myform"
, access the form
with
:
window.document.myform
Common Properties
/
Methods
----------------------------------------------
methods are always followed by parenthesis, whether they have arguments or not
window
methods
:
open window.open(
"URL"
,
"name"
,
"features"
);
close
blur
focus
properties
:
status window.status
=
"hello"
;
document
methods
:
writeln window.writeln(
"test line of text"
);
// add \n character
write window.writeln(
"test line of text"
);
// does not add \n character
properties
:
array
methods
:
properties
:
length document.images.length
--
gives number of images
in
the document
date
methods
:
getSeconds
getMinutes
getHours
properties
:
length document.images.length
--
gives number of images
in
the document
Sample .open usage
window.open(
"URL"
,
"name"
,
"location,menubar"
);
window.open(
"URL"
,
"name"
,
"location=no,status=no"
);
window.open(
"URL"
,
"name"
,
"location,height=100,width=100"
);
|