Categories:

JavaScript Kit > JavaScript Reference > Here

Global functions

Last updated: May 27th, 2008

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
encodeURI() new Used to encode the full URI, not including any parameters added to it , for special characters. For example, it should be used to encode:

http://javascriptkit.com/illegal space.htm

but NOT

http://javascriptkit.com/illegal space.htm?name=John&age=18

This is because encodeURI(), in order to still ensure a valid URI after encoding, does not encode certain special characters such as "#", ":", "+", "-". or "&", characters that should be encoded if they are part of the URL parameter, such as in "?name=John&age=18". To encode the parameter portion, use encodeURIComponent() instead.

encodeURIComponent() new Used to encode the parameter portion of a URI for characters that have special meaning, to separate them from reserved characters such as "&" that act as key/value separators. More inclusive than encodeURI(), it encodes all characters with special meaning in a URL string, including "=" and "&". Use this method only on the parameter portion of a URI; otherwise, the URI may no longer be valid if it contains one of the characters that are part of a valid URI (ie: "+") yet should be escaped if part of the URI parameter.

Example:

var username=document.getElementbyId("username").value
var birthday=document.getElementbyId("borndate").value

var urlquery="process.php?name="+encodeURIComponent(username)+"&birth="+encodeURIComponent(birthday)

decodeURI() new Decodes all special characters within a string that encodeURI() may have encoded.
decodeURIComponent() new Decodes all special characters within a string that decodeURIComponent() may have encoded.
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()

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)


Reference List

Partners
Right column

CopyRight 1998-2012 JavaScript Kit. NO PART may be reproduced without author's permission.