Thursday, 28 February 2013

JavaScript : Number function, String Function, encodeURI(), decodeURI, encodeURIComponent and decodeURIComponent functions


JavaScript : Number function

Description

The number function is used to converts a specified object to a number.

Syntax

Number(obj)

Parameters

obj : Required. An object.
Note : If the object is a Date object the function returns a value in milliseconds calculated from 01 January, 1970 UTC (GMT). The return value is positive if the given date is after the said date and negative if before the said date.
If the object is a string and if there is no well-formed numeric literal the function returns NaN.

Example of Number() function

The following example demonstrate how to use Number() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript: Number function.</title>
</head>
<body>
<h1 style="color: red">JavaScript Number function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var str1 = "4400.23";
var num1 = Number(str1);
document.write('"4400.23" converted to : '+num1);
//]]>
</script>
</body>
</html>



JavaScript : String function

Description

The String function is used to converts a specified object to a string. String is a top-level function and is not associated with any object.

Syntax

String(obj)

Parameters

obj : Required. An object.

Example of String function

The following web document converts the Date object to a string.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript String function.</title>
</head>
<body>
<h1 style="color: red">JavaScript String function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var dt = new Date (4400346634522);
var str1 = String(dt);
document.write('4400346634522 converted to : '+str1);
//]]>
</script>
</body>
</html>

JavaScript : encodeURI() function

encodeURI function

The encodeURI is used to encode 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.

Syntax

encodeURI(uri)

Parameters

uri : A complete Uniform Resource Identifier.
The encodeURI function replaces all characters except the following with the appropriate UTF-8 escape sequences:
TypeCharacters include
Reserved characters; , / ? : @ & = + $
Unescaped charactersalphabetic, decimal digits, - _ . ! ~ * ' ( )
Score#

Example of encodeURI() function

The following web document encode a specified string using encodeURI()function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript encodeURI function.</title>
</head>
<body>
<h1 style="color: red">JavaScript encodeURI function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
str1 = "http://www.w3resource.com/javascript tutorial.html"
document.write(str1+ '<br />')
str2 = encodeURI("http://www.w3resource.com/javascript tutorial.html")
document.write("After using encodeURI function : "+str2)
//]]>
</script>
</body>
</html>



JavaScript : decodeURI function

Description

The decodeURI is used to decode a Uniform Resource Identifier (URI), previously created by encodeURI or by a similar routine.

Syntax

decodeURI(encodedURI)

Parameters

encodedURI : A complete, encoded Uniform Resource Identifier.

Example of decodeURI Functions

The following web document decode a specified string using decodeURI()function. The string was previously encoded by encodeURI() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript decodeURI function.</title>
</head>
<body>
<h1 style="color: red">JavaScript decodeURI function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
str1 = "http://www.w3resource.com/javascript%20tutorial.html "
document.write(str1+ '<br />')
str2 = decodeURI("http://www.w3resource.com/javascript tutorial.html")
document.write("After using decodeURI function : "+str2)
//]]>
</script>
</body>
</html>



JavaScript : encodeURIComponent functions

Description

The encodeURIComponent function is used to 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.

encodeURIComponent escapes all characters except the following:
alphabetic, decimal digits, - _ . ! ~ * ' ( )

Syntax

encodeURIComponent(str1)

Parameters

str1 : A complete, encoded Uniform Resource Identifier.

Example encodeURIComponent Functions

In the following web document encodeURIComponent() function is used to encode a URL parameter.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript encodeURIComponent function.</title>
</head>
<body>
<h1 style="color: red">JavaScript encodeURIComponent() function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
xyz = encodeURIComponent("http://myw3r.com/x=31&y=21")
//Lets create an URL
url1 = "http://w3resource.com/xyz=" + xyz + "&xyz3=45";
//Here is the complete URL
document.write(url1);
//]]>
</script>
</body>
</html>



JavaScript : decodeURIComponent functions

Description

The decodeURIComponent function is used to decode a Uniform Resource Identifier (URI) component previously created by encodeURIComponent.

Syntax

decodeURIComponent(encodedURI)

Parameters

encodedURI : An encoded component of a Uniform Resource Identifier.

Example of decodeURIComponent() function

In the following web document decodeURIComponent() function is used to decode a Uniform Resource Identifier (URI) component previously created byencodeURIComponent.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript decodeURIComponent function.</title>
</head>
<body>
<h1 style="color: red">JavaScript decodeURIComponent() function example</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
url1 = "http://w3resource.com/xyz=http%3A%2F%2Fmyw3r.com%2Fx%3D31%26y%3D21&xyz3=45"
//Lets decode url1
document.write(decodeURIComponent(url1));
//]]>
</script>
</body>
</html>



No comments:

Post a Comment