Thursday, February 5, 2009

Functions in Javascript

There are so many functions in Javascript, I am describing which are used most in common

1. alert(): One handy function is alert(), which puts a string into a little announcement box

Basic Syntax is alert("string value"); which is defined in the javascript declaration to display a pop up with your selected value.

2. prompt(): The built in function, which asks your visitor for some information and then sets a variable equal to whatever your visitor types.

Example:
<html>
<head>
<title>A Form Letter</title>
< script type="text/javascript" language="javascript">
var the_name = prompt("What's your name?", "Enter your name here");

</script>
</head>
<body>
<h1>Dear
<script type = "text/javascript">

document.write(the_name);

</script>
</body>
</html>

3. Date():
The syntax to call the date in the javascript is
var now = new Date();
It creates an Object that store data that require multiple pieces of information, such as a particular moment in time. For example, in JavaScript you need an object to
describe 2:30 PM on Saturday, January 7, 2006, in San Francisco. That’s because it requires many different bits of information: the time, day, month, date, and year, as well as some representation (in relation to Greenwich Mean Time) of the user’s local time.

Using this object we can retrieve all the values we need

a. For Year : get the year of the date stored in the variable now.
Example: var the_year = now.getYear();

b. For Date : Returns the day of the month as an integer from 1 to 31
Example: var the_Date = now.getDate();

c. For Day : Returns day of the week as an integer where 0 is Sunday and 1 is
Monday
Example: var the_Day = now.getDay();

d. For getHour : Returns the hour as an integer between 0 and 23
Example: var the_hours = now.getHours();

e. For Minutes : Returns the minutes as an integer between 0 and 59
Example: var the_minutes = now.getMinutes();

f. For Month : Return the month as an integer between 0 and 11 where 0 is January
and 11 is December
Example: var the_month = now.getMonth();

g. For Seconds : Returns the seconds as an integer between 0 and 59
Example: var the_seconds = now.getSeconds();

h. For Time : Returns the current time in milliseconds where 0 is January 1, 1970,
00:00:00
Example: var the_time = now.getTime();

i. For Year : Returns the year, but this format differs from browser to browser
Exampe: now.getYear();

4. navigator.appName: This function determines which browser you are using
Example:
var browser_name = navigator.appName;
If you are using mozilla then browser_name outputs "mozilla"
If you are using netscape then browser_name outputs "netscape"

It also uses another function to determine the browser properties getBrowser();

Example:
var browser_info = getBrowser();
var browser_name = browserInfo[0];
var browser_version = browserInfo[1];

5. For Redirecting the Users:
Using javascript we can redirect users from one page to another page.
Syntax:
window.location.href ="http://www.mywebsite.testpage.html";

No comments:

Post a Comment