Vous êtes sur la page 1sur 26

JAVA SCRIPTS OBJECTS (CONTD)

JavaScript Number Object

The Number object is an object wrapper for primitive numeric values. Number objects are created with new Number(). Syntax: var num = new Number(value); Note: If the value parameter cannot be converted into a number, it returns NaN (Nota-Number). Example :
Return the largest number possible in JavaScript, by using Number.MAX_VALUE

Example :
Convert

a number into an exponential notation: var num = 5.56789; var n=num.toExponential(3) The output of the code above will be: 5.568e+0

JavaScript Global

The JavaScript global properties and functions can be used with all the built-in JavaScript objects. Infininity Property:
Infinity is a numeric value that represents positive infinity. -Infinity is a numeric value that represents negative infinity. Infinity is displayed when a number exceeds the upper limit of the floating point numbers, which is 1.7976931348623157E+10308. -Infinity is displayed when a number exceeds the lower limit of the floating point numbers, which is -1.7976931348623157E+10308.

<script type="text/javascript> var Month=13; if (Month < 1 || Month > 12) { Month = Number.NaN; } document.write(Month); </script> <script type="text/javascript> var x=1.7976931348623157E+10308; document.write(x + "<br />"); var y=-1.7976931348623157E+10308; document.write(y); </script>

Escape function
The escape() function encodes a string. This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters. This function encodes special characters, with the exception of: *@-_+./ Ex: <script type="text/javascript> document.write(escape("Need tips? Visit W3Schools!")); </script> The output of the code above will be: Need%20tips%3F%20Visit%20W3Schools%21

<script type="text/javascript> var str="Need tips? Visit W3Schools!"; var str_esc=escape(str); document.write(str_esc + "<br />") document.write(unescape(str_esc)) </script> Output: Need%20tips%3F%20Visit%20W3Schools%21 Need tips? Visit W3Schools!

<script type="text/javascript> eval("x=10;y=20;document.write(x*y)"); document.write("<br />" + eval("2+2")); document.write("<br />" + eval(x+17)); </script> Output:
200 4 27

parseFloat()

The parseFloat() function parses a string and returns a floating point number. This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string. Note: Only the first number in the string is returned! Note: Leading and trailing spaces are allowed. Note: If the first character cannot be converted to a number, parseFloat() returns NaN

<script type="text/javascript> document.write(parseFloat("10") + "<br />"); document.write(parseFloat("10.33") + "<br />"); document.write(parseFloat("34 45 66") + "<br />"); document.write(parseFloat(" 60 ") + "<br />"); document.write(parseFloat("40 years") + "<br />"); document.write(parseFloat("He was 40") + "<br />"); </script> Output: 10 10.33 34 60 40 NaN

The Window Object

The window object represents an open window in a browser. If a document contain frames (<frame> or <iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame. Next Slide Shows window object properties:

Functions of window object

Document Object

Each HTML document loaded into a browser window becomes a Document object. The Document object provides access to all HTML elements in a page, from within a script. Tip: The Document object is also part of the Window object, and can be accessed through the window.document property.

The History Object

The history object contains the URLs visited by the user (within a browser window). The history object is part of the window object and is accessed through the window.history property. Property: length Returns the number of URLs in the history list.

<html> <head> <script type="text/javascript"> function goBack() { window.history.back() //history.go(-1). } function goForward() { window.history.forward() //history.go(1) } </script> </head> <body> <input type="button" value="Back" onclick="goBack()" /> <input type="button" value="Forward" onclick="goForward()" /> </body> </html>

The Location Object

The location object contains information about the current URL. The location object is part of the window object and is accessed through the window.location property.

Example: Return the entire URL (of the current page): <script type="text/javascript> document.write(location.href); </script> Output : http://w3schools.com/jsref/prop_loc_href.asp

Example : Return the query portion of a URL. Assume that the current URL is http://www.example.com/submit.htm?email=someone@exampl e.com:

<script type="text/javascript"> document.write(location.search); </script> The output of the code above will be: ?email=someone@example.com

Example :Load a new document


<html> <head> <script type="text/javascript"> function newDoc() { window.location.assign("http://www.w3schools.com") } </script> </head> <body> <input type="button" value="Load new document" onclick="newDoc()" /> </body> </html>

Vous aimerez peut-être aussi