JavaScript 文档

JavaScript的对象

The following objects are available in JavaScript:

NOTE: Each object topic indicates whether the object is part of the client (in Navigator), server (in LiveWire), or is common (built-in to JavaScript). Server objects are not included in this version of the documentation.


anchor object (client)

An anchor is a piece of text identified as the target of a hypertext link.

Syntax

To define an anchor, use standard HTML syntax :

<A NAME="anchorName"
   anchorText
</A>

NAME specifies a tag that becomes an available hypertext target within the current document.
anchorText specifies the text to display at the anchor.

Description

You can reference the anchor objects in your code by using the anchors property of the document object. The anchors property is an array that contains an entry for each anchor in a document.

xxx to be supplied

Properties

  • xxx to be supplied

    Methods

  • xxx to be supplied

    Event handlers

    None.

    Examples

       &amp;amp;lt;A NAME=&amp;amp;quot;javascript_intro&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;H2&amp;amp;gt;Welcome to JavaScript&amp;amp;lt;/H2&amp;amp;gt;&amp;amp;lt;/A&amp;amp;gt; 
     

    See also

  • link object
  • anchors property

    applet object (client)

    Represents a Java applet. xxx NYI.

    Syntax

    xxx to be supplied

    Description

    The applet object executes applets written in Java, not JavaScript. Java applets execute only on Netscape 2.0 and HotJava, and only on 32-bit platforms, such as Windows 95, Windows NT, and Unix. They do not execute on the 16-bit Windows platforms, such as Windows 3.1.

    Properties

  • length

    Methods

  • xxx to be supplied

    Event handlers

  • xxx to be supplied

    Examples

    xxx to be supplied

    See also

  • xxx to be supplied

    button object (client)

    A button object is a pushbutton on an HTML form.

    Syntax

    <INPUT
       TYPE="button"
       NAME="objectName"
       VALUE="buttonText"
       [onClick="handlerText"]>
    
    NAME specifies the name of the button object as a property of the enclosing form object and can be accessed using the name property. VALUE specifies the label to display on the button face and can be accessed using the value property.

    Description

    The button object is a custom button that you can use to perform an action you define.

    Properties

  • name
  • value

    Methods

  • click

    Event handlers

  • onClick

    Examples

    A custom button does not necessarily load a new page into the client; it merely executes the script specified by the onClick event handler. In the following example, myfunction() is a JavaScript function.

       &amp;amp;lt;INPUT TYPE=&amp;amp;quot;button&amp;amp;quot; VALUE=&amp;amp;quot;Calculate&amp;amp;quot; NAME=&amp;amp;quot;calc_button&amp;amp;quot; onClick=&amp;amp;quot;myfunction(this.form)&amp;amp;quot;&amp;amp;gt; 
     

    See also

  • reset and submit objects

    checkbox object (client)

    A checkbox object is a checkbox on an HTML form. A checkbox is a toggle switch that lets the user set a value on or off.

    Syntax

    To define a checkbox, use standard HTML syntax with the addition of the onClick event handler:

    <INPUT
       TYPE="checkbox"
       NAME="objectName"
       [CHECKED]
       [onClick="handlerText"]>
       textToDisplay
    

    Description

    Use the checked property to specify whether the checkbox is currently checked. Use the defaultChecked property to specify whether the checkbox is checked when the form is loaded.

    Properties

  • checked
  • defaultChecked
  • name
  • value

    Methods

  • click

    Event handlers

  • onClick

    Examples

       &amp;amp;lt;B&amp;amp;gt;Specify your music preferences (check all that apply):&amp;amp;lt;/B&amp;amp;gt; &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;checkbox&amp;amp;quot; NAME=&amp;amp;quot;musicpref_rnb&amp;amp;quot; CHECKED&amp;amp;gt; R&amp;amp;amp;B &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;checkbox&amp;amp;quot; NAME=&amp;amp;quot;musicpref_jazz&amp;amp;quot; CHECKED&amp;amp;gt; Jazz &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;checkbox&amp;amp;quot; NAME=&amp;amp;quot;musicpref_blues&amp;amp;quot; CHECKED&amp;amp;gt; Blues &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;checkbox&amp;amp;quot; NAME=&amp;amp;quot;musicpref_newage&amp;amp;quot; CHECKED&amp;amp;gt; New Age 
     

    See also

  • xxx to be supplied

    Date object (common)

    JavaScript has a date object that enables you to work with dates and times. JavaScript handles dates very similar to the way Java handles dates: They have many of the same date methods, and both languages store dates internally as the number of milliseconds since January 1, 1970 00:00:00.

    Syntax

    To create a date object:

    varName = new Date(parameters)
    
    where varName is a JavaScript variable name for the date object being created; it can be a new object or a property of an existing object.

    The parameters for the Date constructor can be any of the following:

    • Nothing: creates today's date and time. For example, today = new Date()
    • A string representing a date in the following form: "Month day, year hours:minutes:seconds". For example, Xmas95= new Date("December 25, 1995 13:30:00") If you omit hours, minutes, or seconds, the value will be set to zero.
    • A set of integer values for year, month, and day. For example, Xmas95 = new Date(95,11,25)
    • A set of values for year, month, day, hour, minute, and seconds For example, Xmas95 = new Date(95,11,25,9,30,0)

    To use date methods:

    dateObj.methodName(parameters)
    

    Exceptions: The UTC and parse methods of date are static methods that you use as follows:

    Date.UTC(params)
    Date.parse(params)
    

    Description

    JavaScript does not have a date data type. However, the date object and its methods enable you to work with dates and times in your applications.

    The date object has a large number of methods for setting, getting, and manipulating dates.

    Properties

    None.

    Methods

  • getDate
  • getDay
  • getHours
  • getMinutes
  • getMonth
  • getSeconds
  • getTime
  • getTimeZoneoffset
  • getYear
  • parse
  • setDate
  • setHours
  • setMinutes
  • setMonth
  • setSeconds
  • setYear
  • toString
  • toGMTString
  • toLocaleString
  • UTC

    Event handlers

    None. Built-in objects do not have event handlers.

    Examples

    xxx to be supplied

    See also

  • xxx to be supplied

    document object (client)

    The document object contains information on the current document.

    Syntax

    To define a document object, use standard HTML syntax with the addition of the onLoad and onUnLoad event handlers:

    <BODY
       BACKGROUND="backgroundImage"
       BGCOLOR="#backgroundColor"
       FGCOLOR="#foregroundColor"
       LINK="#unfollowedLinkColor"
       ALINK="#activatedLinkColor"
       VLINK="#followedLinkColor"
       [onLoad="handlerText"]
       [onUnLoad="handlerText"]>
    </BODY>
    

    BGCOLOR, FGCOLOR, LINK, ALINK, and VLINK are color names or color specifications in the format "#rrggbb".

    Description

    The <BODY>...</BODY> tag encloses an entire document, which is defined by the current URL. The entire body of the document (all other HTML elements for the document) goes within the <BODY>...</BODY> tag.

    You can reference the anchors, forms, and links of a document by using the anchors, forms, and links properties. These properties are arrays that contain an entry for each anchor, form, or link in a document.

    The document object's title property reflects the contents of <TITLE>...</TITLE>. Other properties reflect the contents of the document; for example, bgColor reflects the background color, and lastModified reflects the time last modified. Some of the properties are reflections from HTML attributes; for example, the links property is a reflection of all the links in the document, and the forms property is a reflection of all the forms in the document.

    Properties

  • alinkColor
  • anchors
  • bgColor
  • fgColor
  • forms
  • lastModified
  • linkColor
  • links
  • loadedDate
  • location
  • referrer
  • title
  • vlinkColor

    Methods

  • clear
  • close
  • open
  • write
  • writeln

    Event handlers

  • onLoad
  • onUnLoad

    Examples

    xxx to be supplied

    See also

  • window object

    form object (client)

    A form lets users input text and make choices from form objects such as checkboxes, radio buttons, and selection lists. You can also use a form to post data to or retrieve data from a server.

    Syntax

    To define a form, use standard HTML syntax with the addition of the onSubmit event handler:

    <FORM
       NAME="objectName
       TARGET="windowName"
       ACTION="serverURL"
       METHOD=GET | POST
       [onSubmit="handlerText"]>
    </FORM>
    

    TARGET specifies the window that form responses go to. When you submit a form with a TARGET attribute, instead of seeing the server's responses in the same window that contained the form, you see them in a (possibly new) window.
    ACTION specifies the URL of the server to which form field input information is sent. METHOD specifies how information is sent to the server specified by ACTION. GET (the default) appends the input information to the URL which on most receiving systems becomes the value of the environment variable QUERY_STRING. POST sends the input information in a data body which is available on stdin with the data length set in the environment variable CONTENT_LENGTH.

    Description

    Each form in a document corresponds to a distinct object.

    You can reference the form objects in your code by using the forms property of the document object. The forms property is an array that contains an entry for each form in a document.

    You can reference a form's elements in your code by using the elements property. The elements property is an array that contains an entry for each element (such as a checkbox, radioButton, or text object) in a form.

    Properties

  • action
  • elements
  • method
  • name
  • target

    Methods

  • submit

    Event handlers

  • onSubmit

    Examples

    xxx to be supplied

    See also

  • elements, forms properties

    history object (client)

    The history object contains information on the URLs that the client has visited. This information is stored in a history list, and is accessible through the Navigator's Go menu.

    Syntax

    history.go(delta)
    delta is an integer representing the offset of the destination URL in the history list.

    Description

    The history object is a linked list of URLs the user has visited, as shown in the Navigator's Go menu.

    Properties

  • current
  • length

    Methods

  • back
  • forward
  • go

    Event handlers

    None.

    Examples

    The following example goes to the URL the user visited three clicks ago.

       history.go(-3) 
     

    See also

  • xxx to be supplied

    link object (client)

    A link is a piece of text identified as a hypertext link. When the user clicks the link text, the link hypertext reference is loaded into its target window.

    Syntax

    To define a link, use standard HTML syntax with the addition of the onClick and onMouseOver event handlers:

    <A HREF=locationOrURL
       TARGET="windowName"
       [onClick="handlerText"]
       [onMouseOver="handlerText"]>
       linkText
    </A>
    

    HREF identifies a destination anchor or URL.
    TARGET specifies the window that the link is loaded into.
    linkText is rendered as a hypertext link to the URL.

    Description

    Each link object is a location object.

    You can reference the link objects in your code by using the links property of the document object. The links property is an array that contains an entry for each link in a document.

    Properties

  • target

    Methods

  • xxx to be supplied

    Event handlers

  • onClick
  • onMouseOver

    Examples

    The following example creates a hypertext link to an anchor named javascript_intro.

       &amp;amp;lt;A HREF=&amp;amp;quot;#javascript_intro&amp;amp;quot;&amp;amp;gt;Introduction to JavaScript&amp;amp;lt;/A&amp;amp;gt; 
     

    The following example creates a hypertext link to a URL.

       &amp;amp;lt;A HREF=&amp;amp;quot;http://www.netscape.com&amp;amp;quot;&amp;amp;gt;Netscape Home Page&amp;amp;lt;/A&amp;amp;gt; 
     

    See also

  • anchor object
  • links property

    location object (client)

    The location object contains information on the current URL.

    Syntax

    location.property
    property is one of the properties listed below.

    Description

    xxx to be supplied

    Properties

  • hash
  • host
  • hostname
  • href
  • pathname
  • port
  • protocol
  • search

    Methods

  • assign
  • toString

    Event handlers

    None.

    Examples

    xxx to be supplied

    See also

  • xxx to be supplied

    Math object (common)

    The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.

    Syntax

    Math.propertyName
    or
    Math.methodName(parameters)
    

    Description

    You reference the constant PI as Math.PI. Constants are defined with the full precision of real numbers in JavaScript.

    Similarly, you reference Math functions as methods. For example, the sine function is Math.sin(argument), where argument is the argument.

    It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,

    with Math {
       a = PI * r*r;
       y = r*sin(theta)
       x = r*cos(theta)
    }
    

    Properties

  • E
  • LN10
  • LN2
  • PI
  • SQRT1_2
  • SQRT2

    Methods

  • abs
  • acos
  • asin
  • atan
  • ceil
  • cos
  • exp
  • floor
  • log
  • max
  • min
  • pow
  • random
  • round
  • sin
  • sqrt
  • tan

    Event handlers

    None. Built-in objects do not have event handlers.

    Examples

    xxx to be supplied

    See also

  • xxx to be supplied

    password object (client)

    A password object is a text field on an HTML form. When the user enters text into the field, asterisks (*) hide anything entered from view.

    Syntax

    To define a password object, use standard HTML syntax:

    <INPUT
       TYPE="password"
       NAME="objectName"
       [VALUE="textValue"]
       SIZE=integer
    

    Description

    xxx to be supplied

    Properties

  • defaultValue
  • name
  • value

    Methods

  • focus
  • blur
  • select

    Event handlers

    None.

    Examples

     

       &amp;amp;lt;B&amp;amp;gt;Password:&amp;amp;lt;/B&amp;amp;gt; &amp;amp;lt;INPUT TYPE=&amp;amp;quot;password&amp;amp;quot; NAME=&amp;amp;quot;password&amp;amp;quot; VALUE=&amp;amp;quot;&amp;amp;quot; SIZE=25&amp;amp;gt; 
     

    See also

  • xxx to be supplied

    radioButton object (client)

    A radioButton object is a set of radio buttons on an HTML form. A set of radio buttons lets the user choose one item from a list.

    Syntax

    To define a set of radio buttons, use standard HTML syntax with the addition of the onClick event handler:

    <INPUT
       TYPE="radio"
       NAME="objectName"
       VALUE="buttonValue"
       [CHECKED]
       [onClick="handlerText"]>
       textToDisplay
    

    NAME should contain the same value for all radio buttons in a group.

    Description

    All radio buttons in a radio button group use the same name property. To access the individual radio buttons in your code, follow the object name with an index starting from zero, one for each button the same way you would for an array such as forms: document.forms[0].objectName[0] is the first, document.forms[0].objectName[1] is the second, etc.

    Properties

  • checked
  • defaultChecked
  • index
  • length
  • name
  • value

    Methods

  • click

    Event handlers

  • onClick

    Examples

    The following example defines a radio button group to choose among three catalogs. Each radio button is given the same name, NAME="choice", forming a group of buttons for which only one choice can be selected. The example also defines a text field that defaults to what was chosen via the radio buttons but that allows the user to type a nonstandard catalog name as well. JavaScript automatically sets the catalog name input field based on the radio buttons.

       &amp;amp;lt;INPUT TYPE=&amp;amp;quot;text&amp;amp;quot; NAME=&amp;amp;quot;catalog&amp;amp;quot; SIZE=&amp;amp;quot;20&amp;amp;quot;&amp;amp;gt; &amp;amp;lt;INPUT TYPE=&amp;amp;quot;radio&amp;amp;quot; NAME=&amp;amp;quot;choice&amp;amp;quot; VALUE=&amp;amp;quot;PRICEY&amp;amp;quot; onClick=&amp;amp;quot;form.catalog.value = 'top-of-the-line'&amp;amp;quot;&amp;amp;gt; It's really great! &amp;amp;lt;INPUT TYPE=&amp;amp;quot;radio&amp;amp;quot; NAME=&amp;amp;quot;choice&amp;amp;quot; VALUE=&amp;amp;quot;MODEST&amp;amp;quot; onClick=&amp;amp;quot;form.catalog.value = 'middle-of-the-road'&amp;amp;quot;&amp;amp;gt; It's pretty good. &amp;amp;lt;INPUT TYPE=&amp;amp;quot;radio&amp;amp;quot; NAME=&amp;amp;quot;choice&amp;amp;quot; VALUE=&amp;amp;quot;CHEAPO&amp;amp;quot; onClick=&amp;amp;quot;form.catalog.value = 'bargain-basement'&amp;amp;quot;&amp;amp;gt; It stinks! 
     

    See also

  • selection object

    reset object (client)

    A reset object is a reset button on an HTML form.

    Syntax

    To define a reset button, use standard HTML syntax with the addition of the onClick event handler:

    <INPUT
       [NAME="objectName"]
       TYPE="reset"
       VALUE="buttonText"
       [onClick="handlerText"]>
    
    VALUE specifies the text to display on the button face and can be accessed using the value property.

    Description

    A reset button resets all elements in a form to their defaults.

    Properties

  • name
  • value

    Methods

  • click

    Event handlers

  • onClick

    Examples

    The following example displays a text object containing "CA". If the user types a different state abbreviation in the text object and then clicks the Clear Form button, the original value of "CA" is restored.

       &amp;amp;lt;B&amp;amp;gt;State: &amp;amp;lt;/B&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;text&amp;amp;quot; NAME=&amp;amp;quot;state&amp;amp;quot; VALUE=&amp;amp;quot;CA&amp;amp;quot; SIZE=&amp;amp;quot;2&amp;amp;quot;&amp;amp;gt; &amp;amp;lt;P&amp;amp;gt;&amp;amp;lt;INPUT TYPE=&amp;amp;quot;reset&amp;amp;quot; VALUE=&amp;amp;quot;Clear Form&amp;amp;quot;&amp;amp;gt; 
     

    See also

  • button and submit objects

    selection object (client)

    A selection object is a selection list or scrolling list on an HTML form. A selection list lets the user choose one item from a list. A scrolling list lets the user choose one or more items from a list.

    Syntax

    To define a selection object, use standard HTML syntax with the addition of the onBlur, onChange, and onFocus event handlers:

    <SELECT
       NAME="objectName"
       [SIZE="value"]
       [MULTIPLE]
       [onBlur="handlerText"]
       [onChange="handlerText"]
       [onFocus="handlerText"]>
       <OPTION [SELECTED]> textToDisplay [ ... <OPTION> textToDisplay]
    </SELECT>
    

    SIZE specifies the number of options visible when the form is displayed.

    Description

    You can reference the options of a selection object in your code by using the options property. The options property is an array that contains an entry for each option in a selection object. Each option has the properties listed below.

    The options on selection objects can be updated dynamically. xxx NYI.

    Properties

    The selection object has the following properties:

  • options
  • selectedIndex

    The options property has the following properties:

  • defaultSelected
  • index
  • selected
  • text
  • value

    Methods

  • click

    Event handlers

  • onBlur
  • onChange
  • onFocus

    Examples

    The following example displays a selection list.

       Choose the music type for your free CD: &amp;amp;lt;SELECT NAME=&amp;amp;quot;music_type_single&amp;amp;quot;&amp;amp;gt; &amp;amp;lt;OPTION SELECTED&amp;amp;gt; R&amp;amp;amp;B &amp;amp;lt;OPTION&amp;amp;gt; Jazz &amp;amp;lt;OPTION&amp;amp;gt; Blues &amp;amp;lt;OPTION&amp;amp;gt; New Age&amp;amp;lt;/SELECT&amp;amp;gt; &amp;amp;lt;P&amp;amp;gt;Choose the music types for your free CDs: &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;SELECT NAME=&amp;amp;quot;music_type_multi&amp;amp;quot; MULTIPLE&amp;amp;gt; &amp;amp;lt;OPTION SELECTED&amp;amp;gt; R&amp;amp;amp;B &amp;amp;lt;OPTION&amp;amp;gt; Jazz &amp;amp;lt;OPTION&amp;amp;gt; Blues &amp;amp;lt;OPTION&amp;amp;gt; New Age&amp;amp;lt;/SELECT&amp;amp;gt; 
     

    See also

  • radioButton object
  • options property

    string object (common)

    A string object consists of a series of characters.

    Syntax

    stringName.property|method
    stringName is the name of a string variable.
    property is one of the properties listed below.
    method is one of the methods listed below.

    Description

    A string can be represented as a literal enclosed by single or double quotes; for example, "Netscape" or 'Netscape'.

    Properties

  • length

    Methods

  • anchor
  • big
  • blink
  • bold
  • charAt
  • fixed
  • fontcolor
  • fontsize
  • indexOf
  • italics
  • lastIndexOf
  • link
  • small
  • strike
  • sub
  • substring
  • sup
  • toLowerCase
  • toUpperCase

    Event handlers

    None. Built-in objects do not have event handlers.

    Examples

    The following statement creates a string variable.

       var last_name = &amp;amp;quot;Schaefer&amp;amp;quot; 
     

    last_name.length is 8.
    last_name.toUpperCase() is "SCHAEFER".
    last_name.toLowerCase() is "schaefer".

    See also

  • text and textArea objects

    submit object (client)

    A submit object is a submit button on an HTML form.

    Syntax

    To define a submit button, use standard HTML syntax with the addition of the onClick event handler:

    <INPUT
       TYPE="submit"
       NAME="objectName"
       VALUE="buttonText"
       [onClick="handlerText"]>
    
    VALUE specifies the text to display on the button face and can be accessed using the value property.

    Description

    A submit button causes a form to be submitted.

    Clicking a submit button submits a form to the program specified by the form's action property. This action always loads a new page into the client; it may be the same as the current page, if the action so specifies or is not specified.

    Properties

  • name
  • value

    Methods

  • click

    Event handlers

  • onClick

    Examples

       &amp;amp;lt;INPUT TYPE=&amp;amp;quot;submit&amp;amp;quot; NAME=&amp;amp;quot;submit_button&amp;amp;quot; VALUE=&amp;amp;quot;Done&amp;amp;quot;&amp;amp;gt; 
     

    See also

  • reset and button objects

    text object (client)

    A text object is a text input field on an HTML form. A text field lets the user enter a word, phrase, or series of numbers.

    Syntax

    To define a text object, use standard HTML syntax with the addition of the onBlur, on Change, onFocus, and onSelect event handlers:

    <INPUT
       TYPE="text"
       NAME="objectName"
       VALUE="textValue"
       SIZE=integer
       [onBlur="handlerText"]
       [onChange="handlerText"]
       [onFocus="handlerText"]
       [onSelect="handlerText"]>
    

    Description

    text objects can be updated dynamically by setting this.value

    xxx to be supplied

    Properties

  • defaultValue
  • name
  • value

    Methods

  • focus
  • blur
  • select

    Event handlers

  • onBlur
  • onChange
  • onFocus
  • onSelect

    Examples

       &amp;amp;lt;B&amp;amp;gt;Last name:&amp;amp;lt;/B&amp;amp;gt; &amp;amp;lt;INPUT TYPE=&amp;amp;quot;text&amp;amp;quot; NAME=&amp;amp;quot;last_name&amp;amp;quot; VALUE=&amp;amp;quot;&amp;amp;quot; SIZE=25&amp;amp;gt; 
     

    See also

  • textArea and string objects

    textArea object (client)

    A textArea object is a multiline input field on an HTML form. A textArea field lets the user enter words, phrases, or numbers.

    Syntax

    To define a text area, use standard HTML syntax with the addition of the onBlur, onChange, onFocus, and onSelect event handlers:

    <TEXTAREA
       NAME="objectName"
       ROWS="integer"
       COLS="integer"
       [onBlur="handlerText"]
       [onChange="handlerText"]
       [onFocus="handlerText"]
       [onSelect="handlerText"]>
       textToDisplay
    </TEXTAREA>
    

    textToDisplay allows only ASCII text, and new lines are respected.
    ROWS and COLS define the physical size of the displayed input field in numbers of characters.

    Description

    textArea objects can be updated dynamically. xxx need to explain how. (see text object)

    Properties

  • defaultValue
  • name
  • value

    Methods

  • focus
  • blur
  • select

    Event handlers

  • onBlur
  • onChange
  • onFocus
  • onSelect

    Examples

       &amp;amp;lt;B&amp;amp;gt;Description:&amp;amp;lt;/B&amp;amp;gt; &amp;amp;lt;BR&amp;amp;gt;&amp;amp;lt;TEXTAREA NAME=&amp;amp;quot;item_description&amp;amp;quot; ROWS=6 COLS=55&amp;amp;gt; Our storage ottoman provides an attractive way to store lots of CDs and videos--and it's versatile enough to store other things as well. It can hold up to 72 CDs under the lid and 20 videos in the drawer below. &amp;amp;lt;/TEXTAREA&amp;amp;gt; 
     

    See also

  • text and string objects

    window object (client)

    A window object is the top-level object for each document, location, and history object group.

    Syntax

    xxx to be supplied

    Description

    The window object is the top-level object in the JavaScript client hierarchy. Because the existence of the current window is assumed, you don't have to reference the name of the window when you call its methods and assign its properties. For example, status="Jump to a new location" is a valid property assignment, and close() is a valid method call.

    The self and window properties are synonyms for the current window, and you can optionally use them to refer to the current window. For example, you can close the current window by calling either window.close() or self.close(). You can use these properties to make your code more readable, or to disambiguate the property assignment self.status from a form called status.

    See the methods and properties listed below for more examples.

    You can reference a window's frame objects in your code by using the frames property. The frames property is an array that contains an entry for each frame in a window.

    Properties

  • frames
  • parent
  • self
  • top
  • status
  • defaultStatus

    Methods

  • alert
  • close
  • confirm
  • open
  • prompt
  • setTimeout
  • clearTimeout

    Event handlers

  • onLoad
  • onUnload

    Examples

    xxx to be supplied

    See also

  • document object
  • frames property


     

    JavaScript 的对象属性

    以下的属性可在 JavaScript中用到:


    action property

    String value of a FORM tag's ACTION attribute.

     

    Applies to

    form

    Examples

    xxx Examples to be supplied.


    alinkColor property

    RGB value for color of activated links (after mouse-button down, but before mouse-button up), expressed as a hexadecimal triplet. This property is the JavaScript reflection of the ALINK attribute of the HTML BODY tag.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    anchors property

    Array of objects corresponding to named anchors (<A NAME=""> tags) in source order.

    The anchors array contains an entry for each anchor in a document. For example, if a document contains three anchors, these anchors are reflected as document.anchors[0], document.anchors[1], and document.anchors[2].

    To obtain the number of anchors in a document, use the length property: document.anchors.length.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.

    See also

  • length property

    bgColor property

    RGB value of background color, expressed as a hexadecimal triplet. This property is the JavaScript reflection of the BGCOLOR attribute of the HTML BODY tag.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    checked property

    For checkbox, Boolean, false if not checked, true if checked. For radioButton, Boolean, false if not pressed, true if pressed.

     

    Applies to

    checkbox, radioButton

    Examples

    xxx To be supplied.


    current property

    URL of current page. xxx Not yet implemented in Beta 4.

     

    Applies to

    history

    Examples

    xxx Examples to be supplied.


    defaultChecked property

    For checkbox, Boolean property that indicates if the element is selected by default, by the CHECKED attribute. For radioButton, Boolean property that indicates if the element is selected by default, by the CHECKED attribute.

     

    Applies to

    checkbox, radioButton

    Examples

    xxx Examples to be supplied.


    defaultSelected property

    Boolean property that indicates if the option is selected by default, by the presence of the SELECTED attribute in the HTML OPTION tag.

     

    Applies to

    selection

    Examples

    xxx Examples to be supplied.


    defaultStatus property

    For a window, the defaultStatus property reflects the default message displayed in the status bar at the bottom of the window. Do not confuse defaultStatus with status. The status property reflects a priority or transient message in the status bar, such as the message that appears when a mouseOver event occurs over an anchor.

     

    Applies to

    window

    Examples

    xxx Examples to be supplied.


    defaultValue property

    For text and textArea, string, the initial contents of the field.

     

    Applies to

    password, text, textArea

    Examples

    xxx Examples to be supplied.


    E property

    E is Euler's constant, the base of natural logarithms, roughly 2.718.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    elements property (client)

    Array of objects corresponding to form elements (such as checkbox, radioButton, and text objects) in source order.

    The elements array contains an entry for each object in a form. For example, if a form has a text field, a radio button group, and a checkbox, these elements are reflected as formName.elements[0], formName.elements[1], and formName.elements[2].

     

    Applies to

    form

    Examples

    xxx Examples to be supplied.


    fgColor property

    RGB value of foreground (text) color, expressed as a hexadecimal triplet. This property is the JavaScript reflection of the FGCOLOR attribute of the HTML BODY tag.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    forms property

    Array of objects corresponding to named forms (<FORM NAME=""> tags) in source order.

    The forms array contains an entry for each form object in a document. For example, if a document contains three forms, these forms are reflected as document.forms[0], document.forms[1], and document.forms[2].

    You can refer to a form's elements by using the forms array. For example, you would refer to a text object named quantity in the second form as:

    document.forms[1].quantity
    
    You would refer to the value property of this text object as:
    document.forms[1].quantity.value
    

    To obtain the number of forms in a document, use the length property: document.forms.length.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.

    See also

  • length property

    frames property

    Array of objects corresponding to child frame windows (<FRAMESET> tag) in source order.

    The frames array contains an entry for each child frame in a window. For example, if a window contains three child frames, these frames are reflected as window.frames[0], window.frames[1], and window.frames[2].

    To obtain the number of number of child frames in a window, use the length property: window.frames.length.

     

    Applies to

    window

    Examples

    xxx Examples to be supplied.

    See also

  • length property

    hash property

    The anchor name following the # symbol.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    host property

    The hostname:port part of the location or URL.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.

    See also

  • hostname

    hostname property

    The hostname part of the location or URL.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.

    See also

  • host

    href property

    The entire URL as a JavaScript string.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    index property

    For radioButton, number, the ordinal number of the radioButton field, 0-based. For a select object option, the number identifying the position of the option in the selection, starting from zero.

     

    Applies to

    radioButton, selection

    Examples

    xxx Examples to be supplied.


    lastModified property

    A string containing the last-modified date.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    length property

    For a history object, the length of the history list. For a string object, the integer length of the string. For a radioButton object, the number of radio buttons in the object. For an anchors, forms, frames, links, or options array, the number of elements in the array.

    For a null string, length is zero.

     

    Applies to

  • applet, history, radioButton, string objects
  • anchors, forms, frames, links, options properties

    Examples

    xxx Example with history to be supplied.

    If the string object mystring is "netscape", then mystring.length returns the integer 8.

    If the current document contains five forms, then document.forms.length returns the integer 5.


    linkColor property

    RGB value for color of hyperlinks, expressed as a hexadecimal triplet.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    links property

    Array of objects corresponding to link objects (<A HREF=URL> tags) in source order.

    The links array contains an entry for each link object in a document. For example, if a document contains three link objects, these links are reflected as document.links[0], document.links[1], and document.links[2].

    To obtain the number of links in a document, use the length property: document.links.length.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.

    See also

  • length property

    LN2 property

    LN2 is the natural logarithm of two, roughly 0.693.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    LN10 property

    LN10 is the natural logarithm of ten, roughly 2.302.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    loadedDate property

    xxx Description to be supplied. Not yet implemented in Beta 4.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    location property

    The full URL of the document.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    method property

    Value of METHOD attribute, "get" or "post".

     

    Applies to

    form

    Examples

    xxx Examples to be supplied.


    name property

    A string whose value is the same as the NAME attribute of the object. Note that for button, reset, and submit objects, this is the internal name for the button, not the label that appears onscreen.

     

    Applies to

    button, checkbox, form, password, radioButton, reset, submit, text, textArea

    Examples

    xxx Examples to be supplied.


    options property

    Array of objects corresponding to options in a selection object (<OPTION> tags) in source order.

    The options array contains an entry for each option in a selection object. For example, if a selection object named musicStyle contains three options, these options are reflected as musicStyle.options[0], musicStyle.options[1], and musicStyle.options[2].

    To obtain the number of options in a selection object, use the length property: objectName.options.length.

     

    Applies to

    selection

    Examples

    xxx Examples to be supplied.

    See also

  • length property

    parent property

    In a <FRAMESET> and <FRAME> relationship, the <FRAMESET> window.

     

    Applies to

    window

    Examples

    xxx Examples to be supplied.


    pathname property

    The file or object path name following the third slash.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    PI property

    Pi is the ratio of the circumference of a circle to its diameter, roughly 3.1415.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    port property

    The port number in a URL, if any; otherwise "".

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    protocol property

    The initial substring up to and including the first colon, which indicates the URL's access method.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    referrer property

    xxx Description to be supplied.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    search property

    Any query string or form data after ?.

     

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    selected property

    Boolean property that indicates the current selected state of an option in a select object.

     

    Applies to

    selection

    Examples

    xxx Examples to be supplied.


    selectedIndex property

    xxx to be described

     

    Applies to

    selection

    Examples

    xxx Examples to be supplied.


    self property

    The self property refers to the current window. Use the self property to disambiguate a window property from a form of the same name. You can also use the self property to make your code more readable.

     

    Applies to

    window

    Examples

    In the following example, self.status is used to set the status property. This usage disambiguate the status property of a window from a form called "status".

       &amp;amp;lt;A HREF=&amp;amp;quot;&amp;amp;quot; onClick=&amp;amp;quot;this.href=pickRandomURL();&amp;amp;quot; onMouseOver=&amp;amp;quot;self.status='Pick a random URL' ; return true&amp;amp;quot;&amp;amp;gt; Go!&amp;amp;lt;/A&amp;amp;gt; 
     

    See also

  • window property

    SQRT1_2 property

    SQRT1_2 is the square root of one-half; equivalently, one over the square root of two, roughly 0.707.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    SQRT2 property

    SQRT2 is the square root of two, roughly 1.414.

     

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    status property

    For a window, the status property reflects a priority or transient message in the status bar at the bottom of the window, such as the message that appears when a mouseOver event occurs over an anchor. Do not confuse status with defaultStatus. The defaultStatus property reflects the default message displayed in the status bar.

     

    Applies to

    window

    Examples

    Suppose you have created a JavaScript function called pickRandomURL() that lets you select a URL at random. You can use the onClick event handler of an anchor to specify a value for the HREF attribute of the anchor dynamically, and the onMouseOver event handler to specify a custom message for the window in the status property:

       &amp;amp;lt;A HREF=&amp;amp;quot;&amp;amp;quot; onClick=&amp;amp;quot;this.href=pickRandomURL();&amp;amp;quot; onMouseOver=&amp;amp;quot;self.status='Pick a random URL'; return true&amp;amp;quot;&amp;amp;gt; Go!&amp;amp;lt;/A&amp;amp;gt; 
     

    In the above example, the status property of the window is assigned to the window's self property, as self.status. As this example shows, you must return true to set the status property in the onMouseOver event handler.


    target property

    Window targeted for form response after the form has been submitted.

     

    Applies to

    form, link

    Examples

    xxx Examples to be supplied.


    text property

    String, reflection of the text after the <OPTION> tag.

     

    Applies to

    selection

    Examples

    xxx Examples to be supplied.


    title property

    Current document title.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    top property

    The top-most ancestor window, which is its own parent.

     

    Applies to

    window

    Examples

    xxx Examples to be supplied.


    value property

    For button, reset, and submit objects, a string that is the same as the VALUE attribute (this is the label that appears onscreen, not the internal name for the button). For checkbox, a string, "on" if item is checked; "off" otherwise. For radioButton, a string, reflection of the VALUE attribute. For selection, reflection of VALUE attribute, sent to server on submit. For text and textArea, string, the contents of the field.

    Applies to

    button, checkbox, password, radioButton, reset, selection, submit, text, textArea

    Examples

    xxx Examples to be supplied.


    vlinkColor property

    RGB value for color of visited links, expressed as a hexadecimal triplet. This property is the JavaScript reflection of the VLINK attribute of the HTML BODY tag.

     

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    window property

    The window property refers to the current window. Use the window property to disambiguate a property of the window object from a form of the same name. You can also use the window property to make your code more readable.

     

    Applies to

    window

    Examples

    In the following example, window.status is used to set the status property. This usage disambiguate the status property of a window from a form called "status".

       &amp;amp;lt;A HREF=&amp;amp;quot;&amp;amp;quot; onClick=&amp;amp;quot;this.href=pickRandomURL();&amp;amp;quot; onMouseOver=&amp;amp;quot;window.status='Pick a random URL' ; return true&amp;amp;quot;&amp;amp;gt; Go!&amp;amp;lt;/A&amp;amp;gt; 
     

    See also

  • self property
  •  

    JavaScript的方法

    以下的一些"方法"可用在JavaScript:


    abs method

    Returns the absolute value of its argument.

    Syntax

    abs(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    acos method

    Returns the arc cosine (in radians) of its argument.

    Syntax

    acos(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • asin, atan, cos, sin, tan methods

    alert method

    Displays an Alert dialog box with a message and an OK button.

    Syntax

    alert("message")

    The argument message is any string.

    Description

    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.

    Applies to

    window

    Examples

    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)">

    See also

  • confirm, prompt methods

    anchor method

    Causes the calling string object to be a named anchor by surrounding it with HTML anchor tags, <A NAME=...> </A>.

    Syntax

    string.anchor(name)

    The argument name can be any string.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • link method

    asin method

    Returns the arc sine (in radians) of its argument.

    Syntax

    asin(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • acos, atan, cos, sin, tan methods

    assign method

    Sets location.href so you don't have to type ".href" complements toString().

    Syntax

    assign()

    Description

    Do not call the assign method. It exists so you can use 'location=...' instead of 'location.href=...'. xxx Description to be supplied.

    Applies to

    location

    Examples

    xxx Examples to be supplied.


    atan method

    Returns the arc tangent (in radians) of its argument.

    Syntax

    atan(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • acos, asin, cos, sin, tan methods

    back method

    Loads the previous URL in the history list.

    Syntax

    back()

    Description

    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).

    Applies to

    history

    Examples

    xxx Examples to be supplied.

    See also

  • forward, go methods

    big method

    Causes the calling string object to be displayed in big font by surrounding it with HTML big font tags, <BIG> and </BIG>.

    Syntax

    big()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • fontsize, small methods

    blink method

    Causes the calling string object to blink by surrounding it with HTML blink tags, <BLINK> ... </BLINK>.

    Syntax

    blink()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • bold, italics, strike methods

    blur method

    For password, text, and textArea, removes focus to the object.

    Syntax

    blur()

    Description

    Use the blur method to remove focus from a specific form element.

    Applies to

    password, text, textArea

    Examples

    xxx Examples to be supplied.

    See also

  • focus, select methods

    bold method

    Causes the calling string object to be displayed as bold by surrounding it with HTML bold tags, <B> ... </B>.

    Syntax

    bold()
    NOTE: For Beta4, use B() for this method.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • blink, italics, strike methods

    ceil method

    Returns the least integer greater than or equal to its argument.

    Syntax

    ceil(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • floor method

    charAt method

    Returns the character at the specified index.

    Syntax

    charAt(index)

    index is any integer from 0 to string.length() - 1.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • indexOf, lastIndexOf methods

    clear method

    Clears the window.

    Syntax

    clear()

    Description

    xxx Description to be supplied.

    Applies to

    document

    Examples

    xxx Examples to be supplied.


    clearTimeout method

    Cancels a timeout that was set with the setTimeout method.

    Syntax

    clearTimeout(timeoutID)

    timeoutID is a timeout setting that was returned by a previous call to the setTimeout method.

    Description

    xxx Description to be supplied. Applies to

     

    window

    Examples

    xxx Examples to be supplied.

    See also

  • setTimeout

    click method

    Simulates a mouse click on the calling form element.

    Syntax

    click()

    Description

    The effect of the click method varies according to the calling element:

  • For button, reset, and submit, xxx.
  • For a radioButton, selects a radio button.
  • For a checkbox, checks the check box and sets its value to on.
  • For a selection, sets its value.

    Applies to

    button, checkbox, radioButton, reset, selection, submit

    Examples

    xxx Examples to be supplied.


    close method

    For document and window, closes the window. xxx Not yet implemented in Beta 4.

    Syntax

    close()

    Description

    Document description to be supplied.

    The close method closes the current window.

    Applies to

    document, window

    Examples

    Any of the following examples close the current window:

    window.close()
    self.close()
    close()

    See also

  • open method

    confirm method

    Displays a Confirm dialog box with the specified message and OK and Cancel buttons.

    Syntax

    confirm("message")

    The argument message is any string.

    Description

    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.

    Applies to

    window

    Examples

    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()">

    See also

  • alert, prompt methods

    cos method

    Returns the cosine of its argument. The argument is in radians.

    Syntax

    cos(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • acos, asin, atan, sin, tan methods

    eval function

    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.

    Syntax

    eval(expression)

    Description

    The eval function takes a JavaScript arthimetic expression as its argument and returns the value of the argument as a number.

    Example

    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)
    

    exp method

    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.

    Syntax

    exp(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • log method

    fixed method

    Causes the calling string object to be displayed in fixed-pitch font in HTML by surrounding it with typewriter text tags, <fixed> ... </fixed>.

    Syntax

    fixed()
    NOTE: For Beta4, use TT() for this method.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.


    floor method

    Returns the greatest integer less than or equal to its argument.

    Syntax

    floor(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • ceil method

    focus method

    For password, text, and textArea, gives focus to the object.

    Syntax

    focus()

    Description

    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.

    Applies to

    password, text, textArea

    Examples

    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>

    See also

  • blur, select methods

    fontcolor method

    Causes the calling string object to be displayed in the specified color by surrounding it with HTML font color tags, <FONTCOLOR=color> ... </FONTCOLOR>.

    Syntax

    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.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.


    fontsize method

    Causes the calling string object to be displayed in the specified font size by surrounding it with HTML font size tags, <FONTSIZE=size> ... </FONTSIZE>.

    Syntax

    fontsize(size)

    The argument to the method, size, must be an integer between one and seven.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • big, small methods

    forward method

    Loads the next URL in the history list.

    Syntax

    forward()

    Description

    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).

    Applies to

    history

    Examples

    xxx Examples to be supplied.

    See also

  • back, go methods

    getDate method

    Syntax

    dateObj.getDate()

    where dateObj is a date object.

    Description

    Returns the day of the month for the date object, an integer between 1 and 31.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getDay method

    Syntax

    dateObj.getDay()

    where dateObj is a date object.

    Description

    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.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getHours method

    Syntax

    dateObj.getHours()

    where dateObj is a date object.

    Description

    Returns the hour for the date object, an integer between 0 and 23.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getMinutes method

    Syntax

    dateObjgetMinute()

    where dateObj is a date object.

    Description

    Returns the minutes in the date object, an integer between 0 and 59.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getMonth method

    Syntax

    dateObj.getMonth()

    where dateObj is a date object.

    Description

    Returns the month in the date object, an integer between zero and eleven. Zero corresponds to January, one to Februrary, and so on.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getSeconds method

    Syntax

    dateObj.getSeconds()

    where dateObj is a date object.

    Description

    Returns the seconds in the current time, an integer between 0 and 59.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    getTime method

    Syntax

    dateObj.getTime()

    where dateObj is a date object.

    Description

    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.

    Applies to

    Date

    Examples

    The following example assigns the date value of theBigDay to sameAsBigDay.

    theBigDay = new Date("July 1, 1999")
    sameAsBigDay = new Date();
    sameAsBigDay.setTime(theBigDay.getTime())
    

    See also

  • xxx To be supplied.

    getTimezoneOffset method

    Syntax

    dateObj.getTimezoneOffset()

    where dateObj is a date object.

    Description

    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.

    Applies to

    Date

    Examples

    x = new Date();
    currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60;
    

    See also

  • xxx To be supplied.

    getYear method

    Syntax

     

    getyear()

    Description

    Returns the year in the date object, less 1900.

    Applies to

    Date

    Examples

    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()
    

    See also

  • xxx To be supplied.

    go method

    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.

    Syntax

    go(delta | "string")

    Description

    xxx Description to be supplied.

    Applies to

    history

    Examples

    xxx Examples to be supplied.

    See also

  • back, forward methods

    indexOf method

    Returns the index within the calling string object of the first occurrence of the specified character, starting the search at fromIndex.

    Syntax

    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.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • charAt, lastIndexOf methods

    italics method

    Causes the calling string object to be italicized by surrounding it with HTML italics tags, <I> ... </I>.

    Syntax

    italics()
    NOTE: For Beta4, use I() for this method.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • blink, bold, strike methods

    lastIndexOf method

    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.

    Syntax

    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.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • charAt, indexOf methods

    link method

    Causes the calling string object to be a hyperlink by surrounding it with HTML hyperlink tags, <A HREF=...> </A>.

    Syntax

    link(location)
    The argument to the method, location, should be a valid URL.

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • anchor method

    log method

    Returns the natural logarithm (base e) of its argument.

    Syntax

    log(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • exp method

    max method

    Returns the greater of its two arguments.

    Syntax

    max(arg1, arg2)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • min method

    min method

    Returns the lesser of its two arguments.

    Syntax

    min(arg1, arg2)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • max method

    open method

    For the window object, opens a new web browser window. xxx For the document object, description to be supplied.

    Syntax

    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.

    Description for window

    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:

  • toolbar creates the standard Navigator toolbar, with buttons such as "Back" and "Forward", if true
  • location creates a Location entry field, if true
  • directories creates the standard Navigator directory buttons, such as "What's New" and "What's Cool", if true
  • status creates the status bar at the bottom of the window, if true
  • menubar creates the menu at the top of the window, if true
  • scrollbars creates horizontal and vertical scrollbars when the document grows larger than the window dimensions, if true
  • resizable allows a user to resize the window, if true
  • copyhistory gives the new window the same session history as the current window, if true
  • width specifies the width of the window in pixels
  • height specifies the height of the window in pixels

    Description for document

    Description to be supplied.

    Applies to

    document, window

    Examples

    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></HEAD>");
       msgWindow.document.write("<CENTER><BIG><B>Hello, world!</B></BIG></CENTER>");
    }
    

    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.

       &amp;amp;lt;FORM NAME=&amp;amp;quot;myform&amp;amp;quot;&amp;amp;gt; &amp;amp;lt;INPUT TYPE=&amp;amp;quot;button&amp;amp;quot; NAME=&amp;amp;quot;Button1&amp;amp;quot; VALUE=&amp;amp;quot;Open Sesame!&amp;amp;quot; onClick=&amp;amp;quot;window.open('sesame.html', 'newWin', 'toolbar=no,directories=no,menubar=no,status=yes,width=300,height=300')&amp;amp;quot;&amp;amp;gt; &amp;amp;lt;/form&amp;amp;gt; 
     

    Notice the use of single quotes (') inside the onClick event handler.

    See also

  • close method

    parse method

    Syntax

    Date.parse(date string)

    Description

    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.

    Applies to

    Date

    Examples

    If IPOdate is an existing date object, then

    IPOdate.setTime(Date.parse("Aug 9, 1995"))
    

    See also

  • UTC method

    pow method

    Returns arg1 to the arg2 power, i.e. arg1arg2.

    Syntax

    pow(arg1, arg2)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • sqrt method

    prompt method

    Displays an Prompt dialog box with a message and an input field

    Syntax

    prompt(message, input default)

    Description

    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.

    Applies to

    window

    Examples

    prompt("Enter the number of doughnuts you want:", 12)
    

    See also

  • alert, confirm methods

    random method

    Returns a pseudo-random number between zero and one. xxx NYI.

    Syntax

    xxx To be supplied.

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    round method

    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.

    Syntax

    round(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.


    select method

    For password, text, and textArea, selects the input area of the object.

    Syntax

    select()

    Description

    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.

    Applies to

    password, text, textArea

    Examples

    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>

    See also

  • blur, focus methods

    setDate method

    Syntax

    dateObj.setDate(day)

    where dateObj is a date object.

    Description

    Sets the day of the month for the date object. The argument is an integer from 1 to 31.

    Applies to

    Date

    Examples

    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)
    

    See also

  • xxx To be supplied.

    setHours method

    Syntax

    dateObj.setHours(hours

    where dateObj is a date object.

    Description

    Sets the hours in the current time. The argument is an integer between 0 and 23.

    Applies to

    Date

    Examples

    theBigDay.setHours(7)
    

    See also

  • xxx To be supplied.

    setMinutes method

    Syntax

    dateObj.setMinutes(minutes)

    where dateObj is a date object.

    Description

    Sets the minutes in the current time. The argument is an integer between 0 and 59.

    Applies to

    Date

    Examples

    theBigDay.setMinutes(45)
    

    See also

  • xxx To be supplied.

    setMonth method

    Syntax

    dateObj.setMonth(month)

    where dateObj is a date object.

    Description

    Sets the month in the current date. The argument is an integer between 0 and 11.

    Applies to

    Date

    Examples

    theBigDay.setMonth(6)
    

    See also

  • xxx To be supplied.

    setSeconds method

    Syntax

    dateObj.setSeconds(seconds)

    where dateObj is a date object.

    Description

    Sets the seconds in the current time. The argument is an integer between 0 and 59.

    Applies to

    Date

    Examples

    theBigDay.setSeconds(30)
    

    See also

  • xxx To be supplied.

    setTime method

    Syntax

    dateObj.setTime(timevalue)

    where dateObj is a date object.

    Description

    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.

    Applies to

    Date

    Examples

    theBigDay = new Date("July 1, 1999")
    sameAsBigDay = new Date();
    sameAsBigDay.setTime(theBigDay.getTime())
    

    setTimeout method

    Evaluates an expression after a specified number of milliseconds have elapsed.

    Syntax

    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.

    Description

    xxx Description to be supplied. Applies to

     

    window

    Examples

    xxx Examples to be supplied.

    See also

  • clearTimeout

    setYear method

    Syntax

    dateObj.setYear(year)

    where dateObj is a date object.

    Description

    Sets the year in the current date. Takes as its argument an integer greater than 1900.

    Applies to

    Date

    Examples

    theBigDay.setYear(96)
    

    See also

  • xxx To be supplied.

    sin method

    Returns the sine of its argument. The argument is in radians.

    Syntax

    sin(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • acos, asin, atan, cos, tan methods

    small method

    Causes the calling string object to be displayed in small font by surrounding it with HTML small font tags, <SMALL>...</SMALL>.

    Syntax

    small()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • big, fontsize methods

    sqrt method

    Returns the square root of its argument.

    Syntax

    sqrt(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • pow method

    strike method

    Causes the calling string object to be displayed as struck out text, by surrounding it with HTML strikeout tags, <STRIKE> ... </STRIKE>.

    Syntax

    strike()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • blink, bold, italics methods

    sub method

    Causes the calling string object to be displayed as a subscript by surrounding it with HTML subscript tags, <SUB> ... </SUB>.

    Syntax

    sub()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    sup method


    submit method

    Submits the form.

    Syntax

    submit()

    Description

    xxx Description to be supplied.

    Applies to

    form

    Examples

    xxx Examples to be supplied.


    substring method

    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.

    Syntax

    substring(a, b)

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    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".


    sup method

    Causes the calling string object to be displayed as a superscript by surrounding it with HTML superscript tags, <SUP> ... </SUP>.

    Syntax

    sup()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    xxx Examples to be supplied.

    See also

  • sub method

    tan method

    Returns the tangent of its argument. The argument is in radians.

    Syntax

    tan(arg)

    Applies to

    Math

    Examples

    xxx Examples to be supplied.

    See also

  • acos, asin, atan, cos, sin methods

    toGMTString method

    Syntax

    dateObj.toGMTString()

    where dateObj is a date object.

    Description

    Converts a date to a String, using the Internet GMT conventions.

    Applies to

    Date

    Examples

    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


    toLocaleString method

    Syntax

    dateObj.toLocaleString()

    where dateObj is a date object.

    Description

    Converts a date to a String, using the locale conventions.

    Applies to

    Date

    Examples

    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


    toLowerCase method

    The toLowerCase method of string objects converts the string to all upper case.

    Syntax

    toLowerCase()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    For example, "ALPHABET".toLowerCase yields "alphabet".

    See also

  • toUpperCase method

    toString method

    For location, returns location.href, so you can use location as a string without typing ".href". For Date, converts a date to a string.

    Syntax

    toString()

    Description

    xxx Description to be supplied.

    Applies to

    location, Date methods

    Examples

    Here is an example of using toString with a date object, assuming theBigDay is a date object:

    theBigDay.toString()
    

    More Examples to be supplied.


    toUpperCase method

    The toUpperCase method of string objects converts the string to all upper case.

    Syntax

    toUpperCase()

    Description

    xxx Description to be supplied.

    Applies to

    string

    Examples

    For example, "alphabet".toUpperCase yields "ALPHABET".

    See also

  • toLowerCase method

    UTC method

    Syntax

    Date.UTC(year, month, mday [, hrs] [, min] [, sec])

    Description

    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:

    • year - a year after 1900
    • month - a month between 0-11
    • date - day of the month between 1-31
    • hrs - hours between 0-23
    • min - minutes between 0-59
    • sec - seconds between 0-59

    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.

    Applies to

    Date

    Examples

    The following statement creates a date object using GMT instead of local time:

    gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0))
    

    See also

  • parse method

    write method

    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.

    Syntax

    write()

    Description

    xxx Description to be supplied.

    Applies to

    document

    Examples

    xxx Examples to be supplied.

    See also

  • writeln method

    writeln method

    The same as write(), but adds a newline character. Note that this affects only preformatted text (inside a <PRE> or <XMP> tag).

    Syntax

    writeln()

    Description

    xxx Description to be supplied.

    Applies to

    document

    Examples

    xxx Examples to be supplied.

    See also

    •  
  • write method
  •  

    事件处理

    以下的事件可由 JavaScript 发生:

     

  • onBlur
  • onChange
  • onClick
  • onFocus
  • onLoad
  • onMouseOver
  • onSelect
  • onSubmit
  • onUnload

    onBlur event handler

    A blur event occurs when a text or textArea field on a form loses focus. The onBlur event handler executes JavaScript code when a blur event occurs.

    Applies to

    selection, text, textArea

    Examples

    xxx Examples to be supplied.


    onChange event handler

    A change event occurs when a selection, text, or textArea field loses focus and its value has been modified. The onChange event handler executes JavaScript code when a change event occurs.

    Use the onChange event handler to validate data after it is modified by a user.

    Applies to

    selection, text, textArea

    Examples

    xxx Examples to be supplied.


    onClick event handler

    For button or radioButton, JavaScript code to run when a button is clicked. For checkbox, JavaScript code to run when user checks or unchecks an item.

     

    Applies to

    button, checkbox, radioButton, link, reset, submit

    Examples

    For example, suppose you have created a JavaScript function called compute(). You can execute the compute() function when the user clicks a button by calling the function in the onClick event handler, as follows:

    <INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)">

    In the above example, the keyword this refers to the current object; in this case, the Calculate button. The construct this.form refers to the form containing the button.

    For another example, suppose you have created a JavaScript function called pickRandomURL() that lets you select a URL at random. You can use the onClick event handler of an anchor to dynamically specify a value for the HREF attribute of the anchor, as shown in the following example:

       &amp;amp;lt;A HREF=&amp;amp;quot;&amp;amp;quot; onClick=&amp;amp;quot;this.href=pickRandomURL();&amp;amp;quot; onMouseOver=&amp;amp;quot;window.status='Pick a random URL'; return true&amp;amp;quot;&amp;amp;gt; Go!&amp;amp;lt;/A&amp;amp;gt; 
     

    In the above example, the onMouseOver event handler specifies a custom message for the Navigator status bar when the user places the mouse pointer over the Go! anchor. As this example shows, you must return true to set the window.status property in the onMouseOver event handler.


    onFocus event handler

    Executed when input focus enters the field, either by tabbing in or by clicking but not selecting in the field.

    Applies to

    selection, text, textArea

    Examples

    xxx Examples to be supplied.


    onLoad event handler

    A load event occurs when Navigator finishes loading a window or all frames within a FRAMESET. The onLoad event handler executes JavaScript code when a load event occurs.

    Use the onLoad event handler within either the <BODY> or the <FRAMESET> tag, for example, <BODY onLoad="...">.

    Applies to

    document, window

    Examples

    xxx Examples to be supplied.

    See also

    onUnload event handler


    onMouseOver event handler

    Executes when the mouse pointer is over an object. You must return true if you want to set the window.status property with the onMouseOver event handler.

    Applies to

    link

    Examples

    By default, the HREF value of an anchor displays in the status bar at the bottom of the Navigator when a user places the mouse pointer over the anchor. In the following example, the onMouseOver event handler provides the custom message "Click this if you dare."

       &amp;amp;lt;A HREF=&amp;amp;quot;http://home.netscape.com/&amp;amp;quot; onMouseOver=&amp;amp;quot;window.status='Click this if you dare!'; return true&amp;amp;quot;&amp;amp;gt; Click me&amp;amp;lt;/A&amp;amp;gt; 
     

    See onClick for an example of using onMouseOver when the anchor HREF attribute is set dynamically.


    onSelect event handler

     

    A select event occurs when a user selects some of the text within a text or textArea field. The onSelect event handler executes JavaScript code when a select event occurs.

    Applies to

    text, textArea

    Examples

    xxx Examples to be supplied.


    onSubmit event handler

    Specifies the JavaScript code to run when a user attempts to submit a form. Return true to allow the form to be submitted; return false to prevent the form from being submitted.

    Applies to

    form

    Examples

    In the following example, the onSubmit event handler evaluates the data being submitted to test if it is legal. If the data is legal, the form is submitted; otherwise, the form is not submitted.

       form.onSubmit= &amp;amp;quot;if badFormData(this.form) { return false; } else { return true; }&amp;amp;quot; 
     

    onUnload event handler

    An unload event occurs when you exit a document. The onUnload event handler executes JavaScript code when an unload event occurs.

    Use the onUnload event handler within either the <BODY> or the <FRAMESET> tags, for example, <BODY onUnload="...">.

    Applies to

    document, window

    Examples

    xxx Examples to be supplied.

    See also

    onLoad event handler


  •  

    JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semi-colon.

    Syntax conventions: All keywords in syntax statements are in bold. Words in italics represent user-defined names or statements. Any portions enclosed in square brackets, i.e. [ and ], are optional. {statements} indicates a block of statements, which can consist of a single statement or multiple statements delimited by a curly braces.

    The following statements are available in JavaScript:


    break statement

    The break statement terminates the current while or for loop and transfers program control to the statement following the terminated loop.

    Syntax

    break

    Examples

    The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x.

    function func(x) { 
        var i = 0; 
        while (i < 6) { 
            if (i == 3)  
                break;
            i++;
        }
        return i*x; 
    } 
    

    comment statement

    Comments are notations by the author to explain what the script does, and they are ignored by the interpreter. JavaScript supports Java-style comments:

  • Comments on a single line are preceded by a double-slash (//).
  • Comments that span multiple lines are preceded by a /* and followed by a */.

    Syntax

    1. // comment text 
    2. /* multiple line comment text */

    Examples

    // This is a single-line comment.
    
    
    /* This is a multiple-line comment. It can be of any length, and you can put whatever you want here. */

    continue statement

    The continue statement terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. In contrast to the break statement, it does not terminate the execution of the loop entirely: instead,

    • In a while loop it jumps back to the condition.
    • In a for loop it jumps to the update expression.

    Syntax

    continue

    Examples

    The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12.

     

    i = 0; 
    n = 0; 
    while (i < 5) { 
        i++; 
        if (i == 3) 
            continue;  
        n += i;
    } 
    

    for statement

    A for loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. The parts of the for statement are:

    • The initial expression, generally used to initialize a counter variable. This statement may optionally declare new variables with the var keyword. This expression is optional.
    • The condition that is is evaluated on each pass through the loop. If this condition is true, the statements in the succeeding block are performed. This conditional test is optional. If omitted, then the condition always evaluates to true.
    • An update expression generally used to update or increment the counter variable. This expression is optional.
    • A block of statements that are executed as long as the condition is true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements four spaces from the beginning of the for statement.

    Syntax

    for ([initial expression]; [condition]; [update expression]) {
        statements
    }
    initial expression = statement | variable declartion
    

    Examples

    This simple for statement starts by declaring the variable i and initializing it to zero. It checks that i is less than nine, and performs the two succeeding statements, and increments i by one after each pass through the loop.

    for (var i = 0; i < 9; i++) { 
        n += i;
        myfunc(n);
    }
    

    for...in statement

    The for statement iterates variable var over all the properties of object obj. For each distinct property, it executes the statements in statements.

    Syntax

    for (var in obj) { 
        statements }

    Examples

    The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.

    function dump_props(obj, obj_name) { 
        var result = "", i = "";
        for (i in obj)
            result += obj_name + "." + i + " = " + obj[i] + "/n";
        return result;
    } 

    function statement

    The function statement declares a JavaScript function name with the specified parameters param. To return a value, the function must have a return statement that specifies the value to return. You cannot nest a function statement in another statement or in itself.

    All parameters are passed to functions, by value. In other words, the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

    Syntax

    function name([param] [, param] [..., param]) {
        statements }

    Examples

    //This function returns the total dollar amount of sales, when 
    //given the number of units sold of products a, b, and c.
    function calc_sales(units_a, units_b, units_c) {
        return units_a*79 + units_b*129 + units_c*699
    }

    if...else statement

    The if...else statement is a conditional statement that executes the statements in statements if condition is true. In the optional else clause, it executes the statements in else statements if condition is false. These may be any JavaScript statements, including further nested if statements.

    Syntax

    if (condition) {
        statements
    } [else {
        else statements
    }]

    Examples

    if ( cipher_char == from_char ) {
        result = result + to_char; 
        x++
    } else  
        result = result + clear_char; 
     

    return statement

    The return statement specifies the value to be returned by a function.

    Syntax

    return expression;

    Examples

    The following simple function returns the square of its argument, x, where x is an number.

    function square( x ) { 
        return x * x; 
    } 

    var statement

    The var statement declares a variable varname, optionally initializing it to have value. The variable name varname can be any legal identifier, and value can be any legal expression. The scope of a variable is the current function or, for variables declared outside a function, the current application.

    Using var outside a function is optional; you can declare a variable by simply assigning it a value. However, it is good style to use var, and it is neccessary in functions if there is a global variable of the same name. So, in general, it is a good idea to always use var, but you should definitely use it when declaring a local variable in a function, to ensure that any global variable of the same name does not override it.

    Syntax

    var varname [= value] [..., varname [= value] ]
    

    Examples

    var num_hits = 0, cust_no = 0


    while statement

    The while statement is a loop that evaluates the expression condition,and if it is true, executes statements. It then repeats this process, as long as condition is true. When condition evaluates to false, execution continues with the next statement following the statements.

    Although not required, it is good practice to indent the statements a while loop four spaces from the beginning of the for statement.

    Syntax

    while (condition) {
        statements
    }
    

    Examples

    The following simple while loop iterates as long as n is less than three. Each iteration, it increments n and adds it to x. Therefore, x and n take on the following values

    • After first pass: x = 1 and n = 1
    • After second pass: x = 2 and n = 3
    • After third pass: x = 3 and n = 6

    After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.

    n = 0; 
    x = 0; 
    while( n < 3 ) { 
        n ++; x += n; 
    } 

    with statement

    The with statement establishes object as the default object for the statements. Any property references without an object are then assumed to be for object. Note that the parentheses are required around object.

    Syntax

    with (object){ 
         statements 
    }

    Examples

    with (Math) { 
        a = PI * r*r
        x = r * cos(theta)
        y = r * sin(theta) 
    }
    

  •  

    以下的保留字不可以用作变量,函数名,对象名等,其中有的保留字是为以后JAVASCRIPT扩展用的.

    • abstract
    • boolean
    • break
    • byte
    • case
    • catch
    • char
    • class
    • const
    • continue
    • default
    • do
    • double
    • else
    • extends
    • false
    • final
    • finally
    • float
    • for
    • function
    • goto
    • if
    • implements
    • import
    • in
    • instanceof
    • int
    • interface
    • long
    • native
    • new
    • null
    • package
    • private
    • protected
    • public
    • return
    • short
    • static
    • super
    • switch
    • synchronized
    • this
    • throw
    • throws
    • transient
    • true
    • try
    • var
    • void
    • while
    • with

     

    Javascript的保留字

    JavaScript 的语法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值