1. dateObjectName = new Date()To use a Date object:
2. dateObjectName = new Date("month day, year hours:minutes:seconds")
3. dateObjectName = new Date(year, month, day)
4. dateObjectName = new Date(year, month, day, hours, minutes, seconds)
1. dateObjectName.propertyNameExceptions: The Date object's parse and UTC methods are static methods that you use as follows:
2. dateObjectName.methodName(parameters)
Date.UTC(parameters)
Date.parse(parameters)
month, day, year, hours, minutes, and seconds are string values for form 2 of the syntax. For forms 3 and 4, they are integer values.
propertyName is one of the properties listed below.
methodName is one of the methods listed below.
Form 1 of the syntax creates today's date and time. If you omit hours, minutes, or seconds from form 2 or 4 of the syntax, the value will be set to zero.
Property | Description |
---|---|
prototype | Lets you add a properties to a Date object. |
today = new Date()
birthday = new Date("December 17, 1995 03:24:00")
birthday = new Date(95,12,17)
birthday = new Date(95,12,17,3,24,0)
1. checkboxName.defaultChecked
2. radioName[index].defaultChecked
radioName is the value of the NAME attribute of a Radio object.
index is an integer representing a radio button in a Radio object.
You can set the defaultChecked property at any time. The display of the checkbox or radio button does not update when you set the defaultChecked property, only when you set the checked property.
function radioResetter() {
var i=""
for (i in document.musicForm.musicType) {
if (document.musicForm.musicType[i].defaultChecked==true) {
document.musicForm.musicType[i].checked=true
}
}
}
selectName.options[index].defaultSelected
optionName.defaultSelected
index is an integer representing an option in a Select object.
optionName is the name of a Select object option created using the Option() constructor.
You can set the defaultSelected property at any time. The display of the Select object does not update when you set the defaultSelected property, only when you set the selected or selectedIndex properties.
function restoreDefault() {The previous example assumes that the Select object is similar to the following:
for (var i = 0; i < document.musicForm.musicType.length; i++) {
if (document.musicForm.musicType.options[i].defaultSelected == true) {
document.musicForm.musicType.options[i].selected=true
}
}
}
<SELECT NAME="musicType">
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
</SELECT>
windowReference.defaultStatus
You can set the defaultStatus property at any time. You must return true if you want to set the defaultStatus property in the onMouseOut or onMouseOver event handlers.
function statusSetter() {In the previous example, notice that the onMouseOver event handler returns a value of true. You must return true to set status or defaultStatus in an event handler.
window.defaultStatus = "Click the link for the Netscape home page"
window.status = "Netscape home page"
}
<A HREF="http://home.netscape.com"
onMouseOver = "statusSetter(); return true">Netscape</A>
1. passwordName.defaultValue
2. textName.defaultValue
3. textareaName.defaultValue
textName is either the value of the NAME attribute of a Text object or an element in the elements array.
You can set the defaultValue property at any time. The display of the related object does not update when you set the defaultValue property, only when you set the value property.
function defaultGetter() {
msgWindow=window.open("")
msgWindow.document.write("hidden.defaultValue is " +
document.surfCity.hiddenObj.defaultValue + "<BR>")
msgWindow.document.write("password.defaultValue is " +
document.surfCity.passwordObj.defaultValue + "<BR>")
msgWindow.document.write("text.defaultValue is " +
document.surfCity.textObj.defaultValue + "<BR>")
msgWindow.document.write("textarea.defaultValue is " +
document.surfCity.textareaObj.defaultValue + "<BR>")
msgWindow.document.close()
}
1. navigator.mimeTypes[index1].description
2. navigator.plugins[index2].description
index2 is either an integer representing a plug-in installed on the client or a string containing the name of a Plugin object (from the name property).
For Plugin objects, the description property is a description of the plug-in. The description is supplied by the plug-in itself.
description is a read-only property.
For Plugin objects: filename, length, name properties
<BODY
BACKGROUND="backgroundImage"
BGCOLOR="backgroundColor"
TEXT="foregroundColor"
LINK="unfollowedLinkColor"
ALINK="activatedLinkColor"
VLINK="followedLinkColor"
[onBlur="handlerText"]
[onFocus="handlerText"]
[onLoad="handlerText"]
[onUnload="handlerText"]>
</BODY>
BGCOLOR, TEXT, LINK, ALINK, and VLINK are color specifications expressed as a hexadecimal RGB triplet (in the format "rrggbb" or "#rrggbb") or as one of the string literals listed in "Color values".
1. document.propertyName
2. document.methodName(parameters)
methodName is one of the methods listed below.
You can load a new document by using the window.location object.
The following objects are also properties of the document object:
|
|
|
|
doc0.html
, which defines the frames, contains the following code:
<HTML>
<HEAD>
<TITLE>Document object example</TITLE>
</HEAD>
<FRAMESET COLS="30%,70%">
<FRAME SRC="doc1.html" NAME="frame1">
<FRAME SRC="doc2.html" NAME="frame2">
</FRAMESET>
</HTML>
doc1.html
, which defines the content for the first frame, contains the following code:<HTML>
<SCRIPT>
</SCRIPT>
<BODY
BGCOLOR="antiquewhite"
TEXT="darkviolet"
LINK="fuchsia"
ALINK="forestgreen"
VLINK="navy">
<P><B>Some links</B>
<LI><A HREF="doc2.html#numbers" TARGET="frame2">Numbers</A>
<LI><A HREF="doc2.html#colors" TARGET="frame2">Colors</A>
<LI><A HREF="doc2.html#musicTypes" TARGET="frame2">Music types</A>
<LI><A HREF="doc2.html#countries" TARGET="frame2">Countries</A>
</BODY>
</HTML>
doc2.html
, which defines the content for the second frame, contains the following code:<HTML>
<SCRIPT>
</SCRIPT>
<BODY
BGCOLOR="oldlace" onLoad="alert('Hello, World.')"
TEXT="navy">
<P><A NAME="numbers"><B>Some numbers</B></A>
<UL><LI>one
<LI>two
<LI>three
<LI>four</UL>
<P><A NAME="colors"><B>Some colors</B></A>
<UL><LI>red
<LI>orange
<LI>yellow
<LI>green</UL>
<P><A NAME="musicTypes"><B>Some music types</B></A>
<UL><LI>R&B
<LI>Jazz
<LI>Soul
<LI>Reggae</UL>
<P><A NAME="countries"><B>Some countries</B></A>
<UL><LI>Afghanistan
<LI>Brazil
<LI>Canada
<LI>Finland</UL>
</BODY>
</HTML>
document.domain
www.yahoo.com
might need to share properties with a script on the host search.yahoo.com
.
If scripts on two different servers change their domain property so that both scripts have the same domain name, both scripts can share properties. For example, a script loaded from search.yahoo.com
could set its domain property to "yahoo.com". A script from www.yahoo.com
running in another window could also set its domain property to "yahoo.com". Then, since both scripts have the domain "yahoo.com", these two scripts can share properties, even though they did not originate from the same server.
You can change domain at any time.
document.domain="braveNewWorld.com"
Math.E
function getEuler() {
return Math.E
}
1. formName.elements[index]
2. formName.elements.length
index is an integer representing an object on a form or the name of an object as specified by the NAME attribute.
.elements[0]
, formName.elements[1]
, and formName.elements[2]
.Although you can also reference a form's elements by using the element's name (from the NAME attribute), the elements array provides a way to reference Form objects programmatically without using their names. For example, if the first object on the userInfo form is the userName Text object, you can evaluate it in either of the following ways:
userInfo.userName.valueTo obtain the number of elements on a form, use the length property: formName
userInfo.elements[0].value
.elements.length
. Each radio button in a Radio object appears as a separate element in the elements array.
Elements in the elements array are read-only. For example, the statement formName.elements[0]="music"
has no effect.
The value of each element in the elements array is the full HTML statement for the object.
Property | Description |
---|---|
length | Reflects the number of elements in the form |
<EMBEDFor the complete syntax of the <EMBED> tag, see http://home.netscape.com/eng/mozilla/3.0/handbook/plugins/index.html.
SRC=source
NAME=embedName
HEIGHT=height
WIDTH=width>
[parameterName=parameterValue]
[ ... parameterName=parameterValue]
</EMBED>
NAME=embedName specifies the name of the embedded object in the document. You can use this name when indexing the embeds array.
HEIGHT=height specifies the height of the applet in pixels within the browser window.
WIDTH=width specifies the width of the applet in pixels within the browser window.
document.embeds[0]
, document.embeds[1]
, and document.embeds[2]
.Elements in the embeds array may have public callable functions, if they reference a "LiveConnected" plug-in. See Chapter 4, "LiveConnect."
1. document.embeds[index]index is an integer representing an <EMBED> tag or the name of an embedded object as specified by the NAME attribute.
2. document.embeds.length
To obtain the number of <EMBED> tags in a document, use the length property: document.embeds.length
.
When you use the <EMBED> tag to generate output from a plug-in application, you are not creating a Plugin object. See the Plugin object.
Property | Description |
---|---|
length | Reflects the number of elements in the array |
<EMBED SRC="train.au" HEIGHT=50 WIDTH=250>
navigator.mimeTypes[index].enabledPlugin
The enabledPlugin property is a reference to a Plugin object that represents the plug-in that is configured for the specified MIME type. If the MIME type does not have a plug-in configured, enabledPlugin is null.
enabledPlugin is a read-only property.
// Can we display Shockwave movies?See also the examples for the MimeType object.
mimetype = navigator.mimeTypes["application/x-director"]
if (mimetype) {
// Yes, so can we display with a plug-in?
plugin = mimetype.enabledPlugin
if (plugin)
// Yes, so show the data in-line
document.writeln("Here\'s a movie: <EMBED SRC=mymovie.dir HEIGHT=100 WIDTH=100>")
else
// No, so provide a link to the data
document.writeln("<A HREF='mymovie.dir>Click here</A> to see a movie.")
} else {
// No, so tell them so
document.writeln("Sorry, can't show you this cool movie.")
}
formName.encoding
You can set the encoding property at any time.
function getEncoding() {
return document.musicForm.encoding
}
escape("string")
escape("Hello, World")The following example returns "%26":
escape("&")The following example returns "%21%23":
escape("!#")
[objectName.]eval(string)
string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
Navigator 3.0: a method of every object
If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2"
, to a variable, and then calling eval at a later point in your script.
var x = 2Example 2. In the following example, the getFieldName(n) function returns the name of the nth form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.
var y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")
var field = getFieldName(3)Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assigns z a value of 42 if x is five, and assigns zero to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.
document.write("The field named ", field, " has value of ", eval(field + ".value"))
var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "Example 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:
document.write("<P>z is ", eval(str))
function setValue (textObject, newValue) {Example 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses
eval ("document.forms[0]." + textObject + ".value") = newValue
}
eval('breed')
without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed')
which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.function Dog(name,breed,color) {Example 6. The following example uses eval within a function that defines an object type, flintstone. The statement
this.name=name
this.breed=breed
this.color=color
}
myDog = new Dog("Gabby")
myDog.breed="Lab"
var breed='Shepherd'
document.write("<P>" + eval('breed'))
document.write("<BR>" + myDog.eval('breed'))
fred = new flintstone("x=42")
creates the object fred with the properties x, y, z, and z2. The write statements display the values of these properties as 42, 43, 44, and 45, respectively.function flintstone(str) {
this.eval("this."+str)
this.eval("this.y=43")
this.z=44
this["z2"] = 45
}
fred = new flintstone("x=42")
document.write("<BR>fred.x is " + fred.x)
document.write("<BR>fred.y is " + fred.y)
document.write("<BR>fred.z is " + fred.z)
document.write("<BR>fred.z2 is " + fred.z2)
Math.exp(number)
function getExp(x) {If you pass getExp the value 1, it returns 2.718281828459045.
return Math.exp(x)
}