Your Ad Here

Monday, February 25, 2008

some more

String variables are used when a sequence of characters (any characters not to be treated as numbers) are required for display or program control.

String variables have several symbols in common with number variables.

"=" means the same as it does in numbers, "assign the value on the right to the variable on the left".

"==" means "is equal to" except the comparison is character by character rather than the numerical value. Examples:

var s = 'hi'; // assigns "hi" to variable s
s == 'hi'; // is true
s == 'hi ya'; // is false (is not exactly the same)
s == 'HI'; // is false ("==" is case sensitive)
s == 'bye'; // is false

The program code:

var s = 'hi';
if(s == 'hi') { document.write('The same!'); }

will print "The same!" on your page if the variable s contains 'hi'.

Another symbol that string and number variables have in common is the "+" symbol. For number variables, it means addition. However, for string variables it means concatenation:

var a = "Hello,";
var b = "world!";
var s = a + b;

stores the string "Hello,world!" in the variable s.

If you know ahead of time what the variables a and b will contain, you can accomplish the same thing with:

var s = "Hello," + "world!";

If you want a space in between, you could use either one of the following two lines:

var s = a + " " + b;
var s = "Hello," + " " + "world!";

With the above value in s, the code:

s = s + ' -- real loud :)';
document.write("My program says: " + s);

will print "My program says: Hello, world! -- real loud :)" on your page.

~~ ~~ Objects

An object can contain many variables and entire functions, and even other objects, within itself. In order to use the stuff in the object, the object must be assigned to a variable.

(Although you can make your own objects, this article deals only with objects that are built into the JavaScript language.)

To assign an object to a variable, the name of the object must have a pair of parenthesis at the end (which can have values in them, like functions) and you must use the word: new

~~ ~~ Date

Here is an example of assigning the Date object to a variable.

var d = new Date();

The variable d is now an object variable and has access to all the variables and functions within the object. To accomplish an access, type the object variable's name, then a period, then the name of the function or variable inside the object.

The following example will access several functions and store the results. Wherever you put the following code, it will print the current date and time on your page:



Notice the line: month = month + 1;

It is there because JavaScript's numerical representation of the calendar months are the digits 0 through 11, rather than the human representation of digits 1 through 12. The line adds 1 to the value returned by d.getMonth()

Also, notice the line beginning with: if(year

It is there because some browsers will display the correct 4-digit year and some browsers will display the year minus 1900. The line checks to see if year is less than 2000 and, if so, adds 1900 to it.

Let's do an example with another object.

~~ ~~ String

Sometimes, knowing the length of string variables can be essential to your program. To determine the length, you must create an object variable using the built-in: String

var s = new String('William');

The variable s is now an object variable which contains the string "William" along with several other values and functions. One of the values stored in s is the length of the string it contains. Two of the functions it contains are toUpperCase() and toLowerCase().

This code uses the mentioned value and functions (note that it will print the
 and 
HTML tags):



Note that a line break is represented in string variables with the two-character sequence: \n

The above code will write the following on your page as preformatted text:

Some stuff about William...
Length: 7
Upper Cased: WILLIAM
Lower Cased: william

"Netscape JavaScript Reference" and "Netscape - Client-Side JavaScript Guide" contain lists of built-in objects and functions. The references are linked from http://search.netscape.com/Computers/
Programming/Languages/JavaScript/References

~~ Program Flow Control

Much of what you do with JavaScript will require actions dependant on the contents of variables.

~~ ~~ if()

This articles series has used the if() flow control statement several times. It is of the format:

if() {
// code to execute
}

Between the parenthesis is the evaluation. Between the
curly braces is the code to execute if the evaluation
is true. Example:

if(n == 2) {
n++;
}

One-line code to execute following the if() statement does
not need to be in curly braces. And, whether or not it is
in curly braces, it can be all on one line. These two
examples work just as well as the above:

if(n == 2) { n++; }
if(n == 2) n++;

However, a multi-line code block must be within curly
braces:

if(n == 2) {
n++;
n = n * n;
}

All of the flow control statements in this section follow the same rule: If the code to run is a single line, it may be within curly braces but it is not necessary. However, if the code is multi-line, the curly braces are required.

~~ ~~ else

The if() flow control statement can have an accompanying: else

if(n == 2) n++;
else n--;

A multi-line "else" code block would look something like:

if(n == 2) n++;
else {
n = n + 21;
n = n * n;
}

If you use an else statement, it must immediately follow an if() statement.

For example, this will not run correctly:

if(n == 2) n++;
document.write('something');
else n = n - 1;

The corrected code is:

if(n == 2) n++;
else n = n - 1;
document.write('something');

~~ ~~ while()

The while() flow control statement executes it's code so long as whatever is between it's parenthesis evaluates as true:

var n = 1;
while(n <= 10) {
document.write('
' + n);
n++;
}

writes the current value of n on your page so long as the value of n is less than or equal to 10. In other words, it will write a list of numbers 1 through 10 on your page.

However, this

var n = 22;
while(n <= 10) {
document.write('
' + n);
n++;
}

will write nothing on your page because the statement between the parenthesis is never true.

~~ ~~ do ... while()

The do ... while() statement, however, will always execute it's code once because it checks the value in the parenthesis after the code is executed. Thus,

var n = 22;
do {
document.write('
' + n);
n++;
} while(n <= 10)

will print the number 22 on your page. And

var n = 1;
do {
document.write('
' + n);
n++;
} while(n <= 10)

will print the numbers 1 through 10.

~~ ~~ for()

The for() flow control statement is used to make a series of consistent changes and test a value repeatedly. There are three items within the for()'s parenthesis, separated with semi-colons.

The first item is the variable initialized to a specific value.

The second item is the check.

The third item changes the variable.

This prints the numbers 1 through 10:

for (var n = 1; n <= 10; n++) document.write('
' + n);

~~ Name Spitter

Here is a function that may amuse some people for a few moments. But then, maybe not. It's a simple thing with the purpose of demonstrating some of what has been covered in these first three parts of the JavaScript Tutorial.

The Name spitter is two JavaScript functions and a form that accepts a first name and a last name. When the button is clicked, the spitter does some calculations then opens up a popup window with the name printed twice as many times as the number of characters it contains, with ever increasing space between the names.

There are two functions, which should go between the and tags. The form should be below the tag. Here is the complete page:



















First Name:
Last Name:






soure of javascript tutorial all above post also

creat date on ur computer

print the current date and time on your page:

some basic tips of javascript

ariable Basics and Program Flow Control

Here is what this part of the tutorial covers:

Types of variables
Numbers
Strings
Objects
Date
String
Program Flow Control
if()
else
while()
do...while()
for()
Name Spitter


~~ Types of variables

In Part I of this article series you were introduced to variables. Part II used some more variables while explaining functions.

You may have come to the conclusion that JavaScript has several different types of variables. Well, you're right.

The variable type is determined by what type of value it contains.

JavaScript has 3 main types of variables:

(1) Number -- A variable containing a number. It can
be whole or decimal.

Examples: 4
4.4
44

(2) String -- A variable containing a string of
characters. It may include numbers and any
other characters. Strings are assigned to a
variable or otherwise manipulated in a JavaScript
program by enclosing them between either
single (') or double (") quotes.

Examples: '4b'
"Hello, world!"
"12 + 24"

When it is enclosed between single or double
quotes it is a string variable, even if all
the characters are numerical.

Example: "4"

Because "4" is between quotes, it signals the
browser that it is a string rather than a
number.

(3) Object -- An object is a variable which can
contain more than one value. It can contain
number(s) and string(s), together, in the same
object. An object variable can also contain
functions and even other objects.

Here are some basics about each of the variable types.

~~ ~~ Numbers

Number variables are used when number comparisons or mathematical calculations are required.

Here are some basic calculations that can be done:

Note: The equal sign used in the context of assigning a value to a variable is "determine the value or equation on the right and then let the variable on the left equal that determination". The programming code assigning a value to a variable is referred to as an assignment statement.

var n = 2; // variable n now holds the value 2
n = n * 3; // (* is multiply) variable n now holds the
// value 6
n = n / 4; // (/ is divide) variable n now holds the
// value 1.5
n = n - 7; // (- is subtract) variable n now holds the
// value -5.5
n = n + 13.5;// (+ is addition) variable n now holds the
// value 8
n++; // (++ is increment by 1) variable n now
// holds the value 9
n--; // (-- is decrement by 1) variable n now
// holds the value 8
n = n % 3; // (% is modulus -- the remainder of a
// division) variable n now holds the
// value 2
n = n * n; // variable n now holds the value 4

One could build a calculator using the above mathematical operators. A graphical calculator with a scrolling tape, operated entirely by clicking with no keyboard input being accepted, and with memory keys used to assign and display the results of interim calculations, may be a nice project for later in this series when a few more of the basics of program decision and flow control have been introduced.

Program flow is often determined by the content of a variable. This

if(n == 10) { document.write('It is 10.'); }

will print "It is 10." on your page if the variable n equals 10.

Program flow can also be decided by comparing two or more variables and determining whether they are equal or in what way they are unequal.

Here are some number variable comparisons that can be done. Each comparison yields either "true" or "false":

(n == b) // true if both n and b contain the same number.
Otherwise, false.
(Note: "==" is a test for equality while "="
is an assignment statement symbol.)
(n < b) // true if n is less than b. Otherwise, false.
(n <= b) // true if n is less than or equal to b. Otherwise,
false.
(n > b) // true if n is more than b. Otherwise, false.
(n >= b) // true if n is more than or equal to b. Otherwise,
false.
(n != b) // true if n is not equal to b. Otherwise, false.

In the above examples, the variable b could be an actual number. For example,

if(n < 10) { document.write('Is less than 10.'); }

will print "Is less than 10." on your page if the variable n is less than 10.

~~ ~~ Strings

String variables are used when a sequence of characters (any characters not to be treated as numbers) are required for display or program control.

String variables have several symbols in common with number variables.

"=" means the same as it does in numbers, "assign the value on the right to the variable on the left".

"==" means "is equal to" except the comparison is character by character rather than the numerical value. Examples:

var s = 'hi'; // assigns "hi" to variable s
s == 'hi'; // is true
s == 'hi ya'; // is false (is not exactly the same)
s == 'HI'; // is false ("==" is case sensitive)
s == 'bye'; // is false

The program code:

var s = 'hi';
if(s == 'hi') { document.write('The same!'); }

will print "The same!" on your page if the variable s contains 'hi'.

Another symbol that string and number variables have in common is the "+" symbol. For number variables, it means addition. However, for string variables it means concatenation:

var a = "Hello,";
var b = "world!";
var s = a + b;

stores the string "Hello,world!" in the variable s.

If you know ahead of time what the variables a and b will contain, you can accomplish the same thing with:

var s = "Hello," + "world!";

If you want a space in between, you could use either one of the following two lines:

var s = a + " " + b;
var s = "Hello," + " " + "world!";

With the above value in s, the code:

s = s + ' -- real loud :)';
document.write("My program says: " + s);

will print "My program says: Hello, world! -- real loud :)" on your page.

~~ ~~ Objects

An object can contain many variables and entire functions, and even other objects, within itself. In order to use the stuff in the object, the object must be assigned to a variable.

(Although you can make your own objects, this article deals only with objects that are built into the JavaScript language.)

To assign an object to a variable, the name of the object must have a pair of parenthesis at the end (which can have values in them, like functions) and you must use the word: new

~~ ~~ Date

Here is an example of assigning the Date object to a variable.

var d = new Date();

The variable d is now an object variable and has access to all the variables and functions within the object. To accomplish an access, type the object variable's name, then a period, then the name of the function or variable inside the object.

The following example will access several functions and store the results. Wherever you put the following code, it will print the current date and time on your page:

to count no of time our webpage work




onClick="decider()">

calculate date &diff




onClick="decider()">


When the page first loads, it assigns the current (this moment) date/time value to the variable Now. Then it uses that value to determine the current millisecond number with which to initialize the variable Start.

When you click the "Check Elapsed Milliseconds" on your page, it calls your decider() function. decider() calls the function elapsedmilliseconds() to determine the number of milliseconds that have elapsed since the page was first loaded -- when the variable Start was initialized.

When elapsedmilliseconds() is called, it determines the difference between it's own calculation and the value stored in the variable Start. It stores the result in variable diff.

Then elapsedmilliseconds() returns the value of the variable diff, which is what decider() assigns to its own variable d.

Last, decider() decides whether or not the page has been displayed a total of less than 10 minutes (600,000 milliseconds). If true, it displays an alert box with the value stored in d. Otherwise, it sends the visitor off to http://willmaster.com/

Hopefully, this has given you a glimmer of what is possible. Not only can you call your elapsedmilliseconds() function whenever you want, but other functions can call it, too, and make decisions based on what it returns.

Further elaboration and uses for program decision and flow control statements such as if(), for(), and while() will be in another section of this tutorial series.

(By the way, it took me 243740 milliseconds to write the above seven paragraphs while eating three small handfuls of popcorn.)

Let's suppose you want to modify your decider() function so you can tell it how many seconds to wait before sending the visitor off somewhere. And let's suppose you also want to tell it the location of that somewhere.

So you will modify your decider() function to accept a number (for the number of seconds) and a string of characters (for the URL). It will also be modified to use those data units where appropriate.

Here is the modified decider() function:

function decider(s,url)
{
var d = elapsedmilliseconds();
s = s * 1000; // s multiplies itself by 1000
if(d < s) { alert("Elapsed milliseconds: " + d); }
else { window.location = url; }
}

The decider() function receives two units of data when it
is called, the number of seconds and the url, and stores
them in variables s and url, respectively.

The variable "d" is then compared with "s" to see whether
the alert box is displayed or the visitor is redirected to
the value in "url".

You can call your modified decider() function with


onClick="decider(90,'http://mydomain.com/')">
onClick="decider(45,'http://mydomain.com/')">


or with


(90 seconds)


(45 seconds)


The above will send your function either the number 90 or the number 45 (for the number of seconds) and the string of characters http://mydomain.com (for the URL).

In a later article of this series, you will learn how to type stuff into a form (such as the number of seconds and the url, for the above example) and send that data to the function -- rather than hard-coding the data into a link.

And, you will often want to display function results in ways other than plain alert boxes.

Future articles will help you build popup boxes with your own design, on demand. They will help you display text and graphics on your page depending on which browser your visitor is using, depending on a name typed into a form, or other decisions your custom functions make.

Much of what you do with JavaScript depends on your program making decisions based on the contents of variables:

if([something]) then do this, else do that.

while([something]) do this.

for([each of a series]) do this.

The next article deals with those and other methods of program flow control.

Happy Happy!

William Bontrager, Programmer and Publisher "Screaming Hot CGI" programs "WillMaster Possibilities" ezine http://willmaster.com mailto:possibilities@willmaster.com Copyright 2000 by William and Mari Bontrager


JavaScript Tutorial Part III

Back



Click Here


Click here


Click Here

eTips Member Login

Enter your email address and password to enter the private membership area:
Email:
Password:


Lost Password?
Email:


Not yet a member?

Click here to see what you're missing! Club members receive access to dozens of free video tutorials, utilities and ebooks.

Products


Web-Source.net Products:

• Web Design Mastery
• eBook Starter

Other Products:

• Video Training Vault
• Plug-in Profits
• Affiliate Showcase
• Web Site Templates
• Business Tools
• Web Hosting

Our Other Sites


• Web Design Mastery
• eBook Starter
• Online Forex Trading
• Real Estate Investing
• Raging Hearts

Web Site Design


• HTML Tips
• PC Security
• CSS Tutorial
• HTML Codes
• Web Design Tips
• JavaScript Codes
• JavaScript Tutorial
• Java Applet Tutorial
• 216 Web Safe Colors
• Web Site Development
• ASCII Character Codes

Web Site Tools


• Domain Search
• Format Your Text
• Create Meta Tags
• Article Syndication
• Affiliate Showcase
• Free Classified Ads





Special SiteSell Promotion
Click Here



Daily News For
Webmasters

HI

WHAT ABOUT COMPUTER U KNOW I KNOW AND U ??????????????????

liveCricket Headline Animator