Previous Example-|-Next Example-|-Return to Chapter Listing
Example 7.12:
Built-in Functions--parseInt
This page uses a built-in function, parseInt(form.txtNum.value,nBase), to return an integer value. It examines a string, then extracts a number from the string assuming that the number is in the specified Radix (base). Then it converts the number to decimal. If it's unable to find a number it either returns NaN, or, if the browser is a JavaScript 1.1 browser (Navigator 2, some beta versions of Navigator 3 , and Internet Explorer 3), it returns 0 instead of NaN. (JavaScript 1.1 browsers running on Solaris or Irix will return NaN.)
Type text into the first text box, select the Radix of the number you typed into the text box, then click on the button to see the result.
Remember the data from a form is a string. This function converts a string to a number, if it can. It looks at the string, then tries to extract a number in the specified base from the string, then convert it to decimal.
These are the scripts we used. First, in the HEAD:
Later in the Web page the function is called from this form:
Previous Example-|-Next Example-|-Return to Chapter Listing