The JavaScript Built-in Functions


The last topic I'll take a look at in our coverage of JavaScript functions gives us a look at the functions that are already built in to JavaScript, and are available anywhere in your code. (In fact, these built-in functions are sometimes called global functions.) Table 3.15 lists these functions.

Table 3.15. JavaScript's Global Functions

Function

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

decodeURI

     

x

       

x

x

   

Decodes a uniform resource identifier (URI). URIs are encoded for use with browsersfor example, a space is encoded as a hex value, %20. This function decodes encoded URIs, substituting, for example, spaces for %20.

   

Syntax: decodeURI("URI")

   

Returns: String

decodeURIComponent

       

x

     

x

x

   

Decodes a URI component. The same as decodeURI , except that this function works on components , which are parts of complete URIs.

   

Syntax: decodeURI("URIComponent")

   

Returns: String

encodeURI

     

x

       

x

x

   

Encodes a URI for use in a browser, converting, for example, spaces into their hex equivalent, %20. More recent than the escape function, does not encode these characters; / : @ & = + $ , - _ . ! ~ * ' ( ) #because these characters are now considered legal in URIs.

   

Syntax: encodeURI("Text")

   

Returns: String

encodeURIComponent

     

x

       

x

x

   

Encodes a URI component for use in a browser. The same as encodeURI , except that this function works on URI components, which are parts of complete URIs.

   

Syntax: encodeURI("Text")

   

Returns: String

escape

x

x

x

x

x

x

x

x

x

x

   

"Escapes" a string into a URI that can be used in a browser. This function converts nearly all nonalphanumeric characters to their escaped version. (For example, spaces are converted to hex %20.) Performs more escaping than the more modern encodeURI .

   

Syntax: escape("Text")

   

Returns: String

eval

x

x

x

x

x

x

x

x

x

x

   

Evaluates a JavaScript expression. See below for more information.

   

Syntax: eval("string")

   

Returns: Object reference

isFinite

x

x

x

x

x

x

x

x

x

x

   

Checks whether an expression yields a number outside the maximum or minimum possible values JavaScript can store. Returns false if the expression's value is outside the maximum or minimum possible values.

   

Syntax: isFinite(number)

   

Returns: Boolean

isNaN

x

x

x

x

x

x

x

x

x

x

   

Checks whether a value is a valid number ( isNaN stands for Is Not a Number ) and returns true if the value passed to it is not a number. Useful for checking the results of parseInt and parseFloat .

   

Syntax: isNaN(value)

   

Returns: Boolean

Number

   

x

x

   

x

x

x

x

   

Converts a string to an integer or a floating-point number, as appropriate.

   

Syntax: Number("string")

   

Returns: Number

parseFloat

x

x

x

x

x

x

x

x

x

x

   

Converts a string to a floating-point number. Syntax: parseFloat("string")

   

Returns: Number

parseInt

x

x

x

x

x

x

x

x

x

x

   

Converts a string to an integer. Can also take a base (also called a radix) to use when convertingfor example, parseInt(myString, 16) creates a hexadecimal (base 16) value.

   

Syntax: parseInt("string" [, radix])

   

Returns: Number

toString

x

x

x

x

x

x

x

x

x

x

   

This is a method of every JavaScript and DOM object, and is intended to convert the object into a string representation as meaningfully as it can. Can also take a radix if needed.

   

Syntax: toString("string" [, radix])

   

Returns: String

unescape

x

x

x

x

x

x

x

x

x

x

   

Converts an escaped URI into its unescaped version, the counter-part of the escape function.

   

Syntax: unescape("URI")

   

Returns: String

unwatch

   

x

x

           
   

Directs an external debugger not to watch a particular property anymore.

   

Syntax: unwatch(property)

   

Returns: Nothing

watch

   

x

x

           
   

Directs an external debugger to watch the value of a particular property.

   

Syntax: watch(property, debugHandler)

   

Returns: Nothing

One of the functions in Table 3.15 is particularly powerful eval , which enables you to evaluate JavaScript code. I'll take a closer look at this function here, with an example that enables you to type in JavaScript expressions and evaluate them on-the-fly .

Tip

Also see the execScript method in Chapter 8, "Using window and frame Methods and Events."


Here's the code for that examplein this case, I'm reading the JavaScript expression the user has entered into a text field, evaluating it with eval , and displaying the result in another text field. We're anticipating our work with HTML text fields hereyou might note in passing how this code refers to the text in a text field, which is stored in its HTML value propertyas document.forms[0]. name .value , where forms is a document object array that holds all the forms in the page, and name is the name given to the text field:

(Listing 03-17.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the eval Function          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--  function evaluator()   {   document.forms[0].result.value   = eval(document.forms[0].expression.value)   }  // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Using the eval Function</H1>          <FORM>  <INPUT TYPE="TEXT" NAME="expression">   <INPUT TYPE="BUTTON" ONCLICK="evaluator()" VALUE="  =  ">   <INPUT TYPE="TEXT" NAME="result">  </FORM>      </BODY>  </HTML> 

You can see the results in Figure 3.15, where I've typed in a JavaScript expression and clicked the = button, which makes the result appear in the second text field.

Figure 3.15. Using eval to evaluate a JavaScript expression.

graphics/03fig15.gif

Tip

This example, which evaluates JavaScript expressions on-the-fly, can even be a useful tool as you develop your own code and want to check some JavaScript before putting it into your web page.


One use for the eval function is to get access to objects whose names you already knowfor example, an eval statement can return a reference to the document object if you pass it the string "document" . What use is that? Well, the two major browsers have different object models, and if you try to execute a statement that uses an object that doesn't exist in the current browser, your script will crash. (Just having such statements in a script doesn't cause a problemit's only if you try to execute those specific statements that your script will crash.) To avoid that, you can check which browser you're working with, and then use eval to get a set of objects legal for that browser by passing eval their names. Here's a simple example that gets a reference to the document object by passing the text "document" to eval (this is just an exampleboth browsers support a document object, of course):

(Listing 03-18.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the eval Function          </TITLE>      </HEAD>      <BODY>          <H1>Using the eval Function</H1>          <SCRIPT LANGUAGE="JavaScript">              <!--  var obj = "document"   for (var property in eval(obj)) {   document.write(property + ": " + document[property] + "<BR>")   }  // -->          </SCRIPT>      </BODY>  </HTML> 


Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net