|
Javascript Basics 01
|
By Lisa Spurlin
[Hits: 12113]
|
|
JavaScript adds simple or sophisticated interactivity to a Website, enhancing the user's experience. Like any programminglanguage, you need to understand the building blocks before youcan start programming.
Start at the Beginning
Browsers know to interpret Web pages as HTML because of the tags. Since JavaScript is contained inside an HTMLdocument, it needs to be set apart with the tags.
TITLE< itle>
Don't forget that last tag! Abrowser will try andinterpret the whole HTML page as JavaScript, until it comes tothat closing tag. Without it, the page will generate unsightlyerrors and not load properly.
Comment, Comment, Comment
Commenting code allows you, or someone else looking at it, tounderstand what's occuring in the code. Commenting can be donein both single and multi-line variations:
// single line comments
/* multi-line comments */
But what about the HTML comment inside the script tags.That exists so older browsers that don't understand JavaScriptwon't try and interpret it. Otherwise, the code will render thepage as HTML, resulting in the page displaying incorrectly.
Defining Variables
JavaScript, like all programming languages, uses variables tostore information. These variables can store numbers, strings,variables that have been previously defined, and objects. Forexample:
Numeric: var x = 0; String: var y = "hello"; Variables: var z =x + y; Object: var myImage = new Image();
Strings MUST contain "" around the word or phrase. Otherwise theJavaScript will interpret it as a number. Numbers and previouslydefined variables, likewise, should not have "" unless you wantthat number to be treated as a string.
Ex: var x = hello ** wrong
Variables that store numbers and strings can be combined in anew variable. However, if anything is combined with a string, itis automatically be treated as a string.
Ex: var y = "1"; var z = "2"; var a = y + z;
The variable "a" in this instance is "12" not 3, since the twostrings were combined together as text, not added like numbers.This would be true even if y = 1.
Making a Statement
Notice the semi-colons (;) at the end of each line of code? Thesemi-colon denotes the end of that particular statement. WhileJavaScript can sometimes be forgiving if you don't include thesemi-colon at the end of each statement, it's good practice toremember to do so. Otherwise, you might not remember to put itthere when you really need it.
Alert! Alert!
Alerts are one of the greatest functions in JavaScript. They notonly pass information on to visitors, but help you when you'retrying to hunt down a bug in your code.
Examples of alerts
alert("this is a string"); creates an alert that will containthe text"this is a string"
alert(x) creates an alert that will contain the value defined invariable x (make sure that variable x is defined before callingit)
alert("the number is " + x); -creates an alert that will combinetext and the value in x.
It Can Do Math Too?
JavaScript wouldn't be much of a programming language if itcouldn't do simple math. While there are dozens of complex,built-in math functions, here are the basic symbols you'll needto know:
addition + subtraction - multiplication * division / greaterthan > less than < greater than or equal >= less than or equal<=
What "If" Statements
"If" statements are often used to compare values. If thestatement is true, a set of instructions enclosed in {}executes. Comparisons like this are done using the followingsymbols:
equals == not equal !=
In the example below, you can see how an "if" statement is usedto determine text that displays in an alert window. Copy andpaste the following script into an HTML page to see it at work.
Notice how the instructions for each statement (both the "if"and "else") are enclosed in {}(curly brackets). All curlybrackets must have a beginning and ending, just like HTML tags.If a curly bracket is missing the JavaScript will return anerror, so be sure to proof your code.
________________________________________ This document isprovided for informational purposes only, and i-Netovation.commakes no warranties, either expressed or implied, in thisdocument. Information in this document is subject to changewithout notice. The entire risk of the use or the results of theuse of this document remains with the user. The examplecompanies, organizations, products, people, and events depictedherein are fictitious. No association with any real company,organization, product, person, or event is intended or should beinferred. Complying with all applicable copyright laws is theresponsibility of the user. Without limiting the rights undercopyright, no part of this document may be reproduced, stored inor introduced into a retrieval system, or transmitted in anyform or by any means (electronic, mechanical, photocopying,recording, or otherwise), or for any purpose, wit hout theexpress written permission of i-Netovation.com.
If you believe that any of these documents, related materials orany other i-Netovation.com content materials are beingreproduced or transmitted without permission, please contact:violation@i-netovation.com.
The names of actual companies and products mentioned herein maybe the trademarks of their respective owners.
|
|
|
|