Javascript-Referência JavaScript - Letras S a S
[arquivo anterior (letras R a R)]      [arquivo posterior (letras T a Z)]

scroll

method. scrolls a window to a specified coordinate.

syntax

windowreference.scroll(x-coordinate,y-coordinate)

parameters

windowreference is a valid way of referring to a window, as described in the window object.

x-coordinate is an integer representing the x-coordinate in pixels.

y-coordinate is an integer representing the y-coordinate in pixels.

method of

window object

implemented in

navigator 3.0

description

javascript does not reflect document dimensions in pixels, so when using the scroll method, you must hardcode the x and y coordinates. a document's upper left coordinates are 0,0.

examples

example 1: scroll the current window. the following example scrolls the current window to the coordinates 50,100.

window.scroll(50,100)
example 2: scroll a different window. the following code, which exists in one frame, scrolls a second frame. two text objects let the user specify the x and y coordinates. when the user clicks the go button, the document in frame2 scrolls to the specified coordinates.

<script>
function scrollit(form) {
   var x = parseint(form.x.value)
   var y = parseint(form.y.value)
   parent.frame2.scroll(x, y)
}
</script>
<body>

<form name="myform">
<p><b>specify the coordinates to scroll to:</b>
<br>horizontal:
<input type="text" name=x value="0" size=4>
<br>vertical:
<input type="text" name=y value="0" size=4>
<br><input type="button" value="go"
   onclick="scrollit(document.myform)">
</form>

search

property. a string beginning with a question mark that specifies any query information in the url.

syntax

1. links[index].search
2. location.search
3. areaname.search

parameters

index is an integer representing a link object or the name of a link object as specified by the name attribute.

areaname is the value of the name attribute of an area object.

property of

area object (see link object), link object, location object

implemented in

tainted?

yes

description

the search property specifies a portion of the url. this property applies to http urls only.

the search property contains variable and value pairs; each pair is separated by an ampersand. for example, two pairs in a search string could look like the following:

?x=7&y=5
you can set the search property at any time, although it is safer to set the href property to change a location. if the search that you specify cannot be found in the current location, you will get an error.

in event handlers, you must specify window.location.search instead of simply using location.search. due to the scoping of static objects in javascript, a call to location without specifying an object name is equivalent to document.location, which is a synonym for document.url.

see section 3.3 of rfc 1738 for complete information about the search.

examples

in the following example, the window.open statement creates a window called newwindow and loads the specified url into it. the document.write statements display all the properties of newwindow.location in a window called msgwindow.

newwindow=window.open
   ("http://guide-p.infoseek.com/ww/ns/titles?qt=rfc+1738+&col=ww")

msgwindow.document.write("newwindow.location.href = " +
   newwindow.location.href + "<p>")
msgwindow.document.write("newwindow.location.protocol = " +
   newwindow.location.protocol + "<p>")
msgwindow.document.write("newwindow.location.host = " +
   newwindow.location.host + "<p>")
msgwindow.document.write("newwindow.location.hostname = " +
   newwindow.location.hostname + "<p>")
msgwindow.document.write("newwindow.location.port = " +
   newwindow.location.port + "<p>")
msgwindow.document.write("newwindow.location.pathname = " +
   newwindow.location.pathname + "<p>")
msgwindow.document.write("newwindow.location.hash = " +
   newwindow.location.hash + "<p>")
msgwindow.document.write("newwindow.location.search = " +
   newwindow.location.search + "<p>")
msgwindow.document.close()
the previous example displays the following output:

newwindow.location.href =
   http://guide-p.infoseek.com/ww/ns/titles?qt=rfc+1738+&col=ww
newwindow.location.protocol = http:
newwindow.location.host = guide-p.infoseek.com
newwindow.location.hostname = guide-p.infoseek.com
newwindow.location.port =
newwindow.location.pathname = /ww/ns/titles
newwindow.location.hash =
newwindow.location.search = ?qt=rfc+1738+&col=ww

see also

hash, host, hostname, href, pathname, port, protocol properties


select method

method. selects the input area of the specified password, text, or textarea object.

syntax

1. passwordname.select()
2. textname.select()
3. textareaname.select()

parameters

passwordname is either the value of the name attribute of a password object or an element in the elements array.

textname is either the value of the name attribute of a text object or an element in the elements array.

textareaname is either the value of the name attribute of a textarea object or an element in the elements array.

method of

password object, text object, textarea object

implemented in

navigator 2.0

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.

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 the 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


select object

object. a selection list on an html form. the user can choose one or more items from a selection list.

html syntax

to define a select object, use standard html syntax with the addition of javascript event handlers:

<select
   name="selectname"
   [size="integer"]
   [multiple]
   [onblur="handlertext"]
   [onchange="handlertext"]
   [onfocus="handlertext"]>
   <option value="optionvalue" [selected]> texttodisplay
      [ ... <option> texttodisplay]
</select>

html attributes

name="selectname" specifies the name of the select object. you can access this value using the name property, and you can use this name when indexing the elements array.

size="integer" specifies the number of options visible when the form is displayed.

multiple specifies that multiple items can be selected. if omitted, only one item can be selected from the list.

option specifies a selection element in the list. you can access the options using the options array.

value="optionvalue" specifies a value that is returned to the server when the option is selected and the form is submitted. you can access this value using the value property.

selected specifies that the option is selected by default. you can access this value using the defaultselected property.

texttodisplay specifies the text to display in the list. you can access this value using the text property.

syntax

to create an option to add to an existing select object:

optionname = new option([text, value, defaultselected, selected])
to add the new option to an existing select object:

selectname.options[index1]=optionname
to delete an option from a select object:

selectname.options[index1] = null
to use a select object's properties and methods:

1. selectname.propertyname
2. selectname.methodname(parameters)
3. formname.elements[index].propertyname
4. formname.elements[index].methodname(parameters)
to use an option's properties:

1. selectname.options[index1].propertyname
2. formname.elements[index2].options[index1].propertyname
3. optionname.propertyname

parameters

optionname is either the name of a new object or a property of an existing object. when using option properties and methods, optionname is either the name of an existing option object or a property of an existing object.

text specifies the text to display in the select list. you can access this value using the text property.

value specifies a value that is returned to the server when the option is selected and the form is submitted. you can access this value using the value property.

defaultselected specifies whether the option is initially selected (true or false). you can access this value using the defaultselected property.

selected specifies the current selection state of the option (true or false). you can access this value using the selected property.

selectname is the value of the name attribute of a select object.

formname is either the value of the name attribute of a form object or an element in the forms array.

index is an integer representing a select object on a form or the name of a select object as specified by the name attribute.

index1 is an integer representing an option in a select object.

index2 is an integer representing a select object on a form.

propertyname is one of the properties listed below.

methodname is one of the methods listed below.

property of

implemented in

description

a select object on a form looks as follows. the user can choose one item from the list on the left; the user can choose multiple items from the list on the right:

a select object is a form element and must be defined within a <form> tag.

the options array

you can reference the options of a select object in your code by using the options array. this array contains an entry for each option in a select object (<option> tag) in source order. for example, if a select object named musicstyle contains three options, these options are reflected as musicstyle.options[0], musicstyle.options[1], and musicstyle.options[2].

to use the options array:

1. selectname.options
2. selectname.options[index]
3. selectname.options.length
selectname is either the value of the name attribute of a select object or an element in the elements array.

index is an integer representing an option in a select object.

to obtain the number of options in a select object, use the length property of either the options array or the select object:

1. selectname.length
2. selectname.options.length
the select object has properties that you can access only through the options array. these properties are listed below.

each element in the options array represents a select option; the value returned by selectname.options represents the full html statement for the selectname object.

elements in the options array are read-only. for example, the statement selectname.options[0]="guitar" has no effect.

for select objects that can have multiple selections (the <select> tag has the multiple attribute), you have to loop and test each option individually:

for (var i = 0; i < select.options.length; i++) {
          if (select.options[i].selected)
                    // statements to perform if option is selected
                    ...
}

changing the selection state

you can use the selected and selectedindex properties to change the selection state of options in a select object.

changing option text

to change an option's text, use the text property of the options array. for example, suppose a form has the following select object:

<select name="userchoice">
<option>choice 1
<option>choice 2
<option>choice 3
</select>
you can set the text of the ith item in the selection based on text entered in a text field named whatsnew as follows:

myform.userchoice.options[i].text = myform.whatsnew.value
you do not need to reload or refresh after changing an option's text.

for example, in the following form, the user can enter some text in the first text field and then enter a number between 0 and 2 (inclusive) in the second text field. when the user clicks the button, the text will be substituted for the indicated option number and that option is selected.

     
     new text for the option:
     option to change (0, 1, or 2):

     

the code for this example looks as follows:

<script>
function updatelist(theform, i) {
   theform.userchoice.options[i].text = theform.whatsnew.value
   theform.userchoice.options[i].selected = true
}
</script>
<form>
<select name="userchoice">
   <option>choice 1
   <option>choice 2
   <option>choice 3
</select>
<br>
new text for the option: <input type="text" name="whatsnew">
<br>
option to change (0, 1, or 2): <input type="text" name="idx">
<br>
<input type="button" value="change selection"
onclick="updatelist(this.form, this.form.idx.value)">
</form>

adding options using the option() constructor

each option created using the option() constructor is an object and has the same properties as elements of the options array.

after you create the options and add them to the select object, you must refresh the document by using history.go(0). this statement must be last. when the document reloads, variables are lost if not saved in cookies or form element values.

deleting options

deletion compresses the options array. for example, if you delete options[0], the existing options[1] becomes options[0]. it's a good idea to delete options at the end of the array first.

after you delete an option, you must refresh the document by using history.go(0). this statement must be last. when the document reloads, variables are lost if not saved in cookies or form element values.

properties

the select object has the following properties:

property description
form property
specifies the form containing the select object
length
reflects the number of options in a select object
name
reflects the name attribute
options
reflects the <option> tags
selectedindex
reflects the index of the selected option (or the first selected option, if multiple options are selected)
type
specifies that the object is a select object and indicates whether multiple is specified

the options array has the following properties:

property description
length
reflects the number of options in a select object
selectedindex
reflects the index of the selected option

each element of the options array has the following properties:

property description
defaultselected
reflects the selected attribute
index
reflects the index of an option
selected
lets you programmatically select an option
text property
reflects the texttodisplay that follows an <option> tag
value
reflects the value attribute

objects created with the option() constructor have the following properties:

property description
defaultselected
specifies the initial selection state of the option
index
specifies the index of the option in a select object
prototype
lets you add a properties to an option.
selected
specifies the current selection state of the option
text property
specifies the text for the option
value
specifies the value that is returned to the server when the option is selected and the form is submitted

methods

the select object has the following methods:

  • blur
  • eval
  • focus
  • tostring
  • valueof
  • event handlers

    examples

    example 1. the following example displays two selection lists. in the first list, the user can select only one item; in the second list, the user can select multiple items.

    choose the music type for your free cd:
    <select name="music_type_single">
       <option selected> r&b
       <option> jazz
       <option> blues
       <option> new age
    </select>
    <p>choose the music types for your free cds:
    <br><select name="music_type_multi" multiple>
       <option selected> r&b
       <option> jazz
       <option> blues
       <option> new age
    </select>
    example 2. the following example displays two selection lists that let the user choose a month and day. these selection lists are initialized to the current date. the user can change the month and day by using the selection lists or by choosing preset dates from radio buttons. text fields on the form display the values of the select object's properties and indicate the date chosen and whether it is cinco de mayo.

    <html>
    <head>
    <title>select object example</title>
    </head>
    <body>
    <script>
    var today = new date()
    //---------------
    function updatepropertydisplay(monthobj,dayobj) {
       // get date strings
       var monthinteger, dayinteger, monthstring, daystring
       monthinteger=monthobj.selectedindex
       dayinteger=dayobj.selectedindex
       monthstring=monthobj.options[monthinteger].text
       daystring=dayobj.options[dayinteger].text
       // display property values
       document.selectform.textfulldate.value=monthstring + " " + daystring
       document.selectform.textmonthlength.value=monthobj.length
       document.selectform.textdaylength.value=dayobj.length
       document.selectform.textmonthname.value=monthobj.name
       document.selectform.textdayname.value=dayobj.name
       document.selectform.textmonthindex.value=monthobj.selectedindex
       document.selectform.textdayindex.value=dayobj.selectedindex
       // is it cinco de mayo?
       if (monthobj.options[4].selected && dayobj.options[4].selected)
          document.selectform.textcinco.value="yes!"
       else
          document.selectform.textcinco.value="no"
    }
    </script>
    <!--------------->
    <form name="selectform">
    <p><b>choose a month and day:</b>
    month: <select name="monthselection"
       onchange="updatepropertydisplay(this,document.selectform.dayselection)">
       <option> january <option> february <option> march
       <option> april <option> may <option> june
       <option> july <option> august <option> september
       <option> october <option> november <option> december
    </select>
    day: <select name="dayselection"
       onchange="updatepropertydisplay(document.selectform.monthselection,this)">
       <option> 1 <option> 2 <option> 3 <option> 4 <option> 5
       <option> 6 <option> 7 <option> 8 <option> 9 <option> 10
       <option> 11 <option> 12 <option> 13 <option> 14 <option> 15
       <option> 16 <option> 17 <option> 18 <option> 19 <option> 20
       <option> 21 <option> 22 <option> 23 <option> 24 <option> 25
       <option> 26 <option> 27 <option> 28 <option> 29 <option> 30
       <option> 31
    </select>
    <p><b>set the date to: </b>
    <input type="radio" name="datechoice"
       onclick="
          monthselection.selectedindex=0;
          dayselection.selectedindex=0;
          updatepropertydisplay
             document.selectform.monthselection,document.selectform.dayselection)">
       new year's day
    <input type="radio" name="datechoice"
       onclick="
          monthselection.selectedindex=4;
          dayselection.selectedindex=4;
          updatepropertydisplay
             (document.selectform.monthselection,document.selectform.dayselection)">
       cinco de mayo
    <input type="radio" name="datechoice"
       onclick="
          monthselection.selectedindex=5;
          dayselection.selectedindex=20;
          updatepropertydisplay
             (document.selectform.monthselection,document.selectform.dayselection)">
       summer solstice
    <p><b>property values:</b>
    <br>date chosen: <input type="text" name="textfulldate" value="" size=20">
    <br>monthselection.length<input type="text" name="textmonthlength" value="" size=20">
    <br>dayselection.length<input type="text" name="textdaylength" value="" size=20">
    <br>monthselection.name<input type="text" name="textmonthname" value="" size=20">
    <br>dayselection.name<input type="text" name="textdayname" value="" size=20">
    <br>monthselection.selectedindex
       <input type="text" name="textmonthindex" value="" size=20">
    <br>dayselection.selectedindex<input type="text" name="textdayindex" value="" size=20">
    <br>is it cinco de mayo? <input type="text" name="textcinco" value="" size=20">
    <script>
    document.selectform.monthselection.selectedindex=today.getmonth()
    document.selectform.dayselection.selectedindex=today.getdate()-1
    updatepropertydisplay(document.selectform.monthselection,document.selectform.dayselection)
    </script>
    </form>
    </body>
    </html>
    example 3. add an option using the option() constructor. the following example creates two select objects, one without the multiple attribute and one with. no options are initially defined for either object. when the user clicks a button associated with the select object, the populate function creates four options for the select object and selects the first option.

    <script>
    function populate(inform) {
       colorarray = new array("red", "blue", "yellow", "green")

       var option0 = new option("red", "color_red")
       var option1 = new option("blue", "color_blue")
       var option2 = new option("yellow", "color_yellow")
       var option3 = new option("green", "color_green")

       for (var i=0; i < 4; i++) {
          eval("inform.selecttest.options[i]=option" + i)
          if (i==0) {
             inform.selecttest.options[i].selected=true
          }
       }

       history.go(0)
    }
    </script>


    <h3>select option() constructor</h3>
    <form>
    <select name="selecttest"></select><p>
    <input type="button" value="populate select list" onclick="populate(this.form)">
    <p>
    </form>

    <hr>
    <h3>select-multiple option() constructor</h3>
    <form>
    <select name="selecttest" multiple></select><p>
    <input type="button" value="populate select list" onclick="populate(this.form)">
    </form>
    example 4. delete an option. the following function removes an option from a select object.

    function deleteanitem(thelist,itemno) {
       thelist.options[itemno]=null
       history.go(0)
    }
    see also the examples for the defaultselected property.

    see also

    form object, radio object


    selected

    property. a boolean value indicating whether an option in a select object is selected.

    syntax

    1. selectname.options[index].selected
    2. optionname.selected

    parameters

    selectname is either the value of the name attribute of a select object or an element in the elements array.

    index is an integer representing an option in a select object.

    optionname is the name of a select object option created using the option() constructor.

    property of

    option object (see select object), options array (see select object)

    implemented in

    tainted?

    yes

    description

    if an option in a select object is selected, the value of its selected property is true; otherwise, it is false.

    you can set the selected property at any time. the display of the select object updates immediately when you set the selected property.

    in general, the selected property is more useful than the selectedindex property for select objects that are created with the multiple attribute. with the selected property, you can evaluate every option in the options array to determine multiple selections, and you can select individual options without clearing the selection of other options.

    examples

    see the examples for the defaultselected property.

    see also

    defaultselected, index, selectedindex properties


    selectedindex

    property. an integer specifying the index of the selected option in a select object.

    syntax

    1. selectname.selectedindex
    2. selectname.options.selectedindex

    parameters

    selectname is either the value of the name attribute of a select object or an element in the elements array.

    property of

    select object; options array (see select object)

    implemented in

    navigator 2.0

    tainted?

    yes

    description

    options in a select object are indexed in the order in which they are defined, starting with an index of zero. you can set the selectedindex property at any time. the display of the select object updates immediately when you set the selectedindex property. both forms of the syntax specify the same value. if no option is selected, selectedindex has a value of -1.

    in general, the selectedindex property is more useful for select objects that are created without the multiple attribute. if you evaluate selectedindex when multiple options are selected, the selectedindex property specifies the index of the first option only. setting selectedindex clears any other options that are selected in the select object.

    the selected property of the select object's options array is more useful for select objects that are created with the multiple attribute. with the selected property, you can evaluate every option in the options array to determine multiple selections, and you can select individual options without clearing the selection of other options.

    examples

    in the following example, the getselectedindex function returns the selected index in the musictype select object:

    function getselectedindex() {
       return document.musicform.musictype.selectedindex
    }
    the previous example assumes that the select object is similar to the following:

    <select name="musictype"> 
       <option selected> r&b
       <option> jazz
       <option> blues
       <option> new age
    </select>

    see also

    defaultselected, index, selected properties


    self

    property. the self property is a synonym for the current window or frame.

    syntax

    1. self.propertyname
    2. self.methodname

    parameters

    propertyname is the defaultstatus, status, length, or name property when self refers to a window object.

    propertyname is the length or name property when self refers to a frame object.

    methodname is any method associated with the window object.

    property of

    frame object, window object

    implemented in

    navigator 2.0

    tainted?

    no

    description

    the self property refers to the current window or frame.

    use the self property to disambiguate a window property from a form or form element of the same name. you can also use the self property to make your code more readable.

    the self property is read-only. the value of the self property is

         <object nameattribute>
    where nameattribute is the name attribute if self refers to a frame, or an internal reference if self refers to a window.

    examples

    in the following example, self.status is used to set the status property of the current window. this usage disambiguates the status property of the current window from a form or form element called "status" within the current window.

    <a href=""
       onclick="this.href=pickrandomurl()"
       onmouseover="self.status='pick a random url' ; return true">
    go!</a>

    see also

    window property


    setdate

    method. sets the day of the month for a specified date.

    syntax

    dateobjectname.setdate(dayvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    dayvalue is an integer from one to 31 or a property of an existing object, representing the day of the month.

    method of

    date

    implemented in

    navigator 2.0

    examples

    the second statement below changes the day for thebigday to july 24 from its original value.

    thebigday = new date("july 27, 1962 23:30:00")
    thebigday.setdate(24)

    see also

    getdate method


    sethours

    method. sets the hours for a specified date.

    syntax

    dateobjectname.sethours(hoursvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    hoursvalue is an integer between zero and 23 or a property of an existing object, representing the hour.

    method of

    date

    implemented in

    navigator 2.0

    examples

    thebigday.sethours(7)

    see also

    gethours method


    setminutes

    method. sets the minutes for a specified date.

    syntax

    dateobjectname.setminutes(minutesvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    minutesvalue is an integer between zero and 59 or a property of an existing object, representing the minutes.

    method of

    date

    implemented in

    navigator 2.0

    examples

    thebigday.setminutes(45)

    see also

    getminutes method


    setmonth

    method. sets the month for a specified date.

    syntax

    dateobjectname.setmonth(monthvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    monthvalue is an integer between zero and 11 (representing the months january through december) or a property of an existing object.

    method of

    date

    implemented in

    navigator 2.0

    examples

    thebigday.setmonth(6)

    see also

    getmonth method


    setseconds

    method. sets the seconds for a specified date.

    syntax

    dateobjectname.setseconds(secondsvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    secondsvalue is an integer between zero and 59 or a property of an existing object.

    method of

    date

    implemented in

    navigator 2.0

    examples

    thebigday.setseconds(30)

    see also

    getseconds method


    settime

    method. sets the value of a date object.

    syntax

    dateobjectname.settime(timevalue) 

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    timevalue is an integer or a property of an existing object, representing the number of milliseconds since the epoch (1 january 1970 00:00:00).

    method of

    date

    implemented in

    navigator 2.0

    description

    use the settime method to help assign a date and time to another date object.

    examples

    thebigday = new date("july 1, 1999")
    sameasbigday = new date()
    sameasbigday.settime(thebigday.gettime())

    see also

    gettime method


    settimeout

    method. evaluates an expression after a specified number of milliseconds has elapsed.

    syntax

    timeoutid=settimeout(expression, msec)

    parameters

    timeoutid is an identifier that is used only to cancel the evaluation with the cleartimeout method.

    expression is a string expression or a property of an existing object.

    msec is a numeric value, numeric string, or a property of an existing object in millisecond units.

    method of

    frame object, window object

    implemented in

    navigator 2.0

    description

    the settimeout method evaluates an expression after a specified amount of time. it does not evaluate the expression repeatedly. for example, if a settimeout method specifies five seconds, the expression is evaluated after five seconds, not every five seconds.

    examples

    example 1. the following example displays an alert message five seconds (5,000 milliseconds) after the user clicks a button. if the user clicks the second button before the alert message is displayed, the timeout is canceled and the alert does not display.

    <script language="javascript">
    function displayalert() {
       alert("5 seconds have elapsed since the button was clicked.")
    }
    </script>
    <body>
    <form>
    click the button on the left for a reminder in 5 seconds;
    click the button on the right to cancel the reminder before
    it is displayed.
    <p>
    <input type="button" value="5-second reminder"
       name="remind_button"
       onclick="timerid=settimeout('displayalert()',5000)">
    <input type="button" value="clear the 5-second reminder"
       name="remind_disable_button"
       onclick="cleartimeout(timerid)">
    </form>
    </body>
    example 2. the following example displays the current time in a text object. the showtime function, which is called recursively, uses the settimeout method to update the time every second.

    <head>
    <script language="javascript">
    <!--
    var timerid = null
    var timerrunning = false
    function stopclock(){
       if(timerrunning)
          cleartimeout(timerid)
       timerrunning = false
    }
    function startclock(){
       // make sure the clock is stopped
       stopclock()
       showtime()
    }
    function showtime(){
       var now = new date()
       var hours = now.gethours()
       var minutes = now.getminutes()
       var seconds = now.getseconds()
       var timevalue = "" + ((hours > 12) ? hours - 12 : hours)
       timevalue += ((minutes < 10) ? ":0" : ":") + minutes
       timevalue += ((seconds < 10) ? ":0" : ":") + seconds
       timevalue += (hours >= 12) ? " p.m." : " a.m."
       document.clock.face.value = timevalue
       timerid = settimeout("showtime()",1000)
       timerrunning = true
    }
    //-->
    </script>
    </head>

    <body onload="startclock()">
    <form name="clock" onsubmit="0">
       <input type="text" name="face" size=12 value ="">
    </form>
    </body>

    see also

    cleartimeout method


    setyear

    method. sets the year for a specified date.

    syntax

    dateobjectname.setyear(yearvalue)

    parameters

    dateobjectname is either the name of a date object or a property of an existing object.

    yearvalue is an integer greater than 1900 or a property of an existing object.

    method of

    date

    implemented in

    navigator 2.0

    examples

    thebigday.setyear(96)

    see also

    getyear method


    sin

    method. returns the sine of a number.

    syntax

    math.sin(number)

    parameters

    number is a numeric expression or a property of an existing object, representing the size of an angle in radians.

    method of

    math

    implemented in

    navigator 2.0

    description

    the sin method returns a numeric value between -1 and one, which represents the sine of the argument.

    examples

    the following function returns the sine of the variable x:

    function getsine(x) {
       return math.sin(x)
    }
    if you pass getsine the value math.pi/2, it returns 1.

    see also

    acos, asin, atan, atan2, cos, tan methods


    small

    method. causes a string to be displayed in a small font, as if it were in a <small> tag.

    syntax

    stringname.small()

    parameters

    stringname is any string or a property of an existing object.

    method of

    string

    implemented in

    navigator 2.0

    description

    use the small method with the write or writeln methods to format and display a string in a document.

    examples

    the following example uses string methods to change the size of a string:

    var worldstring="hello, world"

    document.write(worldstring.small())
    document.write("<p>" + worldstring.big())
    document.write("<p>" + worldstring.fontsize(7))
    the previous example produces the same output as the following html:

    <small>hello, world</small>
    <p><big>hello, world</big>
    <p><fontsize=7>hello, world</fontsize>

    see also

    big, fontsize methods


    sort

    method. sorts the elements of an array.

    syntax

    arrayname.sort(comparefunction)

    parameters

    arrayname is the name of an array object or a property of an existing object.

    comparefunction specifies a function that defines the sort order. if omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.

    method of

    array

    implemented in

    navigator 3.0

    description

    if comparefunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. for example, "80" comes before "9" in lexicographic order, but if you are comparing numbers 9 needs to come before 80.

    if comparefunction is supplied, the array elements are sorted according to the return value of the compare function. if a and b are two elements being compared, then:

    so the compare function has the following form:

    function compare(a, b) {
       if (a is less than b by some ordering criterion)
          return -1
       if (a is greater than b by the ordering criterion)
          return 1
       // a must be equal to b
       return 0
    }
    to compare numbers instead of strings, the compare function can simply subtract b from a:

    function comparenumbers(a, b) {
       return a - b
    }
    javascript uses a stable sort: the index partial order of a and b does not change if a and b are equal. if a's index was less than b's before sorting, it will be after sorting, no matter how a and b move due to sorting.

    note
    on some platforms, the sort method does not work. please see the release notes (after starting netscape, choose release notes from the help menu).

    examples

    the following example creates four arrays and displays the original array, then the sorted arrays. the numeric arrays are sorted without, then with, a compare function.

    <script>
    stringarray = new array("blue","humpback","beluga")
    numericstringarray = new array("80","9","700")
    numberarray = new array(40,1,5,200)
    mixednumericarray = new array("80","9","700",40,1,5,200)

    function comparenumbers(a, b) {
       return a - b
    }

    document.write("<b>stringarray:</b> " + stringarray.join() +"<br>")
    document.write("<b>sorted:</b> " + stringarray.sort() +"<p>")

    document.write("<b>numberarray:</b> " + numberarray.join() +"<br>")
    document.write("<b>sorted without a compare function:</b> " + numberarray.sort() +"<br>")
    document.write("<b>sorted with comparenumbers:</b> " + numberarray.sort(comparenumbers) +"<p>")

    document.write("<b>numericstringarray:</b> " + numericstringarray.join() +"<br>")
    document.write("<b>sorted without a compare function:</b> " + numericstringarray.sort() +"<br>")
    document.write("<b>sorted with comparenumbers:</b> " + numericstringarray.sort(comparenumbers) +"<p>")

    document.write("<b>mixednumericarray:</b> " + mixednumericarray.join() +"<br>")
    document.write("<b>sorted without a compare function:</b> " + mixednumericarray.sort() +"<br>")
    document.write("<b>sorted with comparenumbers:</b> " + mixednumericarray.sort(comparenumbers) +"<br>")
    </script>
    this example produces the following output. as the output shows, when a compare function is used, numbers sort correctly whether they are numbers or numeric strings.

    stringarray: blue,humpback,beluga
    sorted: beluga,blue,humpback
    numberarray: 40,1,5,200
    sorted without a compare function: 1,200,40,5
    sorted with comparenumbers: 1,5,40,200
    numericstringarray: 80,9,700
    sorted without a compare function: 700,80,9
    sorted with comparenumbers: 9,80,700
    mixednumericarray: 80,9,700,40,1,5,200
    sorted without a compare function: 1,200,40,5,700,80,9
    sorted with comparenumbers: 1,5,9,40,80,200,700

    see also

    join, reverse methods


    split

    method. splits a string object into an array of strings by separating the string into substrings.

    syntax

    stringname.split([separator])

    parameters

    stringname is any string or a property of an existing object.

    separator specifies the character to use for separating the string. the separator is treated as a string. if separator is omitted, the array returned contains one element consisting of the entire string.

    method of

    string

    implemented in

    navigator 3.0

    description

    the split method returns the new array.

    examples

    the following example defines a function that splits a string into an array of strings using the specified separator. after splitting the string, the function displays messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.

    function splitstring (stringtosplit,separator) {
       arrayofstrings = stringtosplit.split(separator)
       document.write ('<p>the original string is: "' + stringtosplit + '"')
       document.write ('<br>the separator is: "' + separator + '"')
       document.write ("<br>the array has " + arrayofstrings.length + " elements: ")

       for (var i=0; i < arrayofstrings.length; i++) {
          document.write (arrayofstrings[i] + " / ")
       }
    }

    var tempeststring="oh brave new world that has such people in it."
    var monthstring="jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"

    var space=" "
    var comma=","

    splitstring(tempeststring,space)
    splitstring(tempeststring)
    splitstring(monthstring,comma)
    this example produces the following output:

    the original string is: "oh brave new world that has such people in it."
    the separator is: " "
    the array has 10 elements: oh / brave / new / world / that / has / such / people / in / it. /

    the original string is: "oh brave new world that has such people in it."
    the separator is: "undefined"
    the array has 1 elements: oh brave new world that has such people in it. /

    the original string is: "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"
    the separator is: ","
    the array has 12 elements: jan / feb / mar / apr / may / jun / jul / aug / sep / oct / nov / dec /

    see also

    charat, indexof, lastindexof methods


    sqrt

    method. returns the square root of a number.

    syntax

    math.sqrt(number)

    parameters

    number is any non-negative numeric expression or a property of an existing object.

    method of

    math

    implemented in

    navigator 2.0

    description

    if the value of number is outside the required range, sqrt returns zero.

    examples

    the following function returns the square root of the variable x:

    function getroot(x) {
       return math.sqrt(x)
    }
    if you pass getroot the value nine, it returns three; if you pass it the value two, it returns 1.414213562373095.


    sqrt1_2

    property. the square root of one-half; equivalently, one over the square root of two, approximately 0.707.

    syntax

    math.sqrt1_2

    property of

    math

    implemented in

    navigator 2.0

    tainted?

    no

    description

    because sqrt1_2 is a constant, it is a read-only property of math.

    examples

    the following function returns one over the square root of two:

    function getroot1_2() {
       return math.sqrt1_2
    }

    see also

    e, ln2, ln10, log2e, log2e, pi, sqrt2 properties


    sqrt2

    property. the square root of two, approximately 1.414.

    syntax

    math.sqrt2

    property of

    math

    implemented in

    navigator 2.0

    tainted?

    no

    description

    because sqrt2 is a constant, it is a read-only property of math.

    examples

    the following function returns the square root of two:

    function getroot2() {
       return math.sqrt2
    }

    see also

    e, ln2, ln10, log2e, log2e, pi, sqrt1_2 properties


    src

    property. a string specifying the url of an image to be displayed in a document.

    syntax

    imagename.src

    parameters

    imagename is either the name of an image object or an element in the images array.

    property of

    image

    implemented in

    navigator 3.0

    tainted?

    no

    description

    the src property initially reflects the src attribute of the <img> tag. setting the src property begins loading the new url into the image area (and aborts the transfer of any image data that is already loading into the same area). therefore, if you plan to alter the lowsrc property, you should do so before setting the src property. if the url in the src property references an image that is not the same size as the image cell it is loaded into, the source image is scaled to fit.

    when you change the src property of a displayed image, the new image you specify is displayed in the area defined for the original image. for example, suppose an image object originally displays the file beluga.gif:

    <img name="myimage" src="beluga.gif" align="left">
    if you set myimage.src='seaotter.gif', the image seaotter.gif is scaled to fit in the same space originally used by beluga.gif, even if seaotter.gif is not the same size as beluga.gif.

    you can change the src property at any time.

    examples

    the following example displays an image and three radio buttons. the user can click the radio buttons to choose which image is displayed. each image also uses the lowsrc property to display a low-resolution image.

    <script>
    function displayimage(lowres,highres) {
       document.images[0].lowsrc=lowres
       document.images[0].src=highres
    }
    </script>

    <form name="imageform">
    <b>choose an image:</b>
    <br><input type="radio" name="imagechoice" value="image1" checked
       onclick="displayimage('f15el.gif','f15e.gif')">f-15 eagle
    <br><input type="radio" name="imagechoice" value="image2"
       onclick="displayimage('f15e2l.gif','f15e2.gif')">f-15 eagle 2
    <br><input type="radio" name="imagechoice" value="image3"
       onclick="displayimage('ah64l.gif','ah64.gif')">ah-64 apache

    <br>
    <img name="aircraft" src="f15e.gif" lowsrc="f15el.gif" align="left" vspace="10"><br>
    </form>
    see also the examples for the image object.

    see also

    complete, lowsrc properties


    status

    property. specifies 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.

    syntax

    windowreference.status

    parameters

    windowreference is a valid way of referring to a window, as described in the window object.

    property of

    window object

    implemented in

    navigator 2.0

    tainted?

    yes

    description

    do not confuse the status property with the defaultstatus property. the defaultstatus property reflects the default message displayed in the status bar.

    you can set the status property at any time. you must return true if you want to set the status property in the onmouseover event handler.

    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:

    <a href=""
       onclick="this.href=pickrandomurl()"
       onmouseover="self.status='pick a random url'; return true">
    go!</a>
    in the preceding 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.

    see also

    defaultstatus property


    strike

    method. causes a string to be displayed as struck-out text, as if it were in a <strike> tag.

    syntax

    stringname.strike()

    parameters

    stringname is any string or a property of an existing object.

    method of

    string

    implemented in

    navigator 2.0

    description

    use the strike method with the write or writeln methods to format and display a string in a document.

    examples

    the following example uses string methods to change the formatting of a string:

    var worldstring="hello, world"

    document.write(worldstring.blink())
    document.write("<p>" + worldstring.bold())
    document.write("<p>" + worldstring.italics())
    document.write("<p>" + worldstring.strike())
    the previous example produces the same output as the following html:

    <blink>hello, world</blink>
    <p><b>hello, world</b>
    <p><i>hello, world</i>
    <p><strike>hello, world</strike>

    see also

    blink, bold, italics methods


    string

    object. a series of characters.

    syntax

    to create a string object:

    stringobjectname = new string(string)
    to use a string object:

    1. stringname.propertyname
    2. stringname.methodname(parameters)

    parameters

    stringobjectname is the name of a new string object.

    string is any string.

    stringname is the name of a string object or string variable.

    propertyname is one of the properties listed below.

    methodname is one of the methods listed below.

    property of

    none

    implemented in

    description

    the string object is a built-in javascript object.

    a string can be represented as a literal enclosed by single or double quotation marks; for example, "netscape" or 'netscape'.

    properties

    the string object has the following properties:

    property description
    length
    reflects the length of the string
    prototype
    lets you add properties to a string object.

    methods

    the string object has the following methods:

  • anchor method
  • big
  • blink
  • bold
  • charat
  • eval
  • fixed
  • fontcolor
  • fontsize
  • indexof
  • italics
  • lastindexoflink method
  • small
  • split
  • strike
  • sub
  • substring
  • sup
  • tolowercase
  • touppercase
  • tostring
  • valueof
  • event handlers

    none.

    examples

    example 1: string variable. the following statement creates a string variable:

    var last_name = "schaefer"
    example 2: string object properties. the following statements evaluate to eight, "schaefer," and "schaefer":

    last_name.length
    last_name.touppercase()
    last_name.tolowercase()
    example 3: pass a string among scripts in different windows or frames. the following code creates two string variables and opens a second window:

    var lastname = new string("schaefer")
    var firstname = new string ("jesse")
    empwindow=window.open('string2.html','window1','width=300,height=300')
    if the html source for the second window (string2.html) creates two string variables, emplastname and empfirstname, the following code in the first window assigns values to the second window's variables:

    empwindow.empfirstname=firstname
    empwindow.emplastname=lastname
    the following code in the first window displays the values of the second window's variables:

    alert('empfirstname in empwindow is ' + empwindow.empfirstname)
    alert('emplastname in empwindow is ' + empwindow.emplastname)

    see also

    text object, textarea object


    sub

    method. causes a string to be displayed as a subscript, as if it were in a <sub> tag.

    syntax

    stringname.sub()

    parameters

    stringname is any string or a property of an existing object.

    method of

    string

    implemented in

    navigator 2.0

    description

    use the sub method with the write or writeln methods to format and display a string in a document.

    examples

    the following example uses the sub and sup methods to format a string:

    var supertext="superscript"
    var subtext="subscript"

    document.write("this is what a " + supertext.sup() + " looks like.")
    document.write("<p>this is what a " + subtext.sub() + " looks like.")
    the previous example produces the same output as the following html:

    this is what a <sup>superscript</sup> looks like.
    <p>this is what a <sub>subscript</sub> looks like.

    see also

    sup method


    submit method

    method. submits a form.

    syntax

    formname.submit()

    parameters

    formname is the name of any form or an element in the forms array.

    method of

    form object

    implemented in

    navigator 2.0

    description

    the submit method submits the specified form. it performs the same action as a submit button.

    use the submit method to send data back to an http server. the submit method returns the data using either "get" or "post," as specified in the method property.

    the submit method fails without notice, if the form's action is a mailto:, news:, or snews: url. this is for security purposes. users can submit forms with such urls by clicking a submit button, but a confirming dialog will tell them that they are about to give away private or sensitive information.

    examples

    the following example submits a form called musicchoice:

    document.musicchoice.submit()
    if musicchoice is the first form created, you also can submit it as follows:

    document.forms[0].submit()
    see also the example for the form object.

    see also

    submit object, onsubmit event handler


    submit object

    object. a submit button on an html form. a submit button causes a form to be submitted.

    html syntax

    to define a submit button, use standard html syntax with the addition of javascript event handlers:

    <input
       type="submit"
       name="submitname"
       value="buttontext"
       [onblur="handlertext"]
       [onclick="handlertext"]
       [onfocus="handlertext"]>

    html attributes

    name="submitname" specifies the name of the submit object. you can access this value using the name property, and you can use this name when indexing the elements array.

    value="buttontext" specifies the label to display on the button face. you can access this value using the value property.

    syntax

    to use a submit object's properties and methods:

    1. submitname.propertyname
    2. submitname.methodname(parameters)
    3. formname.elements[index].propertyname
    4. formname.elements[index].methodname(parameters)

    parameters

    submitname is the value of the name attribute of a submit object.

    formname is either the value of the name attribute of a form object or an element in the forms array.

    index is an integer representing a submit object on a form or the name of a submit object as specified by the name attribute.

    propertyname is one of the properties listed below.

    methodname is one of the methods listed below.

    property of

    form object

    implemented in

    description

    a submit object on a form looks as follows:

    a submit object is a form element and must be defined within a <form> tag.

    clicking a submit button submits a form to the url 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.

    the submit button's onclick event handler cannot prevent a form from being submitted; instead, use the form's onsubmit event handler or use the submit method instead of a submit object. see the examples for the form object.

    if a form contains only one element, a text object, then when the user enters a value and presses return, the form submits. (this is a standard html feature.)

    properties

    the submit object has the following properties:

    property description
    form property
    specifies the form containing the submit object
    name
    reflects the name attribute
    type
    reflects the type attribute
    value
    reflects the value attribute

    methods

    the submit object has the following methods:

  • blur
  • click
  • eval
  • focus
  • tostring
  • valueof
  • event handlers

    examples

    the following example creates a submit object called submitbutton. the text "done" is displayed on the face of the button.

    <input type="submit" name="submitbutton" value="done">
    see also the examples for the form object.

    see also

    button object, form object, reset object; submit method; onsubmit event handler


    substring

    method. returns a subset of a string object.

    syntax

    stringname.substring(indexa, indexb)

    parameters

    stringname is any string or a property of an existing object.

    indexa is any integer from zero to stringname.length - 1, or a property of an existing object.

    indexb is any integer from zero to stringname.length, or a property of an existing object.

    method of

    string

    implemented in

    navigator 2.0

    description

    characters in a string are indexed from left to right. the index of the first character is zero, and the index of the last character is stringname.length - 1.

    examples

    example 1. the following example uses substring to display characters from the string "netscape":

    var anystring="netscape"
    
    //displays "net"
    document.write(anystring.substring(0,3))
    document.write(anystring.substring(3,0))
    //displays "cap"
    document.write(anystring.substring(4,7))
    document.write(anystring.substring(7,4))
    //displays "netscap"
    document.write(anystring.substring(0,7))
    //displays "netscape"
    document.write(anystring.substring(0,8))
    document.write(anystring.substring(0,10))
    
    example 2. the following example replaces a substring within a string. it will replace both individual characters and substrings. the function call at the end of the example changes the string "brave new world" into "brave new web".

    function replacestring(olds,news,fulls) {
    // replaces olds with news in the string fulls
       for (var i=0; i<fulls.length; i++) {
          if (fulls.substring(i,i+olds.length) == olds) {
             fulls = fulls.substring(0,i)+news+fulls.substring(i+olds.length,fulls.length)
          }
       }
       return fulls
    }
    replacestring("world","web","brave new world")

    suffixes

    property. a string listing possible file suffixes (also known as file name extensions) for the mime type.

    syntax

    navigator.mimetypes[index].suffixes

    parameters

    index is either an integer representing a mime type supported by the client or a string containing the type of a mimetype object (from the type property).

    property of

    mimetype

    implemented in

    navigator 3.0

    tainted?

    no

    description

    the suffixes property is a string consisting of each valid suffix (typically three letters long) separated by commas. for example, the suffixes for the "audio/x-midi" mime type are "mid, midi".

    suffixes is a read-only property.

    examples

    see the examples for the mimetype object.

    see also

    description, enabledplugin, type properties


    sup

    method. causes a string to be displayed as a superscript, as if it were in a <sup> tag.

    syntax

    stringname.sup()

    parameters

    stringname is any string or a property of an existing object.

    method of

    string

    implemented in

    navigator 2.0

    description

    use the sup method with the write or writeln methods to format and display a string in a document.

    examples

    the following example uses the sub and sup methods to format a string:

    var supertext="superscript"
    var subtext="subscript"

    document.write("this is what a " + supertext.sup() + " looks like.")
    document.write("<p>this is what a " + subtext.sub() + " looks like.")
    the previous example produces the same output as the following html:

    this is what a <sup>superscript</sup> looks like.
    <p>this is what a <sub>subscript</sub> looks like.

    see also

    sub method


    [next reference file]