Global Functions Information
CSGNetwork FREE Source Code, Javascript Source, and Code Snippets!
CSGNetwork Custom Search
     Bookmark and Share
 
CSGNetwork
Snippets
Calculators
Top Free Apps
Home
Back



Right click this window and select "view source" in order to copy the source for this script.

Global Functions

Global, or top level functions, are functions in JavaScript that are independent of any particular object.

Global functions

Note: "[]" surrounding a parameter below means the parameter is optional.

Operator Description
parseInt(x, [radix]) Parses any string "x" and returns the first valid number (integer) it encounters. If the first character in the string is not a number, white spaces, or a leading minus sign, parseInt() returns NaN instead. You can test for NaN using the isNaN() function below.

parseInt() supports an optional 2nd "radix" parameter to specify the base of the number to be parsed (valid range is 2-36). Entering "10" would parse the number in the familiar decimal system, while "16" would be hexadecimal. Without this parameter present, parseInt() assumes any number that begins with "0x" to be radix 16, "0" to be radix 8, and any other number to be radix 10.

Examples:

parseInt("3 chances") //returns 3
parseInt("   5 alive") //returns 5
parseInt("I have 3 computers") //returns NaN
parseInt("17", 8) //returns 15

parseFloat(x) Parses any string "x" and returns the first valid floating point number it encounters. Use this function to extract numbers with decimals, for example. If the first character in the string is not a number, white spaces, or a leading minus sign, parseFloat() returns NaN instead. You can test for NaN using the isNaN() function below.

Example:

parseFloat("-3.98 points") //returns -3.98

isNaN(x) isNaN() checks its parameter "x" to see if it's the value "NaN" (not a number)-  an illegal number. The function returns true if "x" is NaN, and false if not. A common example of NaN is 0 divided by 0. This function is required to detect NaN, as using equality operators (== or ===) won't do.
isFinite(x) Determines whether a number is finite. Returns false if x is +infinity, -infinity, or NaN. JavaScript 1.3 statement.
eval(s) Evaluates the string ("s") and returns the results of the evaluation (if available). Eval() allows you to dynamically construct a JavaScript statement or expression.

Examples:

eval("8+3+1") //returns 12
eval("alert(Math.random())") //alerts Math.random()

escape(s)

 

Accepts a string "s" and returns its encoded version. Original string isn't modified. All characters within the string that are non-alphanumeric nor one of the characters @ * - + . / are encoded. The escape() function is often used to encode cookie values before they're stored into a cookie (such as a URL).

Note that escape() has been deprecated in ECMAScript 3.0 in favor of encodeURI(). The later is supported in JavaScript 1.5 and up, while the former indefinitely (though formally deprecated).

Example:

escape("JavaScript Kit") //returns "JavaScript%20Kit"

unescape(s) Unescapes an encoded string "s" previously encoded by escape().

Note that escape() has been deprecated in ECMAScript 3.0 in favor of decodeURI(). The later is supported in JavaScript 1.5 and up, while the former indefinitely (though formally deprecated).

alert(), confirm() and prompt()

These three methods of the Window object are often confused as top-level functions (since the "window" prefix is typically omitted when using them), so we'll listing them here just for your convenience. You can also see an explanation of alert(), confirm() and prompt() on the Windows object page.

methods Description
alert(msg) Displays an Alert dialog box with the desired message and OK button.
confirm(msg) Displays a Confirm dialog box with the specified message and OK and Cancel buttons. Example(s)
prompt(msg, [input]) Displays a Prompt dialog box with a message. Optional "input" argument allows you to specify the default input (response) that gets entered into the dialog box. Set "input" to "" to create a blank input field. Example(s)

Examples

confirm(msg)

var yourstate=window.confirm("Are you sure you are ok?")
if (yourstate) //Boolean variable. Sets to true if user pressed "OK" versus "Cancel."
window.alert("Good!")

prompt(msg, [input])

var thename=window.prompt("please enter your name")
window.alert(thename)



Bookmark and Share
Registered® Trademark™ and Copyright© 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved