|
Java Scripts How to write code Level 1. Place all codes between <body></body>.
Java Scripts is a program embedded in a HTML page. Code written between
the <script></script> tag doesn't appear on the users screen, its left for the
web browser to run the java script program. The <script></script> tag can
also be found between the <head></head> tags. Typically text that writes text to
the screen are found between the <body><body>.
First off Java Script isn't Java! Java Script and Java have almost nothing
to do with the other. Java is used to create applets, small programs that
download over the internet and run inside web browsers. To embed Java
applets into your web pages using applet html tag. When HTML sees the
Applet tag it downloads the Java Applet from the server then runs the specified
tag when user leaves that page the applets is dumped from memory. Java
script can communicate with html directly or Java applets and lets your write
code which give a powerful combination to your website.
Java Script is know as a snap-together language. It is Object-oriented,
"object is a type of thing computer, car, dog etc.", each object should have a
unique name since there can be more than one like car, car1 and car3, but
typically you are encouraged to keep unique names for cars maybe it would be The
Type of Car instead of name for dogs it might be Raven, Jack or Spot instead of
dog1, dog2, dog3, etc.
Objects: have properties like dog fur, car color, computer monitor.
Java Script a window has a title and a form a check box. Properties can
modify objects and properties can apply to completely different objects.
Like each car can have doors which would be properties but now car doors can
also become an object with its own properties, car door windows, car door
handles, etc.
Methods: What objects are able to do is called methods. cars run,
dogs play, computers crash. Java script objects are also methods, buttons
click(), text is selected(), windows will open(). The () parentheses indicate a
method rather than a properties. For those adept in English which I am not
think of objects as nouns and methods as verbs. Objects are things and
methods are actions things which can be done, or have been done to them.
You can put objects, properties and methods together to describe a process or
get a better description of an object. Also known as dot syntax
notice these are separated by .'S or periods.
car.door.window
computer.monitor.cord
raven.black.male
And here are Objects and methods written in dot syntax:
raven.plays()
computer.crashes()
car.windows.rolldown()
Events: are actions that the user performs when at your page. Moving
their mouse or pushing a button or form submitting. Java Script deals with
events with commands called event handlers. A visitor to your page
hits a button which triggers an event handler in your script. The handlers
are;
onAbort: user aborts page load.
onBlur: user leaves the object
onChange: user changed the object
onClick: user clicks the object
onError: script encounters an error
onFocus: user made an object active
onLoad: object finishes loading
onMouseOver: Cursor moves over a object
onMouseOut: mouse moves off of a object
onSelect: user selects the contents of a object
onSubmit: user submitted a form
onUnload: user left the window.
as the user triggers an event handler lets say now a
car handles the event onWindowDown() performing the action window down
Values & Variables: a piece of information in Java Script is known as a
value. there are two types numbers & string value (or text enclosed in ""
quotes), Variables contain values the variable MyCarColor is assigned the string
"Yellow" another way to write this is myCarColor="yellow". the = means is
assigned to. so now the variable name myCarColor is now set to yellow.
Java Script is Case Sensative Upper Case vs. lower case makes a difference.
Value Types:
Number: any number value ie.. 2.155556565
String: Characters inside the "" quotes. ie... "hello, EyeTweaks!"
Boolean: true or false
Null: empty or no value typicall 0 or blank space
object: value associated with the object
function: value returned by a function.
Operators: are symbols used to work along with variables.
Operators:
s + t(number) adds x & y together
s+t(string) adds text x &
text y together ie x=Eye & y=Tweaks =EyeTweaks
s-t
subtracts y from x
s*t
multiples x times y
s/t
divides x by y
s%t
Modulus or the remainder when x is divided by y
s++, ++s
adds one to x same as x=x+1
s--, --s
subtracts on from x same as x=x-1
-s
reverses the sign on x
now s++ & ++s are not the same the first increment s is after the assigned is
completed and before the last s so if s is equal to 1 s=s++ this value would be
1 so s=1 and t=++s is set to 2 it will result in both operators being set to 2
instead of the first one on = to 1 it is now both equal to 2. the --
operators work the same way. If you mix numeric and strings together like
"dog" + 9 you will get dog9.
Assignments & Comparisons: when you place a value in a variable you are
assigning the value to the variable. You will use what is called an
assignment operator to do the job. example is using a = equal operator to
do assignement like carColor="Yellow". another way of saying s=s+6 is s+=6.
Comparisions to compare one value to another, like the time of day or day of
week do this by checking if todaysDate =="Monday" or greater than and less than
when comparing strings remember that "a" is greater > than "A" and Alan is less
< than Sunshine.
Assignments:
s = t sets s to the y value
s += t same as s = s + t
s-=t same as s=s-t
s*=t same as s=s*t
s/=t same as s=s/t
s%=t same as s=s%t
Comparisions:
s==t returns true if s and t are equal
s!=t returns true if s and t are not equal
s>t returns if s is greater than t
s>=t returns true if s is greater than or equal to t
s<t returns true if s is less than t
s<=t returns true if s is less than or equal to t
s&&t returns true if both s and t are true
s||t returns true if either s or t is true
!s returns true if s is false
To write Java Script you need nothing more than word or notepad on Mac it might
be simpletext, personally i use front page and program directly into the file.
Almost any text editor will work for this. Java Script is really just
plain text. When you use your text editor just save the files with the
extensions .htm or .html so your browser can read them once you have uploaded to
your site.
Ok now you know the basics learn that little bit of information and the rest
will become easy.
Next Page
The coding begins.
|