Working With JavScript


Int his article i am going to explain what is Javascript. How can we use it in our webpage or projects. What is its importance. What are the common things that we can do with it. How can we display alert, read input,display input. How can we define functions with and without parameters and then call that function in the code.

Introduction
JavaScript allows us to create new things on our website which is both dynamic and interactive, allows us to do things like find out some information about a user , check that forms have been filled in correctly, rotate images, make random text, do calculations and many other things.
Declaring JavaScript
To add a JavaScript we just have to add the following (either between the tags or between the tags)


<script language="JavaScript">
JavaScript
</script>

Creating Alert
This command will help us to create a popup box.

<script>
// Display the alert box
alert('This text is in an alert box');
</script>

Creating Variables
Variables as we all know is used to store information.
Eg:

var my_number = 2326;
var my_name = 'Welcome';

Creating Prompt
It is used to get the information from user.
Eg:

var name = prompt('Please enter your name', 'Your Name');

Displaying Output
It is used to display the output to the user,
Eg:

var greet = 'Welcome ';

// Display prompt
var name = prompt('Please enter your name', 'Your name');

// Create Output Variable
var output = greet + name;

// Display Text
document.writeln('' + output + '

');

Using For loop
As we know for loop is used if we want to do a process n numbe rof times.
Eg.

for(num=0; num < 20; num++)
{
document.writeln(num);
}

Using While Loop

As we know while loops are used for repeating a pieces of code until some the condition is met. For example this code will repeat until the user enters the word 'welcome':

var password = 'welcome';
var input = prompt('Please enter the greet', '');
while(input != greet)
{
var input= prompt('Please enter the greet''');
}

Using if loop
The if function is used to check if something is true or false. For example we can check to see if text entered by a user matches a piece of text we already have (like a greet).

Eg:

var num = prompt("Please enter a number between 1 and 10","");
if(num == 6)
{
alert('Correct! I');
}
else
{
alert('Wrong! ');
}

Using JavaScript Functions
Functions are very important part. They are pieces of JavaScript which can be declared at the top of the page and can then be referred to again and again in our code. We can even pass parameters to them and make them do different things.
Eg:

function Greet()
{
alert(Welcome!);
}

We can call this function by following code:

<a href="#" onMouseOver="Greet();">Welcome

Function with Parameter
Parameters are a very useful part of functions. They allow us to pass data to the function when it is called. Here is an example:

function Greet(what)
{
alert(what);
}

in the head, then:

<a href="#" onMouseOver="Greet(Good_Morning);">GoodMorning
<a href="#" onMouseOver="say(Good_Evening);">GoodEvening


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: