JavaScript stuff

Posted on | November 11, 2011

Javascript is a compact, object-based scripting language. It can provide interactive web pages, validate form data, and make your web page clearer.

I. Basic Javascript

The JavaScript code is embeded into the HTML document. Simply place the JavaScript statements within the SCRIPT tag.


JavaScript code goes here…


You can optionally specify the LANGUAGE attribute in the SCRIPT tag.


JavaScript code goes here…


For older browsers which do not understand JavaScript, you need to hide thescript with the comment tags.


<!– hide JavaScript from older browsersJavaScript code goes here…// end hiding from older browsers –>


If you wish to define functions, they should be defined in the HEAD part of the HTML document. This is because the HEAD is the first part of the document which is loaded, and that ensures that your functions will be available for your JavaScript code.

A complete HTML example document:




<!– hide JavaScript from older browsers function bar() {

document.write(“Hi there.”)

}// end hiding from older browsers –>




HTML document goes here…



And last, but certainly not least, you may put JavaScript statements within HTML tags. For example, an event handler is placed directly in the tag:

<input type=”button” onClick=”foo()”>.

II. Advanced JavaScript

A.    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.

1. 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);

2. parseFloat

parseFloat()  is the same as parseInt() except that it does floating point numbers and it does not take a radix argument. It canonly do base-10 math.

Example:   floating_value = parseFloat(“-5.234E+2″);

3. 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.”);



{alert(“That’s a valid value.”);


B.     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”));

C.   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 /pre>

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[].

1. 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”>


This will cause the file some_javascript_file.js to be inserted at this point.

2. focus

For some objects, you can force the focus to appear at certain places. Some objects include frames, text areas, password fields, etc. It looks like this:


This will force the focus to appear on “my_frame”.

You can also remove the focus with blur().

  • Google Ads

  • Ads by Google