Saturday, June 28, 2008

JS loops :

JS loops :
Loop statements are used to repeat the execution of a set of statements. There are three loop statements available in JavaScript.
1. do... while Statement
2.while statement
3.for statement

do ...while statement

do ... while executes the statements for once even if the condition is wrong because the condition is checked only at the end of the loop .



In above example at the time of onLoad event happens, it will call that color ( ) function.
In color function an input is obtained from the user using the prompt method of window object. The prompt has two arguments. The first argument passed is the message that will appear in the prompt dialog box (in this case “ Enter your favorite color”)and the second argument is the default value that will appear in the text box( gray in this above program ). In this place you can input your color.
Since the while loop condition is x==”” or x== null , if we didn't give any input the prompt will come again because the condition is true

for loop


for loop executes the statements inside the curly brackets until the condition become false.
for (initialization statement ; condition ; update statement )
{
statements
}



In the above example the 'data' variable is incremented by one in every loop and drawn a line by the color the user has entered through the prompt. When the condition (data < x) become false, the control will come out of the 'for loop'.
document.write(“
”) is to draw a line using the color user has given as input.

while loop.

while loop checks the condition before entering into the loop statements.



In the above example , the numbers are being printed starting from 0 to 4 using the while loop. Document is a built in object and write is a method. Any data passed in the write method gets printed in the browser.

break statement :


break statement is used to terminate the execution of a loop and transfer control to the statement following the loop.

count = 0;
for (i = 0; i < 5 ; i++)
{
if ( i==3) break;
count += i;
}
document.write(count);

count will be 3 because when the if clause become true, the break will get executed and control will go to statement following the loop( in this case - > document.write(count); ).

Wednesday, June 25, 2008

Js functions

JS functions:

JavaScript functions will be executed only when the event happens
or by a call to that function. To keep the browser from executing a
script when the page loads, we can define it as a function.




In this external script 'jsfunc.js ' contains only only a function. This script can be called from the main page.
window.alert("welcome to my page"); is for pop up message .




When the onload event happens ( means if we open that page), from body it will automatically call the fun() function. Actually that event has called the function.

Argument passing and return value:

We can pass arguments through functions as we are doing in C programming language. For declaring a variable 'var' keyword and returning a value 'return' keyword also uses. Rewriting the external script 'jsfunc.js' as below





From page, onload event calls the fun() and this function call add() with arguments 2,3 . The add() function return the added value through 'x' variable to 'sum' variable.The document.write function display the value of the variable in the page.

Sunday, June 15, 2008

Js Comments

Comments can be added to explain the javascript or to make javascript more readable.

Single Line Comments:
Second line of the code shows how to comment a line using // symbol.




Multiple line comments:
This code used mutliple line comment method in Javascript.





One more important thing we have to
remember before writing a professional javascript is to use an HTML
comment tag (end
comment) after the last javascript statement. Because browsers that do
not have support for javascript will display javascript as page
content. To prevent this as part of javascript , the html comment tag
can be used to hide javascript.

Saturday, June 7, 2008

What is JavaScript??

What is Javascript ?
Javascript was designed to add interactivity to HTML pages .The script is computer excutable code and usually embed directly to HTML pages. The script will execute without priliminary compilation and the main advantage is everyone can use it without purchasing license.

What is the relation between Java and Javascript ?

Java and JavaScript are entirely different lanaguages in both concept and design. Javascript copies many Java names and naming conventions . It's official name ECMA script because it is following ECMA -262 standard. Javascript is originally developed by Brendan Eich of Netscape under the name Mocha which is later renamed to Livescript and finally to Javascript. It was first introduced and deployed in the Netscape browser version 2.0B3 in December 1995.


Features
  • Structured Programming : Javascript support almost all the structural programming syntax in 'C' ( eg : if ,while,switch ..etc)
  • Dynamic Programming : Dynamic Typing (type assigning) facility is avaliable and it is heavily object-based. Object are associative arrays, that means obj.x = 5 and obj ["x"] = 5 are equivalent.
  • Functional Programming : Function - level programming is tremondously using in javascript.
  • Protype based : Javascript uses prototype instead of classes for defining object properties, including methods and inheritance. It is possible to simulate many class-based features with prototypes in Javascript.
  • Javascript can react to events : events may be from mouse or keyboard .. etc
  • Javascript can read and write HTML elements
  • Javascript can validate data in a form before it is submitting to server
  • Javascript can be used to create cookies