Part 10

advertisement
Javascrbipt Intro
• Javascript (or js) is a programming
language.
• Interpreted, not compiled.
• Not the same as java, but, similar.
• Use <SCRIPT> </SCRIPT> tags to use.
• Object-oriented – data encapsulated.
• Should write code within HTML comments.
1
Some Objects
•
•
•
•
document – The page we are viewing.
navigator – The browser we are using.
window – The current window.
To make an object do something, or set an
attribute do the following:
– object.do_something();
– object.set_attribute (value);
– object.attribute=value;
2
Examples
• document.bgColor = “red” sets the
background color to red.
• document.writeln(“Text String”); writes out
the text string to the screen with a newline.
• document.write(“Text String”); prints out
text string just as is.
3
Interactivity
• ONMOUSEOVER – lets you cause things
to happen when people mouse over certain
places on the page.
• <A HREF=“”
ONMOUSEOVER=“document.bgColor=‘re
d’”>red</A> -- will make the background
red when you mouse over the word “red”.
4
Forms and Scripts
• Forms let you enter data and then do
something with it.
• A function is a piece of computer code that
does something like manipulating data.
• Your form can call a script when it is done,
or when certain data is entered.
5
Forms and Scripts
• Make a function inside of <script> tags at
the top of your webpage.
• Write a form that calls the function when
things are entered.
– <input type=“button”, value=“press me”,
onclick=“printmessage(1)”>
6
Functions
• printmessage is a function.
– A name given to a sequence of program steps
that do something
• 1 is an argument or parameter, a piece of
data passed to the function so it can do
different things based on different numbers
being entered.
7
Functions 2
• function printresponse (num)
{
if (num == 1)
alert (“It is 1.”);
else
alert (“It is not 1.”);
}
8
Variables
• A variable is a data location or a name for a
piece of data you will use in a program.
• To declare a variable called name, type a
line with the words
– var name;
• You don’t need to specify a variable type
like you do in a lot of other languages.
9
Math operations
• Variables can be modified like in regular
math and algebra operations. The left-hand
side is the location where the result is
stored, and everything on the right hand
side is the formula to be evaluated.
• Variables can appear on both sides of the
equation.
10
If statements
• You can check whether two values are
larger or smaller than one another using if
statements. The format is
• If (leftside comparison rightside)
• Leftside and rightside are algebraic
expressions or numbers.
• Inequality examples: == != <= >= > <
11
Looping
• A loop repeats code a certain number of
times, except that each time, the code can
do slightly different things.
• Example:
var count;
for (count = 0; count < 10; count++)
document.writeln (count + “<BR>”);
12
Download
Related flashcards
Create flashcards