Web Chap4 JavaScript - 2023
Web Chap4 JavaScript - 2023
1ère année GI
(Version 0) 1
Plan
2
Chapitre 4 : Introduction au langage JavaScript
3
Chapitre 4 : Introduction au langage JavaScript
Objectifs
4
Chapitre 4 : Introduction au langage JavaScript
Plan
1. Les principes de JavaScript
2. Où écrire le code JavaScript ?
3. Entrées/sorties avec JS
4. Syntaxe
5. JavaScript : un langage orienté objet
6. Les fonctions/méthodes
7. Les opérateurs
8. Création d’objets en JavaScript
9. Styles en JS
Fatma ELLOUZE
6
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
7
Chapitre 4 : Introduction au langage JavaScript
JavaScript : Principes
8
Chapitre 4 : Introduction au langage JavaScript
Les versions
• ECMAScript 1 a été publiée pour la première fois en juin 1997
• ECMAScript 3 (publié en 1999)
• ECMAScript 5 (ES5) : La 5e édition d'ECMAScript, normalisée en 2009.
Cette norme a été complètement mise en œuvre dans tous les
navigateurs modernes.
• ECMAScript 6 (ES6)/ ECMAScript 2015 (ES2015) : La 6e édition
d'ECMAScript, normalisée en 2015. (e.g., let and const Keywords,
Arrow Functions … )
• ECMAScript 7 / 2016 ECMAScript 8 / 2017 …
Fatma ELLOUZE
9
Chapitre 4 : Introduction au langage JavaScript
contenu du script
</SCRIPT>
10
Chapitre 4 : Introduction au langage JavaScript
11
Chapitre 4 : Introduction au langage JavaScript
12
Chapitre 4 : Introduction au langage JavaScript
Entrées/sorties avec JS
14
Chapitre 4 : Introduction au langage JavaScript
Entrées/sorties
• 3 types de boites de messages peuvent être affichés en utilisant
Javascript
➢Méthode alert (): permet d’écrire un message dans une fenêtre.
➢Méthode confirm (): permet à l’utilisateur de choisir entre les boutons
OK et Annuler.
➢Méthode prompt(): ouvre une boite de dialogue avec une zone saisie
et 2 boutons OK et Annuler
• La méthode [Link]() permet d’écrire du code HTML dans une
page WEB.
Fatma ELLOUZE
2022
15
Chapitre 4 : Introduction au langage JavaScript
prompt(), alert ()
<html>
<head><title>Programme In1</title></head>
<body>
<script type="text/JavaScript">
annee= prompt('En quelle année sommes-nous ? ');
alert('Vous avez répondu : ' + annee);
</script>
</body>
</html>
Fatma ELLOUZE
2022
16
Chapitre 4 : Introduction au langage JavaScript
confirm ()
<html>
<head><title>Programme</title></head>
<body>
<script type="text/JavaScript">
if (confirm('Je vais dire sur quel bouton vous avez appuyé : ')) x= confirm('Je …')
{ if (x==true)
alert(' Sur OK \n Continuez avec :'); }
else {
alert(' Sur Annuler \n Sortez avec Ok !') ;}
</script>
</body> </html>
Fatma ELLOUZE
2022
17
Chapitre 4 : Introduction au langage JavaScript
[Link] ()
• Comment écrire, afficher... l'information dans une fenêtre
HTML ?
<html>
<head>
<title>Programme Out1</title>
</head>
<body>
<script type="text/JavaScript">
</body>
Fatma ELLOUZE
</html>
2022
18
Chapitre 4 : Introduction au langage JavaScript
open(), [Link] ()
</html>
2022
19
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Syntaxe
20
Chapitre 4 : Introduction au langage JavaScript
Syntaxe
21
Chapitre 4 : Introduction au langage JavaScript
22
Chapitre 4 : Introduction au langage JavaScript
23
Chapitre 4 : Introduction au langage JavaScript
de caractères :
2022
//Bonjour !
2022
</script>
25
Chapitre 4 : Introduction au langage JavaScript
Structures conditionnelles
Switch ()
if (condition)
switch(compte) {
{
case 1 :
instructions ; // traitement1 ….
} Break ;
[ else case 2 :
{ // traitement2 ….
instructions ; Break ;
Fatma ELLOUZE
}] default:
2022
Structures conditionnelles
• Exemple
<script language="JavaScript">
var code;
code= [Link]("Entrez le code de chiffres du produit", "");
if ([Link] == 3)
{
[Link] ("Le code est bon!");
}
else
{
[Link]("SVP, recommencez car le code \"" +code+ "\" n'est pas
valide.");
Fatma ELLOUZE
}
2022
</script>
27
Chapitre 4 : Introduction au langage JavaScript
Structures itératives
while (condition) ; }
28
Chapitre 4 : Introduction au langage JavaScript
Structures itératives
Exemple
<script language="Javascript">
for (i=0; i<10; i++) {
[Link](i + "<BR>")
}
</script>
<script language="Javascript">
var
person={fname:"John",lname:"Doe",age:25};
var txt="";
for (x in person)
Fatma ELLOUZE
{
txt=txt + person[x];
2022
}
[Link] (txt);
</script> 29
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Les fonctions/méthodes
30
Chapitre 4 : Introduction au langage JavaScript
Les fonctions
• Deux types de fonctions
1. Les fonctions propres à Javascript. Elles sont associées à un objet bien
particulier comme c’est le cas de la méthode alert() avec l'objet window.
alert (« bonjour »); // alert() est une fonction
[Link] (« bonjour »); // alert() est une méthode appliquée à un objet
syntaxe suivante
2022
31
Chapitre 4 : Introduction au langage JavaScript
NAME spécifié.
32
Chapitre 4 : Introduction au langage JavaScript
Fonctions-utilisateurs Exemple
<html>
• Valeur de retour non typée <head>
• Arguments non typés <script language="JavaScript">
Déclaration function Affiche(Texte)
function ma_fonction(arguments) { alert(Texte); }
{ </script>
instructions ; </head>
return quelque_chose; // ou pas… <body>
} <script language="JavaScript">
Appel
Fatma ELLOUZE
</script>
</body>
</html> 33
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Les opérateurs
34
Chapitre 4 : Introduction au langage JavaScript
Les opérateurs
• On distingue 8 opérateurs de comparaison
• 6 opérateurs de calcul
Fatma ELLOUZE
2022
35
Chapitre 4 : Introduction au langage JavaScript
Les opérateurs
• 2 opérateurs logiques
36
Chapitre 4 : Introduction au langage JavaScript
37
Chapitre 4 : Introduction au langage JavaScript
38
Chapitre 4 : Introduction au langage JavaScript
</FORM>
</BODY>
</HTML> 39
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Styles en JS
40
Les instructions de formatage de document
Chapitre 4 : Introduction au langage JavaScript
en JS
• On peut changer la couleur d'arrière-plan d'un document avec deux
méthodes :
1) [Link]="white"; //une manière ancienne obsolète
2) [Link]="white"; //manière moderne
et recommandée
41
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Les Événements
42
Chapitre 4 : Introduction au langage JavaScript
Événements
• Les événements sont des actions de l'utilisateur, qui vont donner lieu
à une interactivité.
• L'événement
• par excellence est le clic de souris.
• le passage de la souris au-dessus d'une zone,
• le changement d'une valeur, ...
Fatma ELLOUZE
2022
43
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Gestionnaires d’événement
44
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Gestionnaires d’événement
45
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
Gestionnaires d’événement
46
Chapitre 4 : Introduction au langage JavaScript
[Link](‘click', afficher);
2022
47
Chapitre 4 : Introduction au langage JavaScript
Exercice 1
• Voici une zone de texte. Entrez une valeur et appuyer sur le bouton
contrôler pour afficher ce que nous avons écrit dans la zone texte.
(utiliser une fonction avec un paramètre)
Fatma ELLOUZE
2022
48
Chapitre 4 : Introduction au langage JavaScript
Solution
<HTML>
<HEAD>
<SCRIPT LANGUAGE="javascript">
function controle(x) {
alert("Vous avez tapé : " + x);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="form1">
Fatma ELLOUZE
Exercice 2
50
HTML Code:
Chapitre 4 : Introduction au langage JavaScript
• <!DOCTYPE html>
• <html>
• <head>
• <SCRIPT type="text/javascript" src="[Link]"> </SCRIPT>
• </head>
• <body>
• <form name="form">
• 1st Number : <input type="text" name="n1" id="firstNumber" /><br>
• 2nd Number: <input type="text" name = "n2" id="secondNumber" /><br>
• <input type="button" onClick="multiplyBy()" Value="Multiply" />
• <input type="button" onClick="divideBy()" Value="Divide" />
• </form>
Fatma ELLOUZE
• }
2022
52
Fatma ELLOUZE
Chapitre 4 : Introduction au langage JavaScript
2022
53
Chapitre 4 : Introduction au langage JavaScript
Propriété Description
name indique le nom du contrôle. Tous les boutons portent le
même nom.
index l'index ou le rang du bouton radio en commençant par 0.
checked indique l'état en cours de l'élément radio
Fatma ELLOUZE
function reponse(form4) {
if (
([Link][0].checked===true) &&
([Link][1].checked===true) &&
([Link][2].checked===false) &&
([Link][3].checked===true)
) {
alert("C'est la bonne réponse!");
} else {
[Link]([Link][0].checked);
58