Advanced JavaScript Stuff

(This stuff might be useful someday.)

parseInt, parseFloat, isNaN


Sometimes you want to convert from one type of variable to another. JavaScript is pretty loose in its variable types. However, certain situations require certain variables. There are a few functions available to help you convert from one form to another.
parseInt
parseInt() will convert a string to an integer. The format is:
  parseInt( string [,radix] )
You can optionally specify the radix (or base) of the number. If you do not specify the radix, JavaScript will try to guess. It is recommended that you always specify it so there is no possibility of confusion. If the value can not be converted to an integer, then it returns 0 or NaN depending on which platform you are using. Example:
  the_value = parseInt(some_string,10);
parseFloat
parseFloat() is the same as parseInt() except that it does floating point numbers and it does not take a radix argument. It can only do base-10 math. Oh well, no one's perfect. Example:
  floating_value = parseFloat("-5.234E+2");
isNaN
isNaN() can check to see if a value is not a number. This is useful for the parseInt and parseFloat functions to check if it was not a valid number. It returns true or false corresponding to whether or not it was a valid value. Example:
  if( isNaN( parseFloat(some_string)) {
    alert("That was an invalid value.");
  }
  else {
    alert("That's a valid value.");
  }

eval

The eval() command can execute JavaScript code. That may seem pretty silly, but it can be useful in a few situations. One usefull thing is to give it a string, and it will convert it to a number. Example:
integer_value=eval("7");
interger_value will then be the integer 7. Another example is to execute something a little bit more complicated.
  the_field_name=getFieldName(2);
  alert("The value of the field " + the_field_name + " is: " + eval(the_field_name + ".value"));
The function getFieldName() gets the name of the nth member of a form as a string.

Variable Parameter Functions

Sometimes you don't know how many parameters you wish to pass to a function. The logical solution is to pass an array to the function. Example:
function stimpy( var ) {
  for(var i = 0; i < var.arguments.length; i++)
    document.write(var.arguments[i] + "<br>");
}
The array object has a few properties to make this easy. First, the .arguments.length property to determine the number of elements in the array. Then you can step through the arguments by using the bracket notation .arguments[]. That's it.

src

You can specify external JavaScript code with the src statement. This is useful if you have modular code, or you want to break your code into multiple files. It looks like this:
<script language="JavaScript" src="some_javascript_file.js">
</script>
This will cause the file some_javascript_file.js to be inserted at this point.

focus

For some objects, you can force the focus to appear at certain places. Some objects include frames, textareas, password fields, etc. It looks something like this:
top.my_frame.focus();
This will force the focus to appear on "my_frame".
You can also remove the focus with blur().
Return to the Front Page