1. dateObjectName = new Date()
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)
To use a Date
object:
1. dateObjectName.propertyName 2. dateObjectName.methodName(parameters)Exceptions: The Date object's parse and UTC methods are static methods that you use as follows:
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.
The way JavaScript handles dates is very similar to the way Java handles dates: both languages have many of the same date methods, and both store dates internally as the number of milliseconds since January 1, 1970 00:00:00. Dates prior to 1970 are not allowed.
| 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.
A Select object created without the MULTIPLE attribute can have only one option selected by default. When you set defaultSelected in such an object, any previous default selections, including defaults set with the SELECTED attribute, are cleared. If you set defaultSelected in a Select object created with the MULTIPLE attribute, previous default selections are not affected.
function restoreDefault() {
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
}
}
}
The previous example assumes
that the Select object is similar to the following:
<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() {
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>
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.
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.
textareaName is either the value of the NAME attribute of a Textarea 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.
You can clear the document pane (and remove the text, form elements, and so on so they do not redisplay) by using document.close(); document.open(); document.write(). You can omit the document.open() call if you are writing text or HTML, since write does an implicit open of that mime type if the document stream is closed.
You can reference the anchors, forms, and links of a document by using the anchors, forms, and links arrays. These arrays contain an entry for each anchor, form, or link in a document.
Do not use location as a property of the document object; use the document.URL property instead. The document.location property, which is a synonym for document.URL, will be removed in a future release.
| Property | Description |
|---|---|
| alinkColor | Reflects the ALINK attribute |
| anchors | An array reflecting all the anchors in a document |
| applets | An array reflecting all the applets in a document |
| bgColor | Reflects the BGCOLOR attribute |
| cookie | Specifies a cookie |
| domain | Specifies the domain name of the server that served a document |
| embeds array | An array reflecting all the plugins in a document |
| fgColor | Reflects the TEXT attribute |
| forms | An array reflecting all the forms in a document |
| images | An array reflecting all the images in a document |
| lastModified | Reflects the date a document was last modified |
| linkColor | Reflects the LINK attribute |
| links | An array reflecting all the links in a document |
| referrer | Reflects the URL of the calling document |
| title | Reflects the contents of the <TITLE> tag |
| URL | Reflects the complete URL of a document |
| vlinkColor | Reflects the VLINK attribute |
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
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 only in a restricted way. Initially, domain contains the hostname of the Web server from which the document was loaded. You can set domain only to a domain suffix of itself. For example, a script from search.yahoo.com can't set its domain property to "search.yahoo". And a script from IWantYourMoney.com cannot set its domain to "yahoo.com".
Once you change the domain property, you cannot change it back to its original value. For example, if you change domain from "search.yahoo.com" to "yahoo.com", you cannot reset it to "search.yahoo.com".
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.
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.value userInfo.elements[0].valueTo obtain the number of elements on a form, use the length property: formName.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 |
<EMBED SRC=source NAME=embedName HEIGHT=height WIDTH=width> [parameterName=parameterValue] [ ... parameterName=parameterValue] </EMBED>For the complete syntax of the <EMBED> tag, see http://home.netscape.com/eng/mozilla/3.0/handbook/plugins/index.html.
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.
parameterName=parameterValue specifies the name and value of a parameter to pass to the embedded object's plug-in.
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] 2. document.embeds.lengthindex is an integer representing an <EMBED> tag or the name of an embedded object as specified by the NAME attribute.
To obtain the number of <EMBED> tags in a document, use the length property: document.embeds.length.
Use the elements in the embeds array to interact with the plug-in that is displaying the embedded object. If a plug-in is not Java-enabled, you cannot do anything with its element in the embeds array. The fields and methods of the elements in the embeds array vary from plug-in to plug-in; see the documentation supplied by the plug-in manufacturer.
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.
Elements in the embeds array are read-only. For example, the statement document.embeds[0]="myavi.avi" has no effect.
| 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.
Without the enabledPlugin property, you could determine if the user has a particular plug-in installed, if their Navigator can handle a particular MIME type, and if the MIME type is supported by any plug-ins. But you could not determine whether a plug-in is configured for the MIME type. You might need to know this information, for example, so you could dynamically emit an <EMBED> tag on the page if the user has a plug-in configured for the MIME type.
enabledPlugin is a read-only property.
// Can we display Shockwave movies?
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.")
}
See also the examples for the
MimeType object.
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 = 2
var y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")
Example 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 field = getFieldName(3)
document.write("The field named ", field, " has value of ", eval(field + ".value"))
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.
var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "
document.write("<P>z is ", eval(str))
Example 4. In the following
example, the setValue function uses eval to assign the value
of the variable newValue to the text field textObject:
function setValue (textObject, newValue) {
eval ("document.forms[0]." + textObject + ".value") = 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('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) {
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'))
Example 6. The following
example uses eval within a function that defines an object type,
flintstone. The statement 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) {
return Math.exp(x)
}
If you pass getExp the
value 1, it returns 2.718281828459045.