Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Basic Operations in JavaScript

This article will show you some simple javascript functions with their uses. This is recommended for those beginning with javascript.

Javascript is the most essential part in any web application today.This is because it can help to perform various operation on client side itself so that the request need not move to server and your web application responds faster.It may be utilized for variety of things.

This article tells you about simple and basic javascript operations which you need to know before implementing it in your own website.

Here we are going to learn following :

  1. Making simple functions and calling them
  2. List of events
  3. Accessing a field value
  4. Moving to particular location using javascript
  5. Date operationsUsing try-catch to handle exceptions

First we will learn how we may validate a field in a html form:

Listing 1: Making simple functions

<html>
<head>
<title>csanuragjain</title>
<script type="text/javascript">
function fun()
{
alert("Using javascript is simple");
}
</script>
</head>
<body onload="fun()">
</body>
</html>

Here:

  1. We start the javascript using script tag.We set type as "text/javascript" which tells browser that we are dealing with javascript
  2. We make a unction by first writing keyword function and then we write function name which can be any name.Here in our case we took name as fun
  3. We used a javascript function called alert.The use of alert is to display message on browser using a dialog box.The message displayed is “Using javascript is simple “
  4. Remember to use javascript inside head section
  5. Now in body we called the function fun().The function is called when the body loads.This is ensured by calling the function on an event onload

There are various events in javascript.We will discuss the most important ones here:

  1. onclick : If applied on a button then this event get activated when that button is clicked
  2. onmousemove : This event take place when the mouse moves.
  3. onkeypress: This event take place when a key is pressed.
  4. onload : This event take place when an object is loaded
  5. onblur : This event take place when an object lose focus.Like you may apply it on textbox
  6. onsubmit : This event take place when for is submitted

Listing 2: Access field values

<html>
<head>
<title>csanuragjain</title>
<script type="text/javascript">
function reveal()
{
alert(document.getElementById("revealme").value);
}
</script>
</head>
<body>
<input type="password" value="" id="revealme"/>
<input type="submit"  onclick="reveal()" value="submit"/>
</body>
</html>

Here:

  1. We made a function called reveal.
  2. This function uses document.getElementById(“revealme”).We used getElementById and pass “revealme” as argument.It tells that document has to use the object having id as revealme(this will be clear when we explain the body part).We now used the value function to determine the value of that field
  3. Now we write the body part.We will make two fields
  4. The first field is a password field which is a text field to store password.We gave id as revealme so that the javascript know that it has to extract value from this field as explained in previous point
  5. Now we make a submit button and use the onsubmit event to call the reveal method.This ensures that reveal method is called when the submit button is clicked

Now we will learn how to move to particular location using javascript:

Listing 3: Moving to particular location

<script type="text/javascript">
location.href="http://www.google.com";
</script>

Here:

  1. We used location.href which help us to move to a particular location.Here we used it to move to google

Now we learn how to obtain date information.

Listing 4 : Date information

<script type="text/javascript">
var today = new Date();
alert(today);
</script>

Here

  1. First we make a date object.We make a variable call today and use new Date() to make a variable containing the date.
  2. Now we make use of alert to display the date

Now we will learn exception handling.This is very useful when you want the application to run even if a certain piece of code fails to run properly.This is done making use of try-catch statement

Listing 5: Exception handling

<script type="text/javascript">
try
  {
alertme(“hi”);
  }
catch(err)
  {
  alert(err.message);
  }

Here, we have used the try-catch statement.We have added a alertme. Now since alertme function does not exist so an exception occurs and the control now goes to catch statement and a message is displayed with the error which occurred.

This is all for this article.See you next time with some more exciting article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login