JavaScript Programming Course Overview
JavaScript Programming Course Overview
WEB TECHNOLOGIES
Chapter 3
JavaScript Language
SUMMARY
1. Introduction
2. Data structures
3. Control structures
[Link] classes
5. Procedures and functions
Event-driven programming
7. The browser objects
8. The WINDOW class
9. The document class
[Link] processing
[Link] management
[Link] DOM
JAVASCRIPT
1. Introduction
Why JAVASCRIPT?
Main objective:
introduce interactivity with HTML pages
perform simple tasks on the workstation
from the user
Means: introducing small programs, called
SCRIPTS, in HTML pages
structured programming language that contributes to
enrich the HTML
JS is fully integrated into the HTML code
JS contains event handlers
Writing and executing JS code
JavaScript code can be placed in 3 locations:
Between the <SCRIPT> and </SCRIPT> tags
Associated with an HTML tag that manages a
event
Associated with the pseudo-protocol
in a URL
JSEntre the <SCRIPT> tags and
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
...code//code : instructions, function declarations,
etc..
</SCRIPT>
If it is included in the <HEAD> section, it is not
executed right away.
On the other hand, if it is part of the body of the document, it is
immediately executed at the same time as the code
HTML is interpreted.
It is necessary to include the statements of
functions in the <HEAD>..</HEAD> section
JS associated with an HTML tag
Calculate
> JS is generally inserted in the form of a
function call
Function previously declared in the
HEAD party
Linked to the pseudo-protocol
javascript
<A HREF="JavaScript:void(calculer())">
To calculate
This pseudo-URL allows you to launch
the execution of a script written in JS
The VOID operator allows you to ignore the
return parameter of the function
Example
<html>
<head>
<script>
function greet() { alert("Hello everyone!"); }
</script>
</head>
<body>
Immediate execution
<script>
alert("Hello everyone!");
</script>
<H4>Exécution sur événementonClick</H4>
<form>
<input type="button" name="Bouton1" value="Salut" ></form>
Execution on protocoljavascript:
to greet
</body>
</html>
JAVASCRIPT
2. Data structures
The constant data
JS provides the following constants directly
available:
the numerical constants: integers, reals written in
decimal notation, or floating (i.e. in notation
scientific, e.g.: 2718E-3
the 2 boolean constants: true or false
strings, surrounded by quotes ""
or apostrophes ' '
the constant specialnull means 'nothing', 'absence of
value". This value is assigned to any variable
used without being defined (for example prompt()
return null if the Cancel button is selected
Strings
JavaScript converts integers to strings.
characters, which allows to concatenate
names with strings
In the writing instruction in the document
current, [Link](), the data to display
can be separated by commas or +
Special characters can also be inserted.
in the chains: \b (backspace), \f (form feed)
, (new line), (enter), & (tab); ' for
an apostrophe
We can insert HTML codes in the form of
strings that will be well interpreted at execution
like real markers, and not displayed
as they are.
Example
<script>
[Link]("Here is the approximate value at ",
near the constant e:
3);
Mon
<i>high school</i> is located in <u>Aladarissa</u>
in the city of Fes
</script>
The variables
5 types of variables in JS.
Les nombres : number, les chaines : string,
the booleans: boolean, the objects
Example:
var chaine = 'bonjour';
[Link](typeof(string));
Tableau
Declaration
varnom_tableau=new Array(dimension);
Examples
var MyArray = new Array(8)
var Les4saisons = new Array("printemps",
"été", "automne", "hiver");
var month = new Array(12);
Paintings
Properties
give the number of elements.
var month = new Array(12);
There are
the year
var NbMois = [Link];
shared in
quarters";
Methods
reverse() changes the order of the elements
sort() trie in ascending order, or according to the
model indicated as parameter
JAVASCRIPT
3. Control structures
if ... [then] .. else
if (condition1 && / || condition2 ) then (then is
optional)
{ séquence 1 }
else
{ séquence 2 }
The condition must always be surrounded by ( )
Sequence 1 is executed if the condition is
true.
The sequence2 (optional) is executed if the
the condition is false.
Braces { } are not mandatory only in case
multiple instructions.
Switch … Case
The switch instruction evaluates an expression and, depending on
the obtained result and the associated case, execute them
corresponding instructions.
Syntax:
switch (expression)
{
case value1: /*Instructions to execute when the result of the expression matches
to value1*/
instructions1;
[break;]
case valeurN: /* Instructions to execute when the result of the value expression
instructionsN;*/
[break;]
default: /* instructions to be executed when none of the values // match
instructions_def; [break;] */
}
The controlled iteration FOR
for(initial value; condition; continuation)
{sequence of instructions}
an initial value or expression concerning
an integer variable called counter.
a condition: as long as it is true, the
rehearsal is ongoing.
a pursuit expression that consists of
the update of the meter.
Example
<SCRIPT language = "JavaScript" >
Square table<BR>
for (var i = 10; i <= 15; i++)
i = i² = +
i*i+"<BR>"); }
</SCRIPT>
The WHILE iteration
WHILE(condition){sequence of instructions }
<SCRIPT language = "JavaScript" >
function random(N) { return [Link]([Link]()*N)+1; }
max= prompt("Nombres au hasard de 1 à ","6");
<H2>Table of numbers between 1 and randomly drawn,
until obtaining ",max,"</H2>";
tab = new Array(10*max);
a = random(max);
tab[0] = a;
i=0;
while ( a != max ) { a = random(max); tab[i]= a; i ++ }
max was obtained in the i-th draw <P>
i=0;
while ( tab[i] != null )
{ [Link]("tab [", i,"] = ", tab[i],"---");
if (i % 5 == 0 && i != 0) [Link]("<br>");
i ++; };
</SCRIPT>
JAVASCRIPT
6. Event-driven programming
Events
onBlur: occurs when a Textarea, Text, or Select field is no longer active.
onChange: occurs when a Textarea, Text, or Select field is modified
by the user.
onClick:se produit quand un composant Button, Checkbox, Radio Link, Reset
or Submit receives a mouse click
onFocus: occurs when a Textarea, Text, or Select component is activated.
onLoad occurs when the browser has finished loading a window or all.
the frames of a FRAMESET. The onLoad event is positioned in the tag
BODY or in the FRAMESET tag
onMouseOver occurs when the mouse passes over a hyperlink or an area
activatable of an image.
onSelect the product when a Textarea or Text component is selected.
onSubmit: occurs when a form is globally validated
submitButton.
onUnload:se produit quand on quitte un document. L'événementonUnloadse
position in the BODY tag or in the FRAMESET tag.
onAbort: occurs when the user interrupts the loading of an image
onError: occurs when loading a page or an image produces
an error.
onMouseout: occurs when the mouse leaves an area's area of an image or
a hyperlink.
onReset: se produit quand on clique sur le boutonreset of a form
Text areas
<FORM NAME="Questions">
<INPUT TYPE="text" NAME="txtClasse" VALUE="" SIZE="10">
</FORM>
Le texte entré par l'utilisateur est affecté à la propriétévaluedu
composanttxtClasse
The content of the text field is accessible by its name.
complete
[Link]
it can be assigned to a variableMyClass using the expression:
MyClass = [Link]
Forms in the same document are stored in a
table called forms; the first form is then noted
forms[0], the second is forms[1] etc..
If Questions is positioned in the first form, we
can replace the previous assignment with:
MyClass = [Link][0].[Link]
the SELECT object
selectedIndex number of the selected item in
the list
var num = [Link]
options[] is the predefined array containing the
items of the list
[Link][num] is the field object located at No.
num (reminder: the first has the number 0)
[Link][num].value is the value of the option
No. number of the list.
[Link][num].text is the following text
champ <OPTION>.
RADIO buttons
Protocol used:
[Link]
URL :[Link]=
[Link]
[Link]/util/programmation/javascript/J
our3/[Link]
Status bar message:
[Link]
Properties of the OBJECTDOCUMENT
Titre du document :[Link]=
JavaScript Course
Text color: [Link]=
#000000
Couleur du fond :[Link]=
#f1f1f1
Properties of the navigator object
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
JAVASCRIPT