|
Methods and FunctionsThe following methods and functions are available in JavaScript: (A-N)
abs methodReturns the absolute value of a number. SyntaxMath.abs(number) number is any numeric expression or a property of an existing object. Method ofExamplesIn the following example, the user enters a number in the first text box and presses the Calculate button to display the absolute value of the number. <FORM> <P>Enter a number: <INPUT TYPE="text" NAME="absEntry"> <P>The absolute value is: <INPUT TYPE="text" NAME="result"> <P> <INPUT TYPE="button" VALUE="Calculate" onClick="form.result.value = Math.abs(form.absEntry.value)"> </FORM> acos methodReturns the arc cosine (in radians) of a number. SyntaxMath.acos(number) number is a numeric expression between -1 and 1, or a property of an existing object. DescriptionThe acos method returns a numeric value between 0 and pi radians. If the value of number is outside the suggested range, the return value is always 0. Method ofExamples// Displays the value 0 document.write("The arc cosine of 1 is " + Math.acos(1)) // Displays the value 3.141592653589793 document.write("<P>The arc cosine of -1 is " + Math.acos(-1)) // Displays the value 0 document.write("<P>The arc cosine of 2 is " + Math.acos(2)) See alsoalert methodDisplays an Alert dialog box with a message and an OK button. Syntaxalert("message") message is any string or a property of an existing object. DescriptionUse 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. Although alert is a method of the window object, you do not need to specify a windowReference when you call it. For example, windowReference.alert() is unnecessary. Method ofExamplesIn the following example, the testValue() function checks the name entered by a user in the text object 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 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 object, as shown in the following example: Name: <INPUT TYPE="text" NAME="userName" onBlur="testValue(userName.value)"> See alsoanchor methodCreates an HTML anchor that is used as a hypertext target. Syntaxtext.anchor(nameAttribute) text is any string or a property of an existing object.
DescriptionUse the anchor method with the write or writeln methods to programatically create and display an anchor in a document. Create the anchor with the anchor method, then call write or writeln to display the anchor in a document. In the syntax, the text string represents the literal text that you want the user to see. The nameAttribute string represents the NAME attribute of the <A> tag. Anchors created with the anchor method become elements in the anchors array. See the anchor object for information about the anchors array. Method ofExamplesThe following example opens the msgWindow window and creates an anchor for the Table of Contents: The previous example produces the same output as the following HTML: See alsoasin methodReturns the arc sine (in radians) of a number. SyntaxMath.asin(number) number is a numeric expression between -1 and 1, or a property of an existing object. DescriptionThe asin method returns a numeric value between -pi/2 and pi/2 radians. If the value of number is outside the suggested range, the return value is always 0. Method ofExamples// Displays the value 1.570796326794897 (pi/2) document.write("The arc sine of 1 is " + Math.asin(1)) // Displays the value -1.570796326794897 (-pi/2) document.write("<P>The arc sine of -1 is " + Math.asin(-1)) // Displays the value 0 because the argument is out of range document.write("<P>The arc sine of 2 is " + Math.asin(2)) See alsoatan methodReturns the arc tangent (in radians) of a number. SyntaxMath.atan(number) number is either a numeric expression or a property of an existing object, representing the tangent of an angle. DescriptionThe atan method returns a numeric value between -pi/2 and pi/2 radians. Method ofExamples// Displays the value 0.7853981633974483 document.write("The arc tangent of 1 is " + Math.atan(1)) // Displays the value -0.7853981633974483 document.write("<P>The arc tangent of -1 is " + Math.atan(-1)) // Displays the value 0.4636476090008061 document.write("<P>The arc tangent of .5 is " + Math.atan(.5)) See alsoback methodLoads the previous URL in the history list. Syntaxhistory.back() DescriptionThis method performs the same action as a user choosing the Back button in the Navigator. The back method is the same as history.go(-1). Method ofExamplesThe following custom buttons perform the same operations as the Navigator Back and Forward buttons:<P><INPUT TYPE="button" VALUE="< Back" onClick="history.back()"> <P><INPUT TYPE="button" VALUE="> Forward" onClick="history.forward()"> See alsobig methodCauses a string to be displayed in a big font as if it were in a <BIG> tag. SyntaxstringName.big() stringName is any string or a property of an existing object. DescriptionUse the big method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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: Hello, world See alsoblink methodCauses a string to blink as if it were in a <BLINK> tag. SyntaxstringName.blink() stringName is any string or a property of an existing object. DescriptionUse the blink method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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: Hello, world Hello, world
See alsoblur methodRemoves focus from the specified object. Syntax1. passwordName.blur() 2. selectName.blur() 3. textName.blur() 4. textareaName.blur() passwordName is either the value of the NAME attribute of a password object or an element in the elements array.
DescriptionUse the blur method to remove focus from a specific form element. Method ofpassword, select, text, textarea ExamplesThe following example removes focus from the password element userPass: userPass.blur()This example assumes that the password is defined as: <INPUT TYPE="password" NAME="userPass"> See alsobold methodCauses a string to be displayed as bold as if it were in a <B> tag. SyntaxstringName.bold() stringName is any string or a property of an existing object. DescriptionUse the bold method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsoceil methodReturns the least integer greater than or equal to a number. SyntaxMath.ceil(number) number is any numeric expression or a property of an existing object. Method ofExamples//Displays the value 46 document.write("The ceil of 45.95 is " + Math.ceil(45.95)) //Displays the value -45 document.write("<P>The ceil of -45.95 is " + Math.ceil(-45.95)) See alsocharAt methodReturns the character at the specified index. SyntaxstringName.charAt(index) stringName is any string or a property of an existing object.
DescriptionCharacters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1. If the index you supply is out of range, JavaScript returns an empty string. Method ofExamplesThe following example displays characters at different locations in the string "Brave new world".var anyString="Brave new world" document.write("The character at index 0 is " + anyString.charAt(0)) document.write("The character at index 1 is " + anyString.charAt(1)) document.write("The character at index 2 is " + anyString.charAt(2)) document.write("The character at index 3 is " + anyString.charAt(3)) document.write("The character at index 4 is " + anyString.charAt(4)) See alsoclear methodClears the document in a window. Syntaxdocument.clear() DescriptionThe clear method empties the content of a window, regardless of how the content of the window has been painted. Method ofExamplesWhen the following function is called, the clear method empties the contents of the msgWindow window: function windowCleaner() { msgWindow.document.clear() msgWindow.document.close() } See alsoclearTimeout methodCancels a timeout that was set with the setTimeout method. SyntaxclearTimeout(timeoutID) timeoutID is a timeout setting that was returned by a previous call to the setTimeout method. DescriptionSee the description for the setTimeout method. Method ofExamplesSee the examples for the setTimeout method. See alsoclick methodSimulates a mouse click on the calling form element. Syntax1. buttonName.click() 2. radioName[index].click() buttonName is either the value of the NAME attribute of a button, reset, or submit object or an element in the elements array.
DescriptionThe effect of the click method varies according to the calling element: Method ofbutton, checkbox, radio, reset, submit ExamplesThe following example toggles the selection status of the first radio button in the musicType radio object on the musicForm form: document.musicForm.musicType[0].click() The following example toggles the selection status of the newAge checkbox on the musicForm form: document.musicForm.newAge.click() close method (document object)Closes an output stream and forces data sent to layout to display. Syntaxdocument.close() DescriptionThe close method closes a stream opened with the document.open() method. If the stream was opened to layout, the close method forces the content of the stream to display. Font style tags, such as <BIG> and <CENTER>, automatically flush a layout stream. The close method also stops the "meteor shower" in the Netscape icon and displays "Document: Done" in the status bar. Method ofExamplesThe following function calls document.close() to close a stream that was opened with document.open(). The document.close() method forces the content of the stream to display in the window.function windowWriter1() { var myString = "Hello, world!" msgWindow.document.open() msgWindow.document.write(myString + "<P>") msgWindow.document.close() } See alsoclose method (window object)Closes the specified window. SyntaxwindowReference.close() windowReference is a valid way of referring to a window, as described in the window object. DescriptionThe close method closes the specified window. If you call close without specifying a windowReference, JavaScript closes the current window. In event handlers, you must specify window.close() instead of simply using close(). Due to the scoping of static objects in JavaScript, a call to close() without specifying an object name is equivalent to document.close(). Method ofExamplesAny of the following examples close the current window: window.close() self.close() close() The following example closes the messageWin window: messageWin.close() This example assumes that the window was opened in a manner similar to the following: messageWin=window.open("") See alsoconfirm methodDisplays a Confirm dialog box with the specified message and OK and Cancel buttons. Syntaxconfirm("message") message is any string or a property of an existing object. DescriptionUse 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 decision. The confirm method returns true if the user chooses OK and false if the user chooses Cancel. Although confirm is a method of the window object, you do not need to specify a windowReference when you call it. For example, windowReference.confirm() is unnecessary. Method ofExamplesThis 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 alsocos methodReturns the cosine of a number. SyntaxMath.cos(number) number is a numeric expression representing the size of an angle in radians, or a property of an existing object. DescriptionThe cos method returns a numeric value between -1 and 1, which represents the cosine of the angle. Method ofExamples//Displays the value 6.123031769111886e-017 document.write("The cosine of PI/2 radians is " + Math.cos(Math.PI/2)) //Displays the value -1 document.write("<P>The cosine of PI radians is " + Math.cos(Math.PI)) //Displays the value 1 document.write("<P>The cosine of 0 radians is " + Math.cos(0)) See alsoescape functionReturns the ASCII encoding of an argument in the ISO Latin-1 character set. Syntaxescape("string") string is a non-alphanumeric string in the ISO Latin-1 character set, or a property of an existing object. DescriptionThe escape function is not a method associated with any object, but is part of the language itself. The value returned by the escape function is a string of the form "%xx", where xx is the ASCII encoding of a character in the argument. If you pass the escape function an alphanumeric character, the escape function returns the same character. ExamplesThe following example returns "%26" escape("&") The following example returns "%21%23" escape("!#") See alsoeval functionThe eval function evaluates a string and returns a value. Syntaxeval(string)string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. DescriptionThe eval function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself. The argument of the eval function is a string. Do not call eval to evaluate an arithmetic expression. JavaScript evaluates arithmetic expressions automatically. If the argument represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3*x + 2", to a variable, and then calling eval at a later point in your script. ExampleBoth of the write statements below display 42. The first evaluates the string "x + y + 1", and the second evaluates the string "42". var x = 2 var y = 39 var z = "42" document.write(eval("x + y + 1"), " In the following example, the getFieldName(n) function returns the name of the nth form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element. var field = getFieldName(3) document.write("The field named ", field, " has value of ", eval(field + ".value")) The following example uses eval to evaluate the string str. This string consists of JavaScript statements that opens an alert dialog box and assigns z a value of 42 if x is five, and assigns zero to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z. var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; " document.write(" exp methodReturns enumber, where number is the argument, and e is Euler's constant, the base of the natural logarithms. SyntaxMath.exp(number)number is any numeric expression or a property of an existing object. Method ofExamples//Displays the value 2.718281828459045 document.write("The value of e<SUP>1</SUP> is " + Math.exp(1)) See alsofixed methodCauses a string to be displayed in fixed-pitch font as if it were in a <TT> tag. SyntaxstringName.fixed() stringName is any string or a property of an existing object. DescriptionUse the fixed method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe following example uses the fixed method to change the formatting of a string:var worldString="Hello, world" document.write(worldString.fixed()) The previous example produces the same output as the following HTML: <TT>Hello, world</TT> floor methodReturns the greatest integer less than or equal to a number. SyntaxMath.floor(number) number is any numeric expression or a property of an existing object. Method ofExamples//Displays the value 45 document.write("<P>The floor of 45.95 is " + Math.floor(45.95)) //Displays the value -46 document.write("<P>The floor of -45.95 is " + Math.floor(-45.95)) See alsofocus methodGives focus to the specified object. Syntax1. passwordName.focus() 2. selectName.focus() 3. textName.focus() 4. textareaName.focus() passwordName is either the value of the NAME attribute of a password object or an element in the elements array.
DescriptionUse 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. Method ofpassword, select, text, textarea ExamplesIn 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 object 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 alsofontcolor methodCauses a string to be displayed in the specified color as if it were in a <FONT COLOR=color> tag. SyntaxstringName.fontcolor(color) stringName is any string or a property of an existing object.
DescriptionUse the fontcolor method with the write or writeln methods to format and display a string in a document. If you express color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072". The fontcolor method overrides a value set in the fgColor property. Method ofExamplesThe following example uses the fontcolor method to change the color of a stringvar worldString="Hello, world" document.write(worldString.fontcolor("maroon") + " is maroon in this line") document.write("<P>" + worldString.fontcolor("salmon") + " is salmon in this line") document.write("<P>" + worldString.fontcolor("red") + " is red in this line") document.write("<P>" + worldString.fontcolor("8000") + " is maroon in hexadecimal in this line") document.write("<P>" + worldString.fontcolor("FA8072") + " is salmon in hexadecimal in this line") document.write("<P>" + worldString.fontcolor("FF00") + " is red in hexadecimal in this line") The previous example produces the same output as the following HTML: <FONT COLOR="maroon">Hello, world</FONT> is maroon in this line <P><FONT COLOR="salmon">Hello, world</FONT> is salmon in this line <P><FONT COLOR="red">Hello, world</FONT> is red in this line <FONT COLOR="8000">Hello, world</FONT> is maroon in hexadecimal in this line <P><FONT COLOR="FA8072">Hello, world</FONT> is salmon in hexadecimal in this line <P><FONT COLOR="FF00">Hello, world</FONT> is red in hexadecimal in this line fontsize methodCauses a string to be displayed in the specified font size as if it were in a <FONTSIZE=size> tag. SyntaxstringName.fontsize(size) stringName is any string or a property of an existing object.
DescriptionUse the fontsize method with the write or writeln methods to format and display a string in a document. When you specify size as an integer, you set the size of stringName to one of the seven defined sizes. When you specify size as a string such as "-2", you adjust the font size of stringName relative to the size set in the <BASEFONT> tag. Method ofExamplesThe 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 alsoforward methodLoads the next URL in the history list. Syntaxhistory.forward() DescriptionThis method performs the same action as a user choosing the Forward button in the Navigator. The forward method is the same as history.go(1). Method ofExamplesThe following custom buttons perform the same operations as the Navigator Back and Forward buttons:<P><INPUT TYPE="button" VALUE="< Back" onClick="history.back()"> <P><INPUT TYPE="button" VALUE="> Forward" onClick="history.forward()"> See alsogetDate methodReturns the day of the month for the specified date. SyntaxdateObjectName.getDate() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getDate is an integer between 1 and 31. Method ofExamplesThe 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 alsogetDay methodReturns the day of the week for the specified date. SyntaxdateObjectName.getDay() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getDay is an integer corresponding to the day of the week: zero for Sunday, one for Monday, two for Tuesday, and so on. Method ofExamplesThe 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() getHours methodReturns the hour for the specified date. SyntaxdateObjectName.getHours() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getHours is an integer between 0 and 23. Method ofExamplesThe 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 alsogetMinutes methodReturns the minutes in the specified date. SyntaxdateObjectName.getMinutes() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getMinutes is an integer between 0 and 59. Method ofExamplesThe 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 alsogetMonth methodReturns the month in the specified date. SyntaxdateObjectName.getMonth() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getMonth is an integer between zero and eleven. Zero corresponds to January, one to February, and so on. Method ofExamplesThe 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 alsogetSeconds methodReturns the seconds in the current time. SyntaxdateObjectName.getSeconds() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getSeconds is an integer between 0 and 59. Method ofExamplesThe 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 alsogetTime methodReturns the numeric value corresponding to the time for the specified date. SyntaxdateObjectName.getTime() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00. You can use this method to help assign a date and time to another date object. Method ofExamplesThe following example assigns the date value of theBigDay to sameAsBigDay. theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date() sameAsBigDay.setTime(theBigDay.getTime()) See alsogetTimezoneOffset methodReturns the time zone offset in minutes for the current locale. SyntaxdateObjectName.getTimezoneOffset() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe time zone offset is the difference between local time and GMT. Daylight savings time prevents this value from being a constant. Method ofExamplesx = new Date() currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60 getYear methodReturns the year in the specified date. SyntaxdateObjectName.getYear() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe value returned by getYear is the year less 1900. For example, if the year is 1976, the value returned is 76. Method ofExamplesThe 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 alsogo methodLoads a URL from the history list. Syntaxhistory.go(delta | "location")delta is an integer or a property of an existing object, representing a relative position in the history list. location is a string or a property of an existing object, representing all or part of a URL in the history list. DescriptionThe go method navigates to the location in the history list determined by the argument that you specify. You can interactively display the history list by choosing History from the Window menu. Up to 10 items in the history list are also displayed on the Go menu. The delta argument is a positive or negative integer. If delta is greater than zero, the go method 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. If delta is 0, Navigator reloads the current page. The location argument is a string. Use location to load the nearest history entry whose URL contains location as a substring. The location to URL matching is case-insensitive. Each section of a URL contains different information. See the location object for a description of the URL components. Method ofExamplesThe following button navigates to the nearest history entry that contains the string "home.netscape.com": <P><INPUT TYPE="button" VALUE="Go" onClick="history.go('home.netscape.com')"> The following button navigates to the URL that is three entries backward in the history list: <P><INPUT TYPE="button" VALUE="Go" onClick="history.go(-3)"> See alsoindexOf methodReturns the index within the calling string object of the first occurrence of the specified value, starting the search at fromIndex. SyntaxstringName.indexOf(searchValue, [fromIndex]) stringName is any string or a property of an existing object.
DescriptionCharacters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1. If you do not specify a value for fromIndex, JavaScript assumes 0 by default. If searchValue is not found, JavaScript returns -1. Method ofExamplesThe following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".var anyString="Brave new world" //Displays 8 document.write("<P>The index of the first w from the beginning is " + anyString.indexOf("w")) //Displays 10 document.write("<P>The index of the first w from the end is " + anyString.lastIndexOf("w")) //Displays 6 document.write("<P>The index of 'new' from the beginning is " + anyString.indexOf("new")) //Displays 6 document.write("<P>The index of 'new' from the end is " + anyString.lastIndexOf("new")) See alsoisNaN functionOn Unix platforms, evaluates an argument to determine if it is "NaN" (not a number). SyntaxisNaN(testValue) testValue is the value you want to evaluate. DescriptionThe isNaN function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself. isNaN is available on Unix platforms only. On all platforms except Windows, the parseFloat and parseInt functions return "NaN" when they evaluate a value that is not a number. The "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat or parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN". The isNaN function returns true or false. ExamplesThe following example evaluates floatValue to determine if it is a number, then calls a procedure accordingly. floatValue=parseFloat(toFloat) if isNaN(floatValue) { notFloat() } else { isFloat() } See alsoitalics methodCauses a string to be italicized as if it were in an <I> tag. SyntaxstringName.italics() stringName is any string or a property of an existing object. DescriptionUse the italics method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsolastIndexOf methodReturns the index within the calling string object of the last occurrence of the specified value. The calling string is searched backwards, starting at fromIndex. SyntaxstringName.lastIndexOf(searchValue, [fromIndex]) stringName is any string or a property of an existing object.
DescriptionCharacters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1. If you do not specify a value for fromIndex, JavaScript assumes stringName.length - 1 (the end of the string) by default. If searchValue is not found, JavaScript returns -1. Method ofExamplesThe following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".var anyString="Brave new world" //Displays 8 document.write("<P>The index of the first w from the beginning is " + anyString.indexOf("w")) //Displays 10 document.write("<P>The index of the first w from the end is " + anyString.lastIndexOf("w")) //Displays 6 document.write("<P>The index of 'new' from the beginning is " + anyString.indexOf("new")) //Displays 6 document.write("<P>The index of 'new' from the end is " + anyString.lastIndexOf("new")) See alsolink methodCreates an HTML hypertext link that jumps to another URL. SyntaxlinkText.link(hrefAttribute) linkText is any string or a property of an existing object.
DescriptionUse the link method with the write or writeln methods to programatically create and display a hypertext link in a document. Create the link with the link method, then call write or writeln to display the link in a document. In the syntax, the linkText string represents the literal text that you want the user to see. The hrefAttribute string represents the HREF attribute of the <A> tag, and it should be a valid URL. Each section of a URL contains different information. See the location object for a description of the URL components. Links created with the link method become elements in the links array. See the link object for information about the links array. Method ofExamplesThe following example displays the word "Netscape" as a hypertext link that returns the user to the Netscape home page: var hotText="Netscape" var URL="http://www.netscape.com" document.open() document.write("Click to return to " + hotText.link(URL)) document.close() The previous example produces the same output as the following HTML: Click to return to <A HREF="http://www.netscape.com">Netscape</A> See alsolog methodReturns the natural logarithm (base e) of a number. SyntaxMath.log(number)number is any positive numeric expression or a property of an existing object. DescriptionIf the value of number is outside the suggested range, the return value is always -1.797693134862316e+308. Method ofExamples//Displays the value 2.302585092994046 document.write("The natural log of 10 is " + Math.log(10)) //Displays the value 0 document.write("<P>The natural log of 1 is " + Math.log(1)) //Displays the value -1.797693134862316e+308 //because the argument is out of range document.write("<P>The natural log of 0 is " + Math.log(0)) See alsomax methodReturns the greater of two numbers. Syntaxmax(number1, number2)number1 and number2 are any numeric arguments or the properties of existing objects. Method ofExamples//Displays the value 20 document.write("The maximum value is " + Math.max(10,20)) //Displays the value -10 document.write("<P>The maximum value is " + Math.max(-10,-20)) See alsomin methodReturns the lesser of two numbers. Syntaxmin(number1, number2)number1 and number2 are any numeric arguments or the properties of existing objects. Method ofExamples//Displays the value 10 document.write(" See also(O-Z)
open method (document object)Opens a stream to collect the output of write or writeln methods. Syntaxdocument.open(["mimeType"]) mimeType specifies any of the following document types: text/html text/plain image/gif image/jpeg image/x-bitmap plugInplugIn is any two-part plug-in MIME type that Netscape supports. DescriptionThe open method opens a stream to collect the output of write or writeln methods. If the mimeType is text or image, the stream is opened to layout; otherwise, the stream is opened to a plug-in. If a document exists in the target window, the open method clears it. End the stream by using the document.close() method. The close method causes text or images that were sent to layout to display. After using document.close(), issue document.open() again when you want to begin another output stream. mimeType is an optional argument that specifies the type of document to which you are writing. If you do not specify a mimeType, the open method assumes text/html by default. Following is a description of mimeType: Method ofExamplesThe following function calls document.open() to open a stream before issuing a write method:function windowWriter1() { var myString = "Hello, world!" msgWindow.document.open() msgWindow.document.write("<P>" + myString) msgWindow.document.close() }In the following example, the probePlugIn() function determines whether a user has the ShockWave plug-in installed: function probePlugIn(mimeType) { var havePlugIn = false var tiny = window.open("", "teensy", "width=1,height=1") if (tiny != null) { if (tiny.document.open(mimeType) != null) havePlugIn = true tiny.close() } return havePlugIn } var haveShockWavePlugIn = probePlugIn("application/x-director") See alsoopen method (window object)Opens a new web browser window. Syntax[windowVar = ][window].open("URL", "windowName", ["windowFeatures"]) windowVar is the name of a new window. Use this variable when referring to a window's properties, methods, and containership.
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] 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. DescriptionThe 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. In event handlers, you must specify window.open() instead of simply using open(). Due to the scoping of static objects in JavaScript, a call to open() without specifying an object name is equivalent to document.open(). 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. If you specify any item in windowFeatures, all other Boolean windowFeatures are false unless you explicitly specify them. Following is a description of the windowFeatures: Method ofExamplesIn the following example, the windowOpener function opens a window and uses write methods to display a message: function windowOpener() { msgWindow=window.open("","displayWindow","menubar=yes") 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. The window opens with the specified option settings; all other options are false because they are not specified. Notice the use of single quotes (') inside the onClick event handler. See alsoparse methodReturns the number of milliseconds in a date string since January 1, 1970 00:00:00, local time. SyntaxDate.parse(dateString)dateString is a string representing a date or a property of an existing object. DescriptionThe parse method 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 and the Date object. 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.
Because the parse function is a static method of Date, you always use it as Method ofExamplesIf IPOdate is an existing date object, then IPOdate.setTime(Date.parse("Aug 9, 1995")) See alsoparseFloat functionParses a string argument and returns a floating point number. SyntaxparseFloat(string) string is a string that represents the value you want to parse. DescriptionThe parseFloat function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself. parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, parseFloat returns one of the following values: For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN". ExamplesThe following examples all return 3.14: parseFloat("3.14") parseFloat("314e-2") parseFloat("0.0314E+2") var x = "3.14" parseFloat(x) The following example returns "NaN" or 0: parseFloat("FF2") See alsoparseInt functionParses a string argument and returns an integer of the specified radix or base. SyntaxparseInt(string [,radix]) string is a string that represents the value you want to parse.
DescriptionThe parseInt function is a built-in JavaScript function. It is not a method associated with any object, but is part of the language itself. The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used. If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. ParseInt truncates numbers to integer values. If the radix is not specified or is specified as 0, JavaScript assumes the following: If the first character cannot be converted to a number, parseFloat returns one of the following values: For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN". ExamplesThe following examples all return 15: parseInt("F", 16) parseInt("17", 8) parseInt("15", 10) parseInt(15.99, 10) parseInt("FXX123", 16) parseInt("1111", 2) parseInt("15*3", 10) The following examples all return "NaN" or 0: parseInt("Hello", 8) parseInt("0x7", 10) parseInt("FFF", 10) Even though the radix is specified differently, the following examples all return 17 because the input string begins with "0x". parseInt("0x11", 16) parseInt("0x11", 0) parseInt("0x11") See alsopow methodReturns base to the exponent power, that is, baseexponent. Syntaxpow(base, exponent)base is any numeric expression or a property of an existing object. exponent is any numeric expression or a property of an existing object. Method ofExamples//Displays the value 49 document.write("7 to the power of 2 is " + Math.pow(7,2)) //Displays the value 1024 document.write("<P>2 to the power of 10 is " + Math.pow(2,10)) See alsoprompt methodDisplays a Prompt dialog box with a message and an input field. Syntaxprompt(message, [inputDefault])message is any string or a property of an existing object; the string is displayed as the message. inputDefault is a string, integer, or property of an existing object that represents the default value of the input field. DescriptionUse the prompt method to display a dialog box that receives user input. If you do not specify an initial value for inputDefault, the dialog box displays the value <undefined>. Although prompt is a method of the window object, you do not need to specify a windowReference when you call it. For example, windowReference.prompt() is unnecessary. Method ofExamplesprompt("Enter the number of cookies you want to order:", 12) See alsorandom methodReturns a pseudo-random number between zero and one. This method is available on Unix platforms only. SyntaxMath.random() Method ofExamples//Displays a random number between 0 and 1 document.write("The random number is " + Math.random()) round methodReturns the value of a number rounded to the nearest integer. Syntaxround(number)number is any numeric expression or a property of an existing object. DescriptionIf the fractional portion of number is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number is less than .5, the argument is rounded to the next lowest integer. Method ofExamples//Displays the value 20 document.write("The rounded value is " + Math.round(20.49)) //Displays the value 21 document.write("<P>The rounded value is " + Math.round(20.5)) //Displays the value -20 document.write("<P>The rounded value is " + Math.round(-20.5)) //Displays the value -21 document.write("<P>The rounded value is " + Math.round(-20.51)) select methodSelects the input area of the specified password, text, or textarea object. Syntax1. passwordName.select() 2. textName.select() 3. textareaName.select() passwordName is either the value of the NAME attribute of a password object or an element in the elements array.
DescriptionUse 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. Method ofExamplesIn 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 alsosetDate methodSets the day of the month for a specified date. SyntaxdateObjectName.setDate(dayValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplesThe 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") theBigDay.setDate(24) See alsosetHours methodSets the hours for a specified date. SyntaxdateObjectName.setHours(hoursValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplestheBigDay.setHours(7) See alsosetMinutes methodSets the minutes for a specified date. SyntaxdateObjectName.setMinutes(minutesValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplestheBigDay.setMinutes(45) See alsosetMonth methodSets the month for a specified date. SyntaxdateObjectName.setMonth(monthValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplestheBigDay.setMonth(6) See alsosetSeconds methodSets the seconds for a specified date. SyntaxdateObjectName.setSeconds(secondsValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplestheBigDay.setSeconds(30) See alsosetTime methodSets the value of a date object. SyntaxdateObjectName.setTime(timevalue) dateObjectName is either the name of a date object or a property of an existing object.
DescriptionUse the setTime method to help assign a date and time to another date object. Method ofExamplestheBigDay = new Date("July 1, 1999") sameAsBigDay = new Date() sameAsBigDay.setTime(theBigDay.getTime()) See alsosetTimeout methodEvaluates an expression after a specified number of milliseconds have elapsed. SyntaxtimeoutID=setTimeout(expression, msec) timeoutID is an identifier that is used only to cancel the evaluation with the clearTimeout method.
DescriptionThe 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 5 seconds, the expression is evaluated after 5 seconds, not every 5 seconds. Method ofExamplesExample 1. The following example displays an alert message 5 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 cancelled and the alert does not display. Example 2. The following example displays the current time in a text object. The showtime() function, which is called recursively, uses the setTimeout method update the time every second. See alsosetYear methodSets the year for a specified date. SyntaxdateObjectName.setYear(yearValue) dateObjectName is either the name of a date object or a property of an existing object.
Method ofExamplestheBigDay.setYear(96) See alsosin methodReturns the sine of a number. SyntaxMath.sin(number) number is a numeric expression or a property of an existing object, representing the size of an angle in radians. DescriptionThe sin method returns a numeric value between -1 and 1, which represents the sine of the angle. Method ofExamples//Displays the value 1 document.write("The sine of pi/2 radians is " + Math.sin(Math.PI/2)) //Displays the value 1.224606353822377e-016 document.write("<P>The sine of pi radians is " + Math.sin(Math.PI)) //Displays the value 0 document.write("<P>The sine of 0 radians is " + Math.sin(0)) See alsosmall methodCauses a string to be displayed in a small font as if it were in a <SMALL> tag. SyntaxstringName.small() stringName is any string or a property of an existing object. DescriptionUse the small method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsosqrt methodReturns the square root of a number. SyntaxMath.sqrt(number)number is any non-negative numeric expression or a property of an existing object. DescriptionIf the value of number is outside the suggested range, the return value is always 0. Method ofExamples//Displays the value 3 document.write("The square root of 9 is " + Math.sqrt(9)) //Displays the value 1.414213562373095 document.write("<P>The square root of 2 is " + Math.sqrt(2)) //Displays the value 0 because the argument is out of range document.write("<P>The square root of -1 is " + Math.sqrt(-1)) strike methodCauses a string to be displayed as struck out text as if it were in a <STRIKE> tag. SyntaxstringName.strike() stringName is any string or a property of an existing object. DescriptionUse the strike method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsosub methodCauses a string to be displayed as a subscript as if it were in a <SUB> tag. SyntaxstringName.sub() stringName is any string or a property of an existing object. DescriptionUse the sub method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsosup method submit methodSubmits a form. SyntaxformName.submit() formName is the name of any form or an element in the forms array. DescriptionThe 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. Method ofExamplesThe 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 alsosubstring methodReturns a subset of a string object. SyntaxstringName.substring(indexA, indexB) stringName is any string or a property of an existing object.
DescriptionCharacters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1. If indexA is less than indexB, the substring method returns the subset starting with the character at indexA and ending with the character before indexB. If indexA is greater than indexB, the substring method returns the subset starting with the character at indexB and ending with the character before indexA. If indexA is equal to indexB, the substring method returns the empty string. Method ofExamplesThe 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)) sup methodCauses a string to be displayed as a superscript as if it were in a <SUP> tag. SyntaxstringName.sup() stringName is any string or a property of an existing object. DescriptionUse the sup method with the write or writeln methods to format and display a string in a document. Method ofExamplesThe 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 alsotan methodReturns the tangent of a number. SyntaxMath.tan(number) number is a numeric expression representing the size of an angle in radians, or a property of an existing object. DescriptionThe tan method returns a numeric value which represents the tangent of the angle. Method ofExamples//Displays the value 0.9999999999999999 document.write("The tangent of pi/4 radians is " + Math.tan(Math.PI/4)) //Displays the value 0 document.write("<P>The tangent of 0 radians is " + Math.tan(0)) See alsotoGMTString methodConverts a date to a string, using the Internet GMT conventions. SyntaxdateObjectName.toGMTString() dateObjectName is either the name of a date object or a property of an existing object. DescriptionThe exact format of the value returned by toGMTString varies according to the platform. Method ofExamplesIn the following example, today is a date object:today.toGMTString() In this example, the toGMTString method converts the date to GMT (UTC) using the operating system's time zone offset and returns a string value that is similar to the following form. The exact format depends on the platform. Mon, 18 Dec 1995 17:28:35 GMT See alsotoLocaleString methodConverts a date to a string, using the current locale's conventions. SyntaxdateObjectName.toLocaleString() dateObjectName is either the name of a date object or a property of an existing object. DescriptionIf you are trying to pass a date using toLocaleString, be aware that different locales assemble the string in different ways. Using methods such as getHours, getMinutes, and getSeconds will give more portable results.Method ofExamplesIn the following example, today is a date object:today.toLocaleString() In this example, toLocaleString returns a string value that is similar to the following form. The exact format depends on the platform. 12/18/95 17:28:35 See alsotoLowerCase methodReturns the calling string value converted to lower case. SyntaxstringName.toLowerCase() stringName is any string or a property of an existing object. DescriptionThe toLowerCase method returns the value of stringName converted to lower case. toLowerCase does not affect the value of stringName itself. Method ofExamplesThe following examples both yield "alphabet". var upperText="ALPHABET" document.write(upperText.toLowerCase()) "ALPHABET".toLowerCase() See alsotoUpperCase methodReturns the calling string value converted to upper case. SyntaxstringName.toUpperCase() stringName is any string or a property of an existing object. DescriptionThe toUpperCase method returns the value of stringName converted to upper case. toUpperCase does not affect the value of stringName itself. Method ofExamplesThe following examples both yield "ALPHABET". var lowerText="alphabet" document.write(lowerText.toUpperCase()) "alphabet".toUpperCase() See alsounescape functionReturns the ASCII string for the specified value. Syntaxunescape("string") string is a string or a property of an existing object, containing characters in either of the following forms: DescriptionThe unescape function is not a method associated with any object, but is part of the language itself. The string returned by the unescape function is a series of characters in the ISO Latin-1 character set. ExamplesThe following example returns "&" unescape("%26") The following example returns "!#" unescape("%21%23") See alsoUTC methodReturns the number of milliseconds in a date object since January 1, 1970 00:00:00, Universal Coordinated Time (GMT). SyntaxDate.UTC(year, month, day [, hrs] [, min] [, sec]) year is a year after 1900.
DescriptionUTC takes comma-delimited date parameters and returns the number of milliseconds since January 1, 1970 00:00:00, Universal Coordinated Time (GMT). Because UTC is a static method of Date, you always use it as Date.UTC(), rather than as a method of a date object you created. Method ofExamplesThe following statement creates a date object using GMT instead of local time: gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0)) See alsowrite methodWrites one or more HTML expressions to a document in the specified window. Syntaxwrite(expression1 [,expression2], ...[,expressionN])expression1 through expressionN are any JavaScript expressions or the properties of existing objects. DescriptionThe write method displays any number of expressions in a document window. You can specify any JavaScript expression with the write method, including numerics, strings, or logicals. The write method is the same as the writeln method, except the write method does not append a newline character to the end of the output. Use the write method within any <SCRIPT> tag or within an event handler. Event handlers execute after the original document closes, so the write method will implicitly open a new document of mimeType text/html if you do not explicitly issue a document.open() method in the event handler. Method ofExamplesIn the following example, the write method takes several arguments, including strings, a numeric, and a variable:var mystery = "world" // Displays Hello world testing 123 msgWindow.document.write("Hello ", mystery, " testing ", 123)In the following example, the write method takes two arguments. The first argument is an assignment expression, and the second argument is a string literal. //Displays Hello world... msgWindow.document.write(mystr = "Hello "+ "world...")In the following example, the write method takes a single argument that is a conditional expression. If the value of the variable age is less than 18, the method displays "Minor". If the value of age is greater than or equal to 18, the method displays "Adult". msgWindow.document.write(status = (age >= 18) ? "Adult" : "Minor") See alsowriteln methodWrites one or more HTML expressions to a document in the specified window and follows them with a newline character. Syntaxwriteln(expression1 [,expression2], ...[,expressionN])expression1 through expressionN are any JavaScript expressions or the properties of existing objects. DescriptionThe writeln method displays any number of expressions in a document window. You can specify any JavaScript expression, including numerics, strings, or logicals. The writeln method is the same as the write method, except the writeln method appends a newline character to the end of the output. HTML ignores the newline character, except within certain tags such as <PRE>. Use the writeln method within any <SCRIPT> tag or within an event handler. Event handlers execute after the original document closes, so the writeln method will implicitly open a new document of mimeType text/html if you do not explicitly issue a document.open() method in the event handler. Method ofExamplesAll the examples used for the write method are also valid with the writeln method. See also |