Thursday, June 28, 2012

Java Script Tutorial 20 : JavaScript Objects


JavaScript is an Object Based Programming language, and allows you to define your own objects and make your own variable types. 
An object is just a special kind of data. An object has properties and methods. We can create our own objects in java script, which will be discussed in future tutorials. 


Properties :
Properties are the values associated with an object. For example


<script type="text/javascript">
     var txt="This is text.";
     document.write(txt.length);
</script>


In above code, txt is an object and length is its property. Result displayed in browser will be : 13


Methods :
Methods are the actions that can be performed on objects. For example


<script type="text/javascript">
     var txt="This is Text";
     document.write(txt.toUpperCase());
</script>


In above code txt is an object and toUpperCase() is method.  Result displayed in browser will be : 
THIS IS TEXT


Tuesday, June 26, 2012

Java Script Tutorial 19 : JavaScript Special Characters


In JavaScript, Special characters can be added to a text string by using the backslash sign. For example we want to display following text in JavaScript code. 


var txt="This "This is a "big" Apple.";


In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: 


This is a  


We must place a backslash (\) before each double quote in "big" to solve this problem. This result string will look like this :


var txt="This "This is a \"big\" Apple.";


After executing in browser, it will display string as : 


This is a "big" Apple. 


There are many other special characters that can be added to a text string with the backslash sign, these are :


\'          (  single quote  )
\"         (  double quote  )
\\          (  backslash  )
\n         (  new line  )
\r          (  carriage return  )
\t          (  tab  )
\b         (  backspace)
\f          (  form feed  )

Java Script Tutorial 18 : JavaScript Throw


JavaScript throw statement allows you to create an exception. If you use this statement together with the try .. catch statement, you can control program flow and generate error messages. The exception can be a string, integer, Boolean or an object. Syntax of throw is as:


throw exception


For example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 2 and 5:","");
try

       if(x>5)
         {
             throw "Error 1";
         }
       else if(x<2)
         {
             throw "Error 2";
         }
 }
 catch(err)
 {
       if(err=="Error 1")
         {
              document.write("The value is higher then 5.");
         }
       if(err=="Error 2")
         {
              document.write("The value is lower then 2.");
         }  
 }
</script>
</body>
</html>


Java Script Tutorial 17 : JavaScript Try .. Catch

JavaScript try .. catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs. Syntax of JS try .. catch is :


try
  {
       // code
  }
catch()
  {
       // Handle errors
  }


For example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var txt="";
function Showmessage()
{
     try
       {
            adddlert("JS try .. catch testing ....");
       }
     catch(err)
       {
            txt="There was an error on this page.";
            alert(txt);
       }
}
</script>
</head>
<body>
   <input type="button" value="Click to View message" onclick="Showmessage()" />
</body>
</html>

Friday, June 22, 2012

Java Script Tutorial 16 : JavaScript Events

Events are actions that can be detected by JavaScript. Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags. Following are some events we use in javascript.
  • A mouse click
  • A web page loading
  • Mousing over
  • Selecting an input field
  • Submitting an HTML form
  • A keystroke
Details of some some events are below.


onLoad and onUnload :
The onLoad and onUnload events are triggered when the user enters or leaves the page. The onLoad event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.


Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page.


onFocus, onBlur and onChange :
The onFocus, onBlur and onChange events are often used in combination with validation of form fields. In following example the validateEmail() function will be called whenever the user changes the content of the field.


<input type="text" size="30" id="email" onchange="validateEmail()" />


onSubmit :
The onSubmit event is used to validate ALL form fields before submitting it. In following example, the validateForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled. The function validateForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled.


<form method="post" action="mypage.htm" onsubmit="return validateForm()">


onMouseOver :
The onmouseover event can be used to trigger/call a function when the user mouses over an HTML element.