The Global object is the parent of all globally avaliable properties and methods.
Decodes a Uniform Resource Identifier (URI) previously created by encodeURI or by a similar routine.
|
Show Details |
5.5+ |
1.0+ |
6.0+ |
7.0+ |
no |
Parameters
String |
encodedURI |
A complete, encoded Uniform Resource Identifier. |
Returns
- Remarks
-
Replaces each escape sequence in the encoded URI with the character that it represents.
Does not decode escape sequences that could not have been introduced by encodeURI .
- See Also
-
decodeURIComponent|encodeURI|encodeURIComponent
- Availability
-
JavaScript 1.5|JScript 5.5|ECMAScript v3
|
Decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.
|
Show Details |
5.5+ |
1.0+ |
6.0+ |
7.0+ |
no |
Parameters
String |
encodedURI |
An encoded component of a URI. |
Returns
- Remarks
- Replaces each escape sequence in the encoded URI component with the character that it represents.
- See Also
-
decodeURI|encodeURI|encodeURIComponent
- Availability
-
JavaScript 1.5|JScript 5.5|ECMAScript v3
|
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape
sequences representing the UTF-8 encoding of the character.
|
Show Details |
5.5+ |
1.0+ |
6.0+ |
7.0+ |
no |
Parameters
String |
URI |
A complete Uniform Resource Identifier. |
Returns
- Remarks
-
Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI.
encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:
Reserved characters |
; , / : @ & = + $ |
Unescaped characters |
alphabetic, decimal digits, - _ . ! ~ * ' ( ) |
Score |
# |
Note that encodeURI by itself cannot form proper HTTP GET and POST requests, such as for XMLHTTPRequests,
because "&", "+", and "=" are not encoded, which are treated as special characters in GET and POST requests. encodeURIComponent ,
however, does encode these characters. These behaviors are most likely not consistent across browsers.
- See Also
-
decodeURI|decodeURIComponent|encodeURIComponent
- Availability
-
JavaScript 1.5|JScript 5.5|ECMAScript v3
|
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three
escape sequences representing the UTF-8 encoding of the character.
|
Show Details |
5.5+ |
1.0+ |
6.0+ |
7.0+ |
no |
Parameters
String |
String |
A component of a Uniform Resource Identifier. |
Returns
- Remarks
-
encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' (
)
For security reasons, you should call encodeURIComponent on any user-entered parameters that will be passed as part of
a URI. For example, a user could type "Thyme &time=again" for a variable comment . Not using encodeURIComponent
on this variable will give comment=Thyme%20&time=again . Note that the ampersand and the equal sign mark a new
key and value pair. So instead of having a POST comment key equal to "Thyme &time=again", you have
two POST keys, one equal to "Thyme " and another (time ) equal to again. This is super,
super, super dangerous if you are using PHP with register_globals turned on.
- See Also
-
decodeURI|decodeURIComponent|encodeURI
- Availability
-
JavaScript 1.5|JScript 5.5|ECMAScript v3
|
escape( string string) : String
Returns a URI-encoded version of a string.
|
Show Details |
3.0+ |
1.0+ |
2.0+ |
7.0+ |
1.0+ |
Parameters
string |
string |
String to be URI-encoded. |
Returns
- Remarks
- escape has been phased out in favor of encodeURIComponent.
- See Also
-
unescape|encodeURIComponent
- Availability
-
JavaScript 1.0|JScript 1.0
|
eval( String string) : Boolean|Number|Object|String
Evaluates a string of JavaScript code without reference to a particular object.
|
Show Details |
3.0+ |
1.0+ |
2.0+ |
7.0+ |
1.0+ |
Parameters
String |
string |
A string representing a JavaScript expression, statement, or sequence of statements. string can include variables and properties
of existing objects.
|
Returns
Boolean |
|
Number |
|
Object |
|
String |
|
-
The following examples display output using document.write . In server-side JavaScript, you can display
the same output by calling the write function instead of using document.write .
Example: Using eval
In the following code, both of the statements containing eval return 42. The first evaluates the string "x
+ y + 1 "; the second evaluates the string "42 ".
var x = 2;
var y = 39;
var z = "42";
eval("x + y + 1"); // returns 42
eval(z); // returns 42
Example: Using eval to evaluate a string of JavaScript statements
The following example uses eval to evaluate the string str . This string consists of JavaScript
statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0
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("z is ", eval(str));
- Remarks
-
eval is a top-level function and is not associated with any object.
The argument of the eval function is a string. If the string represents an expression, eval
evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements.
Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
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.
If the argument of eval is not a string, eval returns the argument unchanged. In the following
example, the String constructor is specified, and eval returns a String object rather
than evaluating the string.
eval(new String("2+2")); // returns a String object containing "2+2"
eval("2+2"); // returns 4
You cannot indirectly use the eval function by invoking it via a name other than eval ; if you
do, a runtime error might occur. For example, you should not use the following code:
var x = 2;
var y = 4;
var myEval = eval;
myEval("x + y");
You should not use eval to convert property names into properties. For example, consider the following example.
The getFieldName(n) function returns the name of the specified 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"));
However eval is not necessary here. In fact, its use here is discouraged. Instead, use the member operators,
which are much faster:
var field = getFieldName(3);
document.write("The field named ", field, " has value of ",
field[value]);
Backward Compatibility
JavaScript 1.3 and earlier
You can use eval indirectly, although it is discouraged.
JavaScript 1.1
eval is also a method of all objects.
- Availability
-
JavaScript 1.0|JScript 1.0|ECMAScript v1
|
Evaluates whether an argument is a finite number.
|
Show Details |
4.0+ |
1.0+ |
4.06+ |
7.0+ |
1.0+ |
Parameters
Number |
number |
Number to be evaluated. |
Returns
-
Example: Using isFinite
You can check a client input to determine whether it is a finite number.
if (isFinite(ClientInput)) {
/* take specific steps */
}
- Remarks
-
isFinite is a top-level function and is not associated with any object.
You can use this method to determine whether a number is a finite number. The isFinite method examines
the number in its argument. If the argument is NaN , positive infinity or negative infinity, this method returns
false , otherwise it returns true .
- See Also
-
Number.NEGATIVE_INFINITY|Number.POSITIVE_INFINITY
- Availability
-
JavaScript 1.3|JScript 3.0|ECMAScript v1
|
isNaN( String value) : Boolean
Evaluates whether an argument is NaN.
|
Show Details |
4.0+ |
1.0+ |
3.0+ |
7.0+ |
1.0+ |
Parameters
String |
value |
The value you want to evaluate. |
Returns
-
Example: Using isNaN
The following example evaluates floatValue to determine if it is a number, and then calls a procedure accordingly:
floatValue = parseFloat(toFloat);
if (isNaN(floatValue)) {
notFloat();
} else {
isFloat();
}
- Remarks
-
isNaN is a top-level function and is not associated with any object.
The parseFloat and parseInt functions return NaN when they evaluate a value
that is not a number. isNaN returns true if passed NaN , and false otherwise.
This function is necessary, because the value NaN cannot be meaningfully tested with the equality operators.
x == NaN and x === NaN are always false, regardless of what x is, even if x
is NaN . For example, both 1 == NaN and NaN == NaN return false .
- See Also
-
Number.NaN|parseFloat|parseInt
- Availability
-
JavaScript 1.1|JScript 3.0|ECMAScript v1
|
Parses a string and returns its value as a number.
|
Show Details |
3.0+ |
1.0+ |
2.0+ |
7.0+ |
1.0+ |
Parameters
String |
string |
A string that represents the value you want to parse. |
Returns
-
Example: parseFloat returning a number
The following examples all return 3.14:
parseFloat("3.14");
parseFloat("314e-2");
parseFloat("0.0314E+2");
var x = "3.14";
parseFloat(x);
parseFloat("3.14more non-digit characters");
Example: parseFloat returning NaN
The following example returns NaN :
- Remarks
-
parseFloat is a top-level function and is not associated with any object.
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, it returns the value up to that point and ignores
that character and all succeeding characters. Leading and trailing spaces are allowed.
If the first character cannot be converted to a number, parseFloat returns NaN .
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 .
- See Also
-
isNaN|parseInt
- Availability
-
JavaScript 1.0|JScript 1.0|ECMAScript v1
|
parseInt( String string, [ Number radix]) : Number
Parses a string argument and returns an integer of the specified radix or base.
|
Show Details |
3.0+ |
1.0+ |
2.0+ |
7.0+ |
1.0+ |
Parameters
String |
string |
A string that represents the value you want to parse. |
Number |
radix |
(optional)An integer that represents the radix of the above mentioned string.
|
Returns
-
Example: Using parseInt
The 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);
parseInt("12", 13);
The following examples all return NaN :
parseInt("Hello", 8); // Not a number at all
parseInt("0x7", 10); // Not in base 10 format
parseInt("546", 2); // Digits are not valid for binary representations
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");
- Remarks
-
parseInt is a top-level function and is not associated with any object.
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. Leading and trailing spaces are allowed.
If the radix is not specified or is specified as 0, JavaScript assumes the following:
- If the input
string begins with "0x", the radix is 16 (hexadecimal).
- If the input
string begins with "0", the radix is eight (octal). This feature is deprecated.
- If the input
string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns NaN .
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 .
- See Also
-
isNaN|parseFloat|Object.valueOf
- Availability
-
JavaScript 1.0|JScript 1.0|ECMAScript v1
|
Returns a URI-decoded version of a string.
|
Show Details |
3.0+ |
1.0+ |
2.0+ |
7.0+ |
1.0+ |
Parameters
String |
string |
URI-encoded string to be decoded. |
Returns
- Remarks
- unescape has been phased out in favor of decodeURIComponent.
- See Also
-
escape|decodeURIComponent
- Availability
-
JavaScript 1.0|JScript 1.0
|