|
|
JavaScript Programming
JavaScript is a scripting language that was founded by Netscape
some years ago. The language is simple and surprisingly powerful.
JavaScript has a lot of uses for Internet programmers. It can
make calculations, validate forms, work with cookies, and detect
what browser is being used - to name a scarce few.
There's a lot you can do with JavaScript: Images can swap when you move a mouse over them, form elements can influence each other on the fly, and calculations can be made without having to resort to a CGI script. And it works in all major browsers that are version 3.0 or higher (and even Netscape 2.0 to some degree).
MVI Solutions has been working with JavaScript for years, and this tutorial will teach you everything you need to know about using Java scripts on your web site.
We look at JavaScript fundamentals, including variables, if-then statements, link events, and image swaps. Following along as gets down and dirty with the JavaScript Document Object Model, windows and frames, JavaScript syntax with loops, arrays, and functions, and forms.
Most Web pages that claim interactivity really mean you can click on hyperlinks to go to new pages. Even Web pages that have CGI scripts behind them don't really seem all that interactive: Fill out a form, hit the Submit button, and wait. It's more like throwing bottles into an ocean and hoping for a meaningful reply. We have JavaScript. With JavaScript, images can swap when you move a cursor over them, form elements can influence each other on the fly, and calculations can be made without having to resort to a CGI script. There's none of this submit and wait stuff everything happens on your Web page while you're there.
One of the best things about JavaScript is that you can do a great deal with very little programming. You don't need a fancy computer, you don't need any software other than a word processor and a Web browser, and you don't need access to a Web server; you can do all your work right on your own computer.
Even though it's simple to work with, JavaScript is a complete programming language, so as you learn more complicated JavaScript, you're also learning the basics of computer programming. If you want to move on to other programming languages, like Perl, C, C++, or Java, JavaScript is a great introduction.
Sometimes JavaScript isn't JavaScript! It turns out that different browsers deal with JavaScript differently. In fact, different versions of the same browser handle JavaScript differently. So always double-check your JavaScript pages on as many different browsers (or even platforms) as possible.
JavaScript stores information, how it makes decisions based on that information, and how to change images based on user interaction.
Variables are simply the way JavaScript stores information.
These first two lines you've seen before. They're the typical preamble to any JavaScript program.
// load up some variables
var secs_per_min = 60;
var mins_per_hour = 60;
var hours_per_day = 24;
var days_per_year = 365;
The first line here is a comment. This comment states the obvious, but it's nice to block off chunks of variable declarations.
The next couple of lines are variable declarations. There are a few things to notice about these lines:
The first time you use a variable, you should declare it with the word "var."
Although declaring variables with var is not strictly necessary, it's usually a good idea. When we talk about functions two lessons from now, you'll see why.
Variables must start with either a letter or the underscore character.
After the first character, variables can have numbers. So monkey_23 is a fine variable name.
Variable names are case-sensitive.
This means that JavaScript will treat Loop and loop as two different variables. Generally, it's a good idea to pick a naming convention and stick to it. I like having all my variables lowercase, with underscores separating words, like secs_per_min in the example above. Other people prefer using internal capitalization, like secsPerMin.
Variables should describe what they are.
Variables such as x, y, or hack_hack_hack aren't very useful to a person who's trying to figure out your script. Don't make your variables so long that they take forever to type, but make them long enough to be descriptive.
You can give a variable a value when you declare it, or you can wait until later.
In the example, each variable was given a value the first time it was used. You don't have to do this, and we'll see examples later on where it's nice to declare a variable even though we don't know the value right away.
Statements end with a semicolon.
Statements are the sentences of JavaScript and semicolons are the end punctuation marks. Spaces and line breaks are ignored by the JavaScript interpreter so the layout of the script serves only to make it more legible for people. This entire example could have been written in one really long line if you take out the comments. But that would be impossible to read.
To be complete, I should mention that in some cases a semicolon isn't necessary and you might see some scripts in which people leave them out.
However, it's always a good idea to put the semicolon in there. Not only does it make your program more legible, but it also makes it less likely that adding a line later will mess up your program. Always stick a semicolon at the end of a statement. After JavaScript executes these statements, the variable secs_per_year will contain whatever you get when you multiply 60, 60, 24, and 365. From this point on, whenever JavaScript sees the variable secs_per_year, it will substitute in that huge number.
That's all the JavaScript that's in the header of this example. After JavaScript has executed all this code, the above variables will be declared and given values. That's very nice, but we haven't really done anything with the variables yet. That's what happens in the body of the example.
View source! The best way to learn JavaScript is to look at scripts other people have written. Selecting View Source on your browser can view JavaScript, just like HTML.
Finally, as with HTML, the best way to learn JavaScript is to experiment freely and often. At several places in this tutorial, you'll be given the opportunity to try things out. Don't be afraid to expand beyond the exercise to try new things.
Contact us for more help!
|
|