以下的一些"方法"可用在JavaScript:
Returns the absolute value of its argument.
abs(arg)
xxx Examples to be supplied.
Returns the arc cosine (in radians) of its argument.
acos(arg)
xxx Examples to be supplied.
Displays an Alert dialog box with a message and an OK button.
alert("message")
The argument message is any string.
Use the alert method to display a message that does not require a user decision. The message argument specifies a message that the dialog box contains.
In the following example, the testValue function checks the name entered by a user in the text element of a form to make sure that it is no more than eight characters in length. This example uses the alert method to prompt the user of an application to enter a valid value.
function testValue(textElement) { if (textElement.length > 8) { alert("Please enter a name that is 8 characters or less") } }You can call the testValue function in the onBlur event handler of a form's text element, as shown in the following example:
Name: <INPUT TYPE="text" NAME="userName" onBlur="testValue(userName)">
Causes the calling string object to be a named anchor by surrounding it with HTML anchor tags, <A NAME=...> </A>.
string.anchor(name)
The argument name can be any string.
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the arc sine (in radians) of its argument.
asin(arg)
xxx Examples to be supplied.
Sets location.href so you don't have to type ".href" complements toString().
assign()
Do not call the assign method. It exists so you can use 'location=...' instead of 'location.href=...'. xxx Description to be supplied.
xxx Examples to be supplied.
Returns the arc tangent (in radians) of its argument.
atan(arg)
xxx Examples to be supplied.
Loads the previous URL in the history list.
back()
This method performs the same action as a user choosing the Back button in the Navigator. The back method is the same as go(-1).
xxx Examples to be supplied.
Causes the calling string object to be displayed in big font by surrounding it with HTML big font tags, <BIG> and </BIG>.
big()
xxx Description to be supplied.
xxx Examples to be supplied.
Causes the calling string object to blink by surrounding it with HTML blink tags, <BLINK> ... </BLINK>.
blink()
xxx Description to be supplied.
xxx Examples to be supplied.
For password, text, and textArea, removes focus to the object.
blur()
Use the blur method to remove focus from a specific form element.
xxx Examples to be supplied.
Causes the calling string object to be displayed as bold by surrounding it with HTML bold tags, <B> ... </B>.
bold()NOTE: For Beta4, use B() for this method.
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the least integer greater than or equal to its argument.
ceil(arg)
xxx Examples to be supplied.
Returns the character at the specified index.
charAt(index)
index is any integer from 0 to string.length() - 1.
xxx Description to be supplied.
xxx Examples to be supplied.
Clears the window.
clear()
xxx Description to be supplied.
xxx Examples to be supplied.
Cancels a timeout that was set with the setTimeout method.
clearTimeout(timeoutID)
timeoutID is a timeout setting that was returned by a previous call to the setTimeout method.
xxx Description to be supplied. Applies to
xxx Examples to be supplied.
Simulates a mouse click on the calling form element.
click()
The effect of the click method varies according to the calling element:
button, checkbox, radioButton, reset, selection, submit
xxx Examples to be supplied.
For document and window, closes the window. xxx Not yet implemented in Beta 4.
close()
Document description to be supplied.
The close method closes the current window.
Any of the following examples close the current window:
window.close() self.close() close()
Displays a Confirm dialog box with the specified message and OK and Cancel buttons.
confirm("message")
The argument message is any string.
Use the confirm method to ask the user to make a decision that requires either an OK or a Cancel. The message argument specifies a message that prompts the user for the decison. The confirm method returns true if the user chooses OK and false if the user chooses Cancel.
This example uses the confirm method in the confirmCleanUp function to confirm that the user of an application really wants to quit. If the user chooses OK, the custom cleanUp() function closes the application.
function confirmCleanUp() { if (confirm("Are you sure you want to quit this application?")) { cleanUp() } }You can call the confirmCleanUp function in the onClick event handler of a form's pushbutton, as shown in the following example:
<INPUT TYPE="button" VALUE="Quit" onClick="confirmCleanUp()">
Returns the cosine of its argument. The argument is in radians.
cos(arg)
xxx Examples to be supplied.
The eval function is a function built-in to JavaScript. It is not a method associated with any object, but is part of the language itself.
eval(expression)
The eval function takes a JavaScript arthimetic expression as its argument and returns the value of the argument as a number.
Both of the uses of eval in the following example assign the value 42 to the variable result.
x = 6 result = eval((3+3)*7) result = eval(x*7)
Returns e to the power of its argument, i.e. ex, where x is the argument, and e is Euler's constant, the base of the natural logarithms.
exp(arg)
xxx Examples to be supplied.
Causes the calling string object to be displayed in fixed-pitch font in HTML by surrounding it with typewriter text tags, <fixed> ... </fixed>.
fixed()NOTE: For Beta4, use TT() for this method.
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the greatest integer less than or equal to its argument.
floor(arg)
xxx Examples to be supplied.
For password, text, and textArea, gives focus to the object.
focus()
Use the focus method to navigate to a specific form element and give it focus. You can then either programatically enter a value in the element or let the user enter a value.
In the following example, the checkPassword function confirms that a user has entered a valid password. If the password is not valid, the focus method returns focus to the password field and the select method highlights it so the user can re-enter the password.
function checkPassword(userPass) { if (badPassword) { alert("Please enter your password again.") userPass.focus() userPass.select() } }This example assumes that the password is defined as:
<INPUT TYPE=password NAME=userPass>
Causes the calling string object to be displayed in the specified color by surrounding it with HTML font color tags, <FONTCOLOR=color> ... </FONTCOLOR>.
fontcolor(color)
The argument to the method, color, must be a string containing a hashmark (#) followed by a triplet of hexadecimal number pairs. These three pairs represent the red, green, and blue values for the desired color, respectively.
xxx Description to be supplied.
xxx Examples to be supplied.
Causes the calling string object to be displayed in the specified font size by surrounding it with HTML font size tags, <FONTSIZE=size> ... </FONTSIZE>.
fontsize(size)
The argument to the method, size, must be an integer between one and seven.
xxx Description to be supplied.
xxx Examples to be supplied.
Loads the next URL in the history list.
forward()
This method performs the same action as a user choosing the Forward button in the Navigator. The forward method is the same as go(1).
xxx Examples to be supplied.
dateObj.getDate()
where dateObj
is a date object.
Returns the day of the month for the date object, an integer between 1 and 31.
The second statement below assigns the value 25 to the variable day, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00") day = Xmas95.getDate()
dateObj.getDay()
where dateObj
is a date object.
Returns the day of the week for the date object, an integer corresponding to the day of the week: zero for Sunday, one for Monday, two for Tuesday, and so on.
The second statement below assigns the value 1 to weekday, based on the value of the date object Xmas95. This is because December 25, 1995 is a Monday.
Xmas95 = new Date("December 25, 1995 23:15:00") weekday = Xmas95.getDay()
dateObj.getHours()
where dateObj
is a date object.
Returns the hour for the date object, an integer between 0 and 23.
The second statement below assigns the value 23 to the variable hours, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00") hours = Xmas95.getHours()
dateObjgetMinute()
where dateObj
is a date object.
Returns the minutes in the date object, an integer between 0 and 59.
The second statement below assigns the value 15 to the variable minutes, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00") minutes = Xmas95.getMinutes()
dateObj.getMonth()
where dateObj
is a date object.
Returns the month in the date object, an integer between zero and eleven. Zero corresponds to January, one to Februrary, and so on.
The second statement below assigns the value 11 to the variable month, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00") month = Xmas95.getDate()
dateObj.getSeconds()
where dateObj
is a date object.
Returns the seconds in the current time, an integer between 0 and 59.
The second statement below assigns the value 30 to the variable secs, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:30") secs = Xmas95.getSeconds()
dateObj.getTime()
where dateObj
is a date object.
Returns the numeric value for the date object. This number is the number of milliseconds since the epoch (1 January 1970 00:00:00) You can use this method to help assign a date and time to another date object.
The following example assigns the date value of theBigDay to sameAsBigDay.
theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime())
dateObj.getTimezoneOffset()
where dateObj
is a date object.
Return the time zone offset in minutes for the current locale, i.e. the difference between the local time and GMT. This value would be a constant except for daylight savings time.
x = new Date(); currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60;
getyear()
Returns the year in the date object, less 1900.
The second statement below assigns the value 95 to the variable year, based on the value of the date object Xmas95.
Xmas95 = new Date("December 25, 1995 23:15:00") year = Xmas95.getYear()
The argument delta is an integer. If delta is greater than zero, then it loads the URL that is that number of entries forward in the history list; otherwise, it loads the URL that is that number of entries backward in the history list.
The argument string is a string. Go to the newest history entry whose title or URL contains string as a substring; substring matching is case-insensitive.
go(delta | "string")
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the index within the calling string object of the first occurrence of the specified character, starting the search at fromIndex.
indexOf(character, [fromIndex])
character is the character to search for.
fromIndex is the location within the calling string to start the search from, any integer from 0 to string.length() - 1.
xxx Description to be supplied.
xxx Examples to be supplied.
Causes the calling string object to be italicized by surrounding it with HTML italics tags, <I> ... </I>.
italics()NOTE: For Beta4, use I() for this method.
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the index within the calling string object of the last occurrence of the specified character. The calling string is searched backwards, starting at fromIndex.
indexOf(character, [fromIndex])
character is the character to search for.
fromIndex is the location within the calling string to start the search from, any integer from 0 to string.length() - 1.
xxx Description to be supplied.
xxx Examples to be supplied.
Causes the calling string object to be a hyperlink by surrounding it with HTML hyperlink tags, <A HREF=...> </A>.
link(location)The argument to the method, location, should be a valid URL.
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the natural logarithm (base e) of its argument.
log(arg)
xxx Examples to be supplied.
Returns the greater of its two arguments.
max(arg1, arg2)
xxx Examples to be supplied.
Returns the lesser of its two arguments.
min(arg1, arg2)
xxx Examples to be supplied.
For the window object, opens a new web browser window. xxx For the document object, description to be supplied.
The first form of the syntax is for the document object; the second form is for window.
1. document.open("MIME type") 2. window.open("URL", "windowName", ["windowFeatures"])
URL specifies the URL to open in the new window.
windowName specifies a name for the window object being opened.
windowFeatures is a comma-separated list of any of the following options and values:
toolbar[=yes|no]|[=1|0] location[=yes|no]|[=1|0] directories[=yes|no]|[=1|0] status[=yes|no]|[=1|0] menubar[=yes|no]|[=1|0] scrollbars[=yes|no]|[=1|0] resizable[=yes|no]|[=1|0] copyhistory[=yes|no]|[=1|0] width=pixels height=pixels
You may use any subset of these options. Separate options with a comma. Do not put spaces between the options.
pixels is a positive integer specifying the dimension in pixels.
NOTE: In event handlers, you must specify window.open() unless you want to call document.open() because of the scoping of static objects in JavaScript.
The open method opens a new web browser window on the client, similar to choosing File|New Web Browser from the menu of the Navigator. The URL argument specifies the URL contained by the new window. If URL is an empty string, a new, empty window is created.
windowFeatures is an optional, comma-separated list of options for the new window. The boolean windowFeatures options are set to true if they are specified without values, or as yes or 1. For example, open("", "messageWindow", "toolbar") and open("", "messageWindow", "toolbar=1") both set the toolbar option to true. If windowName does not specify an existing window and you do not specify windowFeatures, all boolean windowFeatures are true by default.
Following is a description of the windowFeatures:
Description to be supplied.
In the following example, the windowOpener function opens a window and uses write methods to display a message:
function windowOpener() { msgWindow=open("","Display window","toolbar=no,directories=no,menubar=no"); msgWindow.document.write("<HEAD><TITLE>Message window</TITLE>"); msgWindow.document.write("<CENTER><BIG><B>Hello, world!</B>"); }
The following is an onClick event handler that opens a new client window displaying the content specified in the file sesame.html. It opens it with the specified option settings and names the corresponding window object newWin.
Notice the use of single quotes (') inside the onClick event handler.
Date.parse(date string)
The parse function takes a date string (such as "Dec 25, 1995"), and returns the number of milliseconds since January 1, 1970 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime method.
Given a string representing a time, parse returns the time value. It accepts the IETF standard date syntax: "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time zone abbreviations, but for general use, use a time zone offset, for example "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.
The parse function is a static method of Date. That means that you always use it as
Date.parse()
, rather than as a method of a date object you created.
If IPOdate is an existing date object, then
IPOdate.setTime(Date.parse("Aug 9, 1995"))
Returns arg1 to the arg2 power, i.e. arg1arg2.
pow(arg1, arg2)
xxx Examples to be supplied.
Displays an Prompt dialog box with a message and an input field
prompt(message, input default)
Use the prompt method to display a dialog box that takes user input. The arguments, message and input default are JavaScript values to be displayed as the message and the default value in the input field, respectively.
prompt("Enter the number of doughnuts you want:", 12)
Returns a pseudo-random number between zero and one. xxx NYI.
xxx To be supplied.
xxx Examples to be supplied.
Returns its argument, rounded to the nearest integer. In other words, if its argument is n.5 or greater (where n is an integer), returns n+1, otherwise returns n.
round(arg)
xxx Examples to be supplied.
For password, text, and textArea, selects the input area of the object.
select()
Use the select method to highlight the input area of a form element. You can use the select method with the focus method to highlight a field and position the cursor for a user response.
In the following example, the checkPassword function confirms that a user has entered a valid password. If the password is not valid, the select method highlights the password field and focus method returns focus to it so the user can re-enter the password.
function checkPassword(userPass) { if (badPassword) { alert("Please enter your password again.") userPass.focus() userPass.select() } }This example assumes that the password is defined as:
<INPUT TYPE=password NAME=userPass>
dateObj.setDate(day)
where dateObj
is a date object.
Sets the day of the month for the date object. The argument is an integer from 1 to 31.
The second statement below changes the day for theBigDay to the 24th of July from its original value.
theBigDay = new Date("July 27, 1962 23:30:00") Xmas95.setDate(24)
dateObj.setHours(hours
where dateObj
is a date object.
Sets the hours in the current time. The argument is an integer between 0 and 23.
theBigDay.setHours(7)
dateObj.setMinutes(minutes)
where dateObj
is a date object.
Sets the minutes in the current time. The argument is an integer between 0 and 59.
theBigDay.setMinutes(45)
dateObj.setMonth(month)
where dateObj
is a date object.
Sets the month in the current date. The argument is an integer between 0 and 11.
theBigDay.setMonth(6)
dateObj.setSeconds(seconds)
where dateObj
is a date object.
Sets the seconds in the current time. The argument is an integer between 0 and 59.
theBigDay.setSeconds(30)
dateObj.setTime(timevalue)
where dateObj
is a date object.
Sets the value of the date object. This argument is the number of milliseconds since the epoch (1 January 1970 00:00:00) You can use this method to help assign a date and time to another date object.
theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime())
Evaluates an expression after a specified number of milliseconds have elapsed.
timeoutID=setTimeout(expression, msec)
expression is a string expression.
msec is a numeric value or numeric string in millisecond units.
timeoutID is an identifier that is used only to cancel the evaluation with the clearTimeout method.
xxx Description to be supplied. Applies to
xxx Examples to be supplied.
dateObj.setYear(year)
where dateObj
is a date object.
Sets the year in the current date. Takes as its argument an integer greater than 1900.
theBigDay.setYear(96)
Returns the sine of its argument. The argument is in radians.
sin(arg)
xxx Examples to be supplied.
Causes the calling string object to be displayed in small font by surrounding it with HTML small font tags, <SMALL>....
small()
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the square root of its argument.
sqrt(arg)
xxx Examples to be supplied.
Causes the calling string object to be displayed as struck out text, by surrounding it with HTML strikeout tags, <STRIKE> ... </STRIKE>.
strike()
xxx Description to be supplied.
xxx Examples to be supplied.
Causes the calling string object to be displayed as a subscript by surrounding it with HTML subscript tags, <SUB> ... </SUB>.
sub()
xxx Description to be supplied.
xxx Examples to be supplied.
sup method
Submits the form.
submit()
xxx Description to be supplied.
xxx Examples to be supplied.
The substring method returns a subset of a string object. If a < b, then it returns the subset starting at character a and ending before character b, considering the first character of the string to be character zero (0). If a > b, then it returns the subset starting at character b and ending at character a. If a = b, then it returns the empty string.
substring(a, b)
xxx Description to be supplied.
If string x is "netscape" then x.substring(0,3) or x.substring(3,0) returns "net", and x.substring(4,7) or x.substring(7,4) returns "cap".
Causes the calling string object to be displayed as a superscript by surrounding it with HTML superscript tags, <SUP> ... </SUP>.
sup()
xxx Description to be supplied.
xxx Examples to be supplied.
Returns the tangent of its argument. The argument is in radians.
tan(arg)
xxx Examples to be supplied.
dateObj.toGMTString()
where dateObj
is a date object.
Converts a date to a String, using the Internet GMT conventions.
today.toGMTString()
If today is a date object, then toGMTString would convert the date to
GMT (UTC) using the operating system's time zone offset, and return a string
value in the following form:
Mon, 18 Dec 1995 17:28:35 GMT
dateObj.toLocaleString()
where dateObj
is a date object.
Converts a date to a String, using the locale conventions.
today.toLocaleString()
If today is a date object, then toLocaleString will return a string value
in the following form:
12/18/95 17:28:35
The toLowerCase method of string objects converts the string to all upper case.
toLowerCase()
xxx Description to be supplied.
For example, "ALPHABET".toLowerCase yields "alphabet".
For location, returns location.href, so you can use location as a string without typing ".href". For Date, converts a date to a string.
toString()
xxx Description to be supplied.
Here is an example of using toString with a date object, assuming theBigDay is a date object:
theBigDay.toString()
More Examples to be supplied.
The toUpperCase method of string objects converts the string to all upper case.
toUpperCase()
xxx Description to be supplied.
For example, "alphabet".toUpperCase yields "ALPHABET".
Date.UTC(year, month, mday [, hrs] [, min] [, sec])
UTC takes comma-delimited date parameters and returns the number of milliseconds since January 1, 1970 00:00:00, Universal Coordinated Time (GMT). The arameters are:
UTC is a static method of Date. That means that you always use it as
Date.UTC()
, rather than as a method of a date object you created.
The following statement creates a date object using GMT instead of local time:
gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0))
Write HTML to the current window, in sequence with the HTML containing this SCRIPT. SCRIPTs have no effect on paragraphs or other structures in which they may occur.
write()
xxx Description to be supplied.
xxx Examples to be supplied.
The same as write(), but adds a newline character. Note that this affects only preformatted text (inside a <PRE> or <XMP> tag).
writeln()
xxx Description to be supplied.
xxx Examples to be supplied.