![]() | |
Advanced JavaScript Stuff(This stuff might be useful someday.) parseInt, parseFloat, isNaNSometimes 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( 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
floating_value = parseFloat("-5.234E+2");
isNaN
if( isNaN( parseFloat(some_string)) {
alert("That was an invalid value.");
}
else {
alert("That's a valid value.");
}
evalThe 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 FunctionsSometimes 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:
srcYou 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. focusFor 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
|