Programming Tutorials on XHTML, CSS, JavaScript, JQuery, JSON, Python, Django, Amazon Web Services, ASP.NET, Web Forms, and SQL
Thursday, June 28, 2012
Java Script Tutorial 22 : JavaScript Array Object
An array is a special variable, which can hold more than one value, at a time. For example, if you have a list of items, storing the country names in single variables could look like as follow :
var country1="Pakistan";
var country2="USA";
var country3="Russia";
An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own ID so that it can be easily accessed. We can defined array in following ways :
var country=new Array();
country[0]="Pakistan";
country[1]="USA";
country[2]="Russia";
or
var country=new Array("Pakistan","USA","Russia");
or
var country=["Pakistan","USA"," Russia"];
You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. For example
document.write(country[1]);
Result of above code will be : USA
To modify a value in an existing array, just add a new value to the array with a specified index number:
country[1]="Germany";
This code change the value USA to Germany inside array object.
document.write(country[0]);
Now It will result as : Germany
Java Script Tutorial 21 : JavaScript Date Object
The JavaScript Date object is used to work with dates and times. Date objects are created with the Date() constructor. We can initiate date by four ways:
- new Date()
- new Date(milliseconds)
- new Date(dateString)
- new Date(year, month, day, hours, minutes, seconds, milliseconds)
We can operate a number of methods after creating a date object. These method allow you to get and set value of year, month, day, hour, minute, second, and milliseconds of the object. All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.
For example
var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)
Set/Manipulate Dates :
We can easily manipulate the date by using the methods available for the Date object. For example
var myDateobj=new Date();
myDateobj.setFullYear(2011,0,15);
Adding days to a date shifts the month or year, the changes are handled automatically by the Date object itself.
Compare Two Dates :
We can also use Date object to compare two dates. For example
var y=new Date();
y.setFullYear(2110,0,16);
var current = new Date();
if ( y > current )
{
alert("Current Date is before 16th January 2110");
}
else
{
alert("Current Date is after 14th January 2100");
}
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>
Subscribe to:
Posts (Atom)