Java Script Quick Tutorial
7. Tips & Tricks

Summary :

1. Simple errors
2. Concatenation error
3. Text after the JavaScript code is ignored
4. Don't redefine a form


1. Simple errors
 

Forgetting "" for a string :

Copy of the javascript code :

document.write ( "Hello world <br>" );
document.write ( Hello world again <br> );

Result :


2. Concatenation error
 

The error is the minus 1 (-1) :

Copy of the javascript code :

document.write ( "Last link number = " + document.links.length - 1 );

Result :


Note : NaN means Not a Number

With parenthesis, it's ok :

Copy of the javascript code :

document.write ( "Last link number = " + (document.links.length - 1) );

Result :

Without the minus 1 ( -1 ), it's ok :

Copy of the javascript code :

document.write ( "Last link number = " + document.links.length );

Result :

With the calculation before, it's ok :

Copy of the javascript code :

document.write ( document.links.length -1 + " is the last link number" );

Result :

With 2 steps, it's ok :

Copy of the javascript code :

document.write ( "Last link number = " );
document.write ( document.links.length - 1 );

Result :


3. Text after the JavaScript code is ignored
 

Links list here :

Yahoo.com Altavista.com

Copy of the javascript code :

// Some information :
document.write ( "<b>" + "document.links.length = " + document.links.length + "<br></b>" );

// Write links.length.
document.write ( "document.links [ 0 ==> " + (document.links.length-1) + " ] :<br>" );

//----- Loop and write all links.
for (var i = 0; i < document.links.length; i++)
document.write ( "link [ " + i + " ] = " + document.links[i] + "<br>" );

Result :

Links list here ( 2 more links ) :

Hotmail.com Mailcity.com

Copy of the javascript code :

// Some information :
document.write ( "<b>" + "document.links.length = " + document.links.length + "<br></b>" );

// Write links.length.
document.write ( "document.links [ 0 ==> " + (document.links.length-1) + " ] :<br>" );

//----- Loop and write all links.
for (var i = 0; i < document.links.length; i++)
document.write ( "link [ " + i + " ] = " + document.links[i] + "<br>" );

Result :


4. Don't redefine a form
 

Form1 definition

This is Form1 

'Form1' general information ( working ) :

Copy of the javascript code :

document.write ( "Form1.action = " + Form1.action + "<br>" );
document.write ( "Form1.elements.count = " + Form1.elements.count + "<br>" );
document.write ( "Form1.elements.length = " + Form1.elements.length + "<br>" );
document.write ( "Form1.encoding = " + Form1.encoding + "<br>" );
document.write ( "Form1.method = " + Form1.method + "<br>" );
document.write ( "Form1.target = " + Form1.target + "<br>" );
document.write ( "Form1.elements[0].name = " + Form1.elements[0].name + "<br>" );

Result :

When ouputting the property Form1.outerHTML, Form1 is going to be redefined :

Copy of the javascript code :

document.write ( "Form1.outerHTML = " );
document.write ( Form1.outerHTML );
document.write ( "<br>");

Result :

'Form1' general information ( Not working )

( don't work here, because Form1 has been redefined when writing Form1.outerHTML ) :

Copy of the javascript code :

document.write ( "Form1.action = " + Form1.action + "<br>" );
document.write ( "Form1.elements.count = " + Form1.elements.count + "<br>" );
document.write ( "Form1.elements.length = " + Form1.elements.length + "<br>" );
document.write ( "Form1.encoding = " + Form1.encoding + "<br>" );
document.write ( "Form1.method = " + Form1.method + "<br>" );
document.write ( "Form1.target = " + Form1.target + "<br>" );
document.write ( "Form1.elements[0].name = " + Form1.elements[0].name + "<br>" );

Result :


 

Hosted by www.Geocities.ws

1