10:31:21
10:31:21 Programmation Web Programmation Web 11
Programmation Web :
Programmation Web :
DOM / JavaScript
DOM / JavaScript
2 2
10:31:27
10:31:27 Programmation WebProgrammation Web
DOM = Document Object Model
DOM = Document Object Model
API (Application Programming Interface) pour la
API (Application Programming Interface) pour la
manipulation de HTML / XML
manipulation de HTML / XML
Définit la structure logique des documents
Définit la structure logique des documents
Définit la façon d’y accéder, de la manipuler
Définit la façon d’y accéder, de la manipuler
Créer des documents
Créer des documents
Parcourir leur structure
Parcourir leur structure
Ajouter, effacer, modifier des éléments
Ajouter, effacer, modifier des éléments
Ajouter, effacer, modifier leur contenu
Ajouter, effacer, modifier leur contenu
3 3
10:31:31
10:31:31 Programmation webProgrammation web
Qu’est-ce que le DOM ?
Qu’est-ce que le DOM ?
<
<
table
table
>
>
<
<
tbody
tbody
>
>
<
<
tr
tr
><
><
td
td
>
>
Shady Grove</
Shady Grove
</
td
td
>
>
<
<
td
td
>
>
Aeolian</
Aeolian
</
td
td
>
>
</
</
tr
tr
>
>
<
<
tr
tr
><
><
td
td
>
>
Over the River, Charlie</
Over the River, Charlie
</
td
td
>
>
<
<
td
td
>
>
Dorian</
Dorian
</
td
td
>
>
</
</
tr
tr
>
>
</
</
tbody
tbody
>
>
</
</
table
table
>
>
4 4
10:31:36
10:31:36 Programmation webProgrammation web
Qu’est-ce que le DOM ?
Qu’est-ce que le DOM ?
Représentation arborescente du document
Représentation arborescente du document
Modèle objet (structure + méthodes)
Modèle objet (structure + méthodes)
Permet la manipulation du document
Permet la manipulation du document
Une implémentation : JavaScript…
Une implémentation : JavaScript…
…
…
Des implémentations :
Des implémentations :
JavaScript IE
JavaScript IE
JavaScript Mozilla / Firefox
JavaScript Mozilla / Firefox
JavaScript Opera
JavaScript Opera
5 5
10:31:40
10:31:40 Programmation WebProgrammation Web
JavaScript : Principe
JavaScript : Principe
Langage de script objet
Langage de script objet
Syntaxe style C / C++ / Java
Syntaxe style C / C++ / Java
Sensible à la casse
Sensible à la casse
N’est PAS du Java
N’est PAS du Java
Exécuté par le client Web
Exécuté par le client Web
Peut être désactivé sur le client
Peut être désactivé sur le client
Nombreux objets pour la manipulation HTML
Nombreux objets pour la manipulation HTML
Gestion des événements HTML
Gestion des événements HTML
Rendre les pages dynamiques (HTML+CSS+JS)
Rendre les pages dynamiques (HTML+CSS+JS)
Haut niveau d’incompatibilité…
Haut niveau d’incompatibilité…
6 6
10:31:44
10:31:44 Programmation Web 2012-2013Programmation Web 2012-2013
JavaScript : Balise script
JavaScript : Balise script
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
language
language
=
=
"JavaScript"
"JavaScript"
>
>
<!--script
script
// -->
// -->
</
</
script
script
>
>
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
language
language
=
=
"JavaScript"
"JavaScript"
src
src
=
=
"URI"
"URI"
>
>
</
</
script
script
>
>
Masquer le script aux
Masquer le script aux
navigateurs non compatibles
navigateurs non compatibles
avec JavaScript
7 7
10:31:47
10:31:47 Programmation Web 2012-2013Programmation Web 2012-2013
JavaScript : Exemple
JavaScript : Exemple
<
<
html
html
>
>
<
<
head
head
>
>
<
<
title
title
>
>
Ma première page Web
Ma première page Web
</
</
title
title
>
>
</
</
head
head
>
>
<
<
body
body
>
>
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
language
language
=
=
"JavaScript"
"JavaScript"
>
>
<!--
document.writeln(
document.writeln("Salut !"
"Salut !"
) ;
) ;
// -->
// -->
</
</
script
script
>
>
</
</
body
body
>
>
</
</
html
html
>
>
8 8
10:31:51
10:31:51 Programmation Web 2012-2013Programmation Web 2012-2013
Variables
Variables
Déclaration de variables facultative
Déclaration de variables facultative
Variables non typées à la déclaration
Variables non typées à la déclaration
var
var
nom_variable
nom_variable
;
;
Typage dynamique à l’affectation
Typage dynamique à l’affectation
Types gérés:
Types gérés:
Nombres (
Nombres (
10, 3.14
10, 3.14
)
)
Booléens (true
Booléens (
true
, false
,
false
)
)
Chaînes ("Salut !"
Chaînes (
"Salut !"
, 'Salut !'
,
'Salut !'
)
)
null
null
9 9
10:31:55
10:31:55 Programmation Web 2012-2013Programmation Web 2012-2013
Structures conditionnelles
Structures conditionnelles
if
if
(
(
condition
condition
)
)
{
{
instructions ;
instructions ;
}
}
[
[ else
else
{
{
instructions ;
instructions ;
}
}
]
]
10 10
10:31:59
10:31:59 Programmation Web 2012-2013Programmation Web 2012-2013
Structures conditionnelles
Structures conditionnelles
switch
switch
(
(
expression
expression
)
)
{
{
case
case
étiquette
étiquette
:
:
instructions ;
instructions ;
break
break
;
;
case
case
étiquette
étiquette
:
:
instructions ;
instructions ;
break
break
;
;
default
default
:
:
instructions ;
instructions ;
}
}
11 11
10:32:03
10:32:03 Programmation Web 2012-2013Programmation Web 2012-2013
Structures itératives
Structures itératives
while
while
(
(
condition
condition
)
)
{
{
instructions ;
instructions ;
}
}
do
do
{
{
instructions ;
instructions ;
}
}
while
12 12
10:32:07
10:32:07 Programmation Web 2012-2013Programmation Web 2012-2013
Structures itératives
Structures itératives
for
for
(
(
instr ;
instr ;
condition ; instr
condition ; instr
)
)
{
{
instructions ;
instructions ;
}
}
for
for
(
(
variable
variable
in
in
objet
objet
)
)
{
{
instructions ;
instructions ;
}
}
13 13
10:32:11
10:32:11 Programmation Web 2012-2013Programmation Web 2012-2013
Commentaires
Commentaires
// Commentaire ligne
// Commentaire ligne
/* Commentaire multi-lignes */
/* Commentaire multi-lignes */
14 14
10:32:15
10:32:15 Programmation Web 2012-2013Programmation Web 2012-2013
Fonctions
Fonctions
Valeur de retour non typée
Valeur de retour non typée
Arguments non typés
Arguments non typés
// Déclaration
// Déclaration
function
function
ma_fonction
ma_fonction
(
(
arguments
arguments
)
)
{
{
instructions ;
instructions ;
return
return
quelque_chose;
quelque_chose;
// ou pas…
// ou pas…
}
}
ma_fonction
15 15
10:32:19
10:32:19 Programmation Web 2012-2013Programmation Web 2012-2013
Objets prédéfinis
Objets prédéfinis
window
window
alert(
alert(
message
message
)
)
// Message d’avertissement
// Message d’avertissement
confirm(
confirm(
message
message
)
)
// Message de confirmation : retourne
// Message de confirmation : retourne
true ou false
true ou false
prompt(
prompt(
message
message
,
,
valeur_par_défaut
valeur_par_défaut
)
)
// Boîte de saisie : retourne la chaîne
// Boîte de saisie : retourne la chaîne
saisie
saisie
16 16
10:32:23
10:32:23 Programmation Web 2012-2013Programmation Web 2012-2013
Objets prédéfinis
Objets prédéfinis
document
document
write(
write(
message
message
)
)
// Ecrire dans le document
// Ecrire dans le document
writeln(
writeln(
message
message
)
)
// Ecrire dans le document (retour à la
// Ecrire dans le document (retour à la
ligne)
ligne)
17 17
10:32:27
10:32:27 Programmation Web 2012-2013Programmation Web 2012-2013
Chaînes : Propriétés & Méthodes
Chaînes : Propriétés & Méthodes
Propriétés
Propriétés
length
length
…
…
Méthodes
Méthodes
charAt(index
charAt(
index
)
)
charCodeAt(
charCodeAt(
index
index
)
)
concat(
concat(
chaine2
chaine2
,
,
chaine3
chaine3
, …)
, …)
fromCharCode(code1
fromCharCode(
code1
,
,
code2
code2
, …)
, …)
indexOf(
indexOf(
aiguille
aiguille
[,
[,
index
index
])
])
lastIndexOf(aiguille
lastIndexOf(
aiguille
[, index
[,
index
])
])
match(
match(
expr_reg
expr_reg
)
)
18 18
10:32:31
10:32:31 Programmation Web 2012-2013Programmation Web 2012-2013
Chaînes : Exemples
Chaînes : Exemples
var
var
s =
s =
"Bon anniversaire Benjamin"
"Bon anniversaire Benjamin"
;
;
document.write(s.charAt(
document.write(s.charAt(
2)) ;
2
)) ;
n
n
document.write(s.charCodeAt(
document.write(s.charCodeAt(
2)) ;
2
)) ;
110
110
document.write(s.concat(
document.write(s.concat(
" du groupe C12"
" du groupe C12"
)) ;
)) ;
Bon anniversaire Benjamin du groupe C12
Bon anniversaire Benjamin du groupe C12
document.write(String.fromCharCode(
document.write(String.fromCharCode(
65,
65
,
66, 67)) ;
66, 67
)) ;
ABC
ABC
document.write(s.indexOf(
document.write(s.indexOf(
"Benjamin"
"Benjamin"
)) ;
)) ;
17
17
document.write(s.lastIndexOf(
document.write(s.lastIndexOf(
"a"
"a"
)) ;
)) ;
21
21
document.write(s.match(
document.write(s.match(
/Benjamin$/
/Benjamin$/
)) ;
)) ;
19 19
10:32:36
10:32:36 Programmation Web 2012-2013Programmation Web 2012-2013
Chaînes : Méthodes
Chaînes : Méthodes
Méthodes
Méthodes
replace(expr_reg, nouvelle_chaine
replace(
expr_reg, nouvelle_chaine
)
)
search(
search(
expr_reg
expr_reg
)
)
slice(debut
slice(
debut
[,
[,
fin
fin
])
])
split(
split(
separateur
separateur
[,
[,
limite
limite
])
])
substr(
substr(
debut
debut
[,
[,
taille
taille
])
])
substring(
substring(
debut
debut
,
,
fin
fin
)
)
toLowerCase()
toLowerCase()
toUpperCase()
toUpperCase()
Opérateurs
Opérateurs
20 20
10:32:41
10:32:41 Programmation Web 2012-2013Programmation Web 2012-2013
Chaînes : Exemples
Chaînes : Exemples
var
var
s =
s =
"Bon anniversaire Benjamin"
"Bon anniversaire Benjamin"
;
;
document.write(s.replace(
document.write(s.replace(
/i/g
/i/g
,
,
'I'
'I'
)) ;
)) ;
Bon annIversaIre BenjamIn
Bon annIversaIre BenjamIn
document.write(s.search(
document.write(s.search(
/n{2}/i
/n{2}/i
)) ;
)) ;
5
5
document.write(s.slice(
document.write(s.slice(
17)) ;
17
)) ;
Benjamin
Benjamin
document.write(s.split(
document.write(s.split(
" "
" "
)) ;
)) ;
Bon,anniversaire,Benjamin
Bon,anniversaire,Benjamin
document.write(s.substr(
document.write(s.substr(
4,
4
,
12)) ;
12
)) ;
anniversaire
anniversaire
document.write(s.substring(
document.write(s.substring(
4,
4
,
16)) ;
16
)) ;
anniversaire
anniversaire
document.write(s.toUpperCase()+s.toLowerCase()) ;
document.write(s.toUpperCase()+s.toLowerCase()) ;
21 21
10:32:45
10:32:45 Programmation Web 2012-2013Programmation Web 2012-2013
Objet
Objet
Math
Math
Propriétés
Propriétés
E, LN10, LN2, LOG10E, LOG2E, PI,
E, LN10, LN2, LOG10E, LOG2E, PI,
SQRT1_2, SQRT2
SQRT1_2, SQRT2
Méthodes
Méthodes
abs(
abs(
val
val
)
)
acos(
acos(
val
val
), cos(
), cos(
val
val
), …
), …
exp(
exp(
val
val
), log(
), log(
val
val
)
)
floor(
floor(
val
val
), round(
), round(
val
val
), ceil(
), ceil(
val)
val)
max(
max(
val1
val1
,
,
val2
val2
), min(
), min(
val1
val1
,
,
val2
val2
)
)
pow(
pow(
val
val
,
,
puiss
puiss
), sqrt(val
), sqrt(
val
)
)
22 22
10:32:50
10:32:50 Programmation Web 2012-2013Programmation Web 2012-2013
Objet
Objet
Math
Math
: Exemples
: Exemples
document.write(
document.write(
115.
115
.
04+
04
+
15) ;
15
) ;
130.04000000000002
130.04000000000002
(
(
Euh ?...
Euh ?...
)
)
document.write(Math.PI) ;
document.write(Math.PI) ;
3.141592653589793
3.141592653589793
document.write(Math.abs(
document.write(Math.abs(
-12.
-12
.
34)) ;
34
)) ;
12.34
12.34
document.write(Math.floor(
document.write(Math.floor(
12.
12
.
54)) ;
54
)) ;
12
12
document.write(Math.round(
document.write(Math.round(12
12.
.54
54)) ;
)) ;
13
13
document.write(Math.ceil(
document.write(Math.ceil(
12.
12
.
54)) ;
54
)) ;
13
13
document.write(Math.random()) ;
document.write(Math.random()) ;
0.394555831655689
0.394555831655689
23 23
10:32:54
10:32:54 Programmation Web 2012-2013Programmation Web 2012-2013
Propriétés & Fonctions supérieures
Propriétés & Fonctions supérieures
Propriétés
Propriétés
Infinity, NaN, undefined
Infinity, NaN, undefined
Fonctions
Fonctions
eval(
eval(
chaine
chaine
)
)
isFinite(
isFinite(
nombre
nombre
)
)
isNaN(
isNaN(
objet
objet
)
)
parseFloat(
parseFloat(
chaine
chaine
)
)
parseInt(
parseInt(
chaine
chaine
)
)
escape(
escape(
chaine
chaine
)
)
unescape(
unescape(
chaine
chaine
)
)
24 24
10:32:58
10:32:58 Programmation Web 2012-2013Programmation Web 2012-2013
document.write(eval(
document.write(eval(
"Math.pow(3+2, 2)"
"Math.pow(3+2, 2)"
)) ;
)) ;
25
25
document.write(isFinite(Math.log(
document.write(isFinite(Math.log(
0))) ;
0
))) ;
false
false
document.write(isNaN(
document.write(isNaN(
"abcd"
"abcd"
)) ;
)) ;
true
true
document.write(
document.write(
"12.34"
"12.34"
+
+
2) ;
2
) ;
12.342
12.342
document.write(parseFloat(
document.write(parseFloat(
"12.34"
"12.34"
)+2
)+
2) ;
) ;
14.34
14.34
document.write(escape(
document.write(escape(
"Bon anniversaire"
"Bon anniversaire"
)) ;
)) ;
Bon%20anniversaire
Bon%20anniversaire
document.write(unescape(
document.write(unescape(
"Bon%20anniversaire"
"Bon%20anniversaire"
)) ;
)) ;
Bon anniversaire
Bon anniversaire
Propriétés & Fonctions supérieures
Propriétés & Fonctions supérieures
25 25
10:33:02
10:33:02 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux
Tableaux
Objet Array
Objet Array
Déclaration
Déclaration
var
var
tab1 =
tab1 =
new
new
Array(
Array(
taille) ;
taille
) ;
var
var
tab2 =
tab2 =
new
new
Array(
Array(
1,
1
,
"a"
"a"
,
,
9,
9
,
…) ;
…
) ;
index
index
0 1 2 …
0 1 2 …
Utilisation
Utilisation
window.alert(tab2[
window.alert(tab2[
0]) ;
0
]) ;
// 1
// 1
tab2[
tab2[
2] = 6
2
] = 6
// 6 remplace 9
// 6 remplace 9
Accroissement automatique de la taille
Accroissement automatique de la taille
var
var
tab1 =
tab1 =
new
new
Array(
Array(
2) ;
2
) ;
tab1[
26 26
10:33:06
10:33:06 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux
Tableaux
Parcours
Parcours
var
var
tab2 =
tab2 =
new
new
Array(
Array(
1,
1
,
"a"
"a"
,
,
9) ;
9
) ;
tab2[
tab2[
200] =
200
] =
12 ;
12
;
for (i in tab2)
for (i in tab2)
window.alert(
window.alert(
"tab2["
"tab2["
+ i +
+ i +
"] = "
"] = "
+ tab2[i]) ;
+ tab2[i]) ;
// tab2[0] = 1
// tab2[0] = 1
// tab2[1] = a
// tab2[1] = a
// tab2[2] = 9
// tab2[2] = 9
// tab2[200] = 12
// tab2[200] = 12
27 27
10:33:10
10:33:10 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux : Propriétés & Méthodes
Tableaux : Propriétés & Méthodes
Propriétés
Propriétés
length
length
…
…
Méthodes
Méthodes
concat(
concat(
tab2
tab2
,
,
tab3
tab3
, …)
, …)
join(sépar
join(
sépar
)
)
Transforme un tableau en chaîne de
Transforme un tableau en chaîne de
caractères.
caractères.
pop()
pop()
Retire le dernier élément d'un tableau
Retire le dernier élément d'un tableau
push(val1
push(
val1
,
,
val2
val2
, …)
, …)
Ajoute un ou plusieurs
Ajoute un ou plusieurs
éléments à la fin d'un tableau.
éléments à la fin d'un tableau.
shift():
shift():
Retire le premier élément d'un tableau
Retire le premier élément d'un tableau
unshift(
unshift(
val1
val1
,
,
val2
val2
, …)
, …)
28 28
10:33:14
10:33:14 Programmation Web 2012-2013Programmation Web 2012-2013
Contrôle de formulaires
Contrôle de formulaires
Vérifier la cohérence de la saisie
Vérifier la cohérence de la saisie
Contrôles sur le client
Contrôles sur le client
Évite les transmissions client / serveur
Évite les transmissions client / serveur
Contrôles possibles:
Contrôles possibles:
Présence de valeur
Présence de valeur
Numérique / Chaîne
Numérique / Chaîne
Expressions régulières
Expressions régulières
Événement
Événement
onSubmit
onSubmit
29 29
10:33:18
10:33:18 Programmation Web 2012-2013Programmation Web 2012-2013
Contrôle de formulaires
Contrôle de formulaires
<
<
html
html
><
><
head
head
><
><
title
title
>
>
Contrôle
Contrôle
</
</
title
title
>
>
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
>
>
function
function
verif
verif
() {
() {
if
if
(document.formu.txt.value !=
(document.formu.txt.value !=
''
''
)
)
return
return
window.confirm(
window.confirm(
'Envoyer ?'
'Envoyer ?'
) ;
) ;
return
return
false ;
false ;
}
}
</
</
script
script
></
></
head
head
><
><
body
body
>
>
<
<
form
form
name
name
=
=
"formu"
"formu"
action
action
=
=
"URI"
"URI"
method
method
=
=
"GET"
"GET"
onSubmit="
onSubmit="
return
return
verif() ;"
verif() ;"
>
>
<
<
input
input
type
type
=
=
"text"
"text"
name
name
=
=
"txt"
"txt"
>
>
<
<
input
input
type
type
=
=
"submit"
"submit"
value
value
=
=
"Envoyer"
"Envoyer"
>
>
</
30 30
10:33:22
10:33:22 Programmation Web 2012-2013Programmation Web 2012-2013
Formulaires : Propriétés & Méthodes
Formulaires : Propriétés & Méthodes
Propriétés
Propriétés
action
action
elements
elements
encoding
encoding
length
length
method
method
name
name
target
target
Méthodes
Méthodes
reset()
reset()
submit()
submit()
31 31
10:33:26
10:33:26 Programmation Web 2012-2013Programmation Web 2012-2013
Objets
Objets
commandes
commandes
de formulaires
de formulaires
Text
Text
Textarea
Textarea
Hidden
Hidden
Password
Password
CheckBox
CheckBox
Radio (/!\ tableau de /!\)
Radio (/!\ tableau de /!\)
Submit / Reset
Submit / Reset
Select
Select
Option
Option
32 32
10:33:30
10:33:30 Programmation Web 2012-2013Programmation Web 2012-2013
Formulaires : Exemple
Formulaires : Exemple
<
<
form
form
name
name
=
=
'formu'
'formu'
onSubmit='return verif(
onSubmit='return verif(
this
this
);
);
'
'
>
>
<
<
input
input
type
type
=
=
'text'
'text'
name
name
=
=
'texte'
'texte'
><
><
br
br
>
>
<
<
select
select
name
name
=
=
'sel'
'sel'
>
>
<
<
option
option
>
>
?
?
<
<
option
option
value
value
=
=
1
1
>
>
Un
Un
<
<
option
option
value
value
=
=
2
2
>
>
Deux
Deux
</
</
select
select
><
><
br
br
>
>
<
<
input
input
type
type
=
=
'radio'
'radio'
name
name
=
=
'rad'
'rad'
id
id
=
=
'rad1'
'rad1'
>
>
<
<
label
label
for
for
=
=
'rad1'
'rad1'
>
>
oui</
oui
</
label
label
>
>
<
<
input
input
type
type
=
=
'radio'
'radio'
name
name
=
=
'rad'
'rad'
id
id
=
=
'rad2'
'rad2'
>
>
<
<
label
label
for
for
=
=
'rad2'
'rad2'
>
>
non</
non
</
label
label
><
><
br
br
>
>
<
<
input
input
type
type
=
=
'checkbox'
'checkbox'
name
name
=
=
'chk'
'chk'
id
id
=
=
'chk1'
'chk1'
>
>
<
<
label
label
for
for
=
=
'chk1'
'chk1'
>
>
OK</
OK
</
label
label
><
><
br
br
>
>
<
<
input
input
type
type
=
=
'submit'
'submit'
value
value
=
=
'Envoyer'
'Envoyer'
>
>
</
33 33
10:33:34
10:33:34 Programmation Web 2012-2013Programmation Web 2012-2013
Formulaires : accès aux champs
Formulaires : accès aux champs
<
<
form
form
name
name
=
=
'formu'
'formu'
onSubmit='return verif(
onSubmit='return verif(
this
this
);
);
'
'
>
>
<
<
input
input
type
type
=
=
'text'
'text'
name
name
=
=
'texte'
'texte'
> ...
> ...
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
>
>
function
function
verif(f)
verif(f)
{
{
... f.texte.value ;
... f.texte.value ;
}
}
</
</
script
script
>
>
<
<
form
form
name
name
=
=
'formu'
'formu'
onSubmit='return verif();
onSubmit='return verif();
'
'
>
>
<
<
input
input
type
type
=
=
'text'
'text'
name
name
=
=
'texte'
'texte'
> ...
> ...
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
>
>
function
function
verif(f)
verif(f)
{
{
... document.formu.texte.value ;
... document.formu.texte.value ;
... document.forms[0].elements[0].value ;
... document.forms[0].elements[0].value ;
... document.forms[
... document.forms[
"formu"
"formu"
].elements[
].elements[
"texte"
"texte"
].value ;
].value ;
}
}
</
</
script
script
>
>
Objet formulaire
Objet formulaire
Nom du champ
Nom du champ
objet champ texte
objet champ texte
Index du formulaire dans la page
Index du formulaire dans la page
Index du champ dans le formulaire
Index du champ dans le formulaire
Nom du formulaire
Nom du formulaire
objet formulaire
objet formulaire
Nom du champ dans le formulaire
Nom du champ dans le formulaire
Tableau des formulaires de la page
Tableau des formulaires de la page
Tableau des champs du formulaire
Tableau des champs du formulaire
Nom du formulaire dans la page
Nom du formulaire dans la page
Nom du champ dans le formulaire
34 34
10:33:38
10:33:38 Programmation Web 2012-2013Programmation Web 2012-2013
Formulaires : Exemple
Formulaires : Exemple
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
>
>
function
function
verif(f)
verif(f)
{
{
window.alert(f.texte.value) ;
window.alert(f.texte.value) ;
window.alert(f.sel.selectedIndex) ;
window.alert(f.sel.selectedIndex) ;
window.alert(f.sel[f.sel.selectedIndex].text) ;
window.alert(f.sel[f.sel.selectedIndex].text) ;
window.alert(f.sel[f.sel.selectedIndex].value) ;
window.alert(f.sel[f.sel.selectedIndex].value) ;
window.alert(f.rad[
window.alert(f.rad[
0].checked) ;
0
].checked) ;
window.alert(f.chk.checked) ;
window.alert(f.chk.checked) ;
return
return
false
false
;
;
}
}
</
35 35
10:33:42
10:33:42 Programmation Web 2012-2013Programmation Web 2012-2013
Expressions rationnelles
Expressions rationnelles
Expressions rationnelles / régulières
Expressions rationnelles / régulières
/modele/drapeaux (drapeaux: g, i, gi)
/modele/drapeaux (drapeaux: g, i, gi)
new RegExp("modele"[, "drapeaux"])
new RegExp("modele"[, "drapeaux"])
Modèles
Modèles
^
^
: début de
: début de
$
$
: fin de
: fin de
*
*
: 0 à n fois
: 0 à n fois
+
+
: 1 à n fois
: 1 à n fois
?
?
: 0 à 1 fois
: 0 à 1 fois
.
.
: un caractère sauf retour chariot
: un caractère sauf retour chariot
|
|
: ou
: ou
36 36
10:33:46
10:33:46 Programmation Web 2012-2013Programmation Web 2012-2013
Expressions rationnelles
Expressions rationnelles
Modèles
Modèles
(x)
(x)
:
:
x
x
et mémorise
et mémorise
{n}
{n}
:
:
n
n
fois
fois
{n,}
{n,}
: au moins
: au moins
n
n
fois
fois
{n,m}
{n,m}
: de
: de
n
n
à
à
m
m
fois
fois
[xyz]
[xyz]
: 1 élément de la liste
: 1 élément de la liste
[a-z]
[a-z]
: 1 élément de la série
: 1 élément de la série
[^xyz]
[^xyz]
: 1 élément n'étant pas dans la liste
: 1 élément n'étant pas dans la liste
[^a-z]
[^a-z]
: 1 élément n'étant pas dans la série
: 1 élément n'étant pas dans la série
\b
\b
: frontière de mot
: frontière de mot
37 37
10:33:50
10:33:50 Programmation Web 2012-2013Programmation Web 2012-2013
Expressions rationnelles
Expressions rationnelles
Modèles
Modèles
\d = [0-9]
\d = [0-9]
: chiffre
: chiffre
\D = [^0-9]
\D = [^0-9]
: non chiffre
: non chiffre
\n
\n
: retour à la ligne
: retour à la ligne
\s
\s
: séparateur de mot
: séparateur de mot
\S
\S
: non séparateur de mot
: non séparateur de mot
\t
\t
: tabulation
: tabulation
\w =
\w =
[A-Za-z0-9_]
[A-Za-z0-9_]
: 1 caractère alphanumérique
: 1 caractère alphanumérique
Méthodes
Méthodes
38 38
10:33:53
10:33:53 Programmation Web 2012-2013Programmation Web 2012-2013
Expressions rationnelles : Exemples
Expressions rationnelles : Exemples
<
<
script
script
type
type
=
=
"text/javascript"
"text/javascript"
>
>
document.write(
document.write(
/l/
/l/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^l/
/^l/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^h/
/^h/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^h/i
/^h/i
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^Hel.o/
/^Hel.o/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^Hel+o/
/^Hel+o/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^He+llo/
/^He+llo/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^Hea*llo$/
/^Hea*llo$/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^He(l|o)*$/
/^He(l|o)*$/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^H[leos]+/
/^H[leos]+/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^H[^leo]+/
/^H[^leo]+/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^H[^kyz]+/
/^H[^kyz]+/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^H[a-z]*/
/^H[a-z]*/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
document.write(
document.write(
/^H[a-z]*$/
/^H[a-z]*$/
.test(
.test(
'Hello'
'Hello'
)) ;
)) ;
</
</
script
script
>
>
true
true
false
false
false
false
true
true
true
true
true
true
true
true
true
true
true
true
true
true
false
false
true
true
true
true
true
true
Dates : Propriétés & Méthodes
Dates : Propriétés & Méthodes
Méthodes
Méthodes
Constructeur
Constructeur
getDay(), attention de 0 (dimanche) à 6 (samedi)…
getDay(), attention de 0 (dimanche) à 6 (samedi)…
getDate() / setDate()
getDate() / setDate()
getMonth() / setMonth(), attention de 0 à 11…
getMonth() / setMonth(), attention de 0 à 11…
getHours()/ setHours()
getHours()/ setHours()
getMinutes() / setMinutes()
getMinutes() / setMinutes()
getTime() / setTime()
getTime() / setTime()
getYear() / setYear() / getFullYear() / setFullYear()
getYear() / setYear() / getFullYear() / setFullYear()
parse()
parse()
39 39
10:33:57
Dates : Exemples
Dates : Exemples
var
var
today =
today =
new
new
Date()
Date()
document
document
.write(today) ;
.write(today) ;
var
var
birthday =
birthday =
new
new
Date(
Date(
"December 17, 1995
"December 17, 1995
03:24:00")
03:24:00")
document
document
.write(birthday) ;
.write(birthday) ;
birthday =
birthday =
new
new
Date(95
Date(95
,11,17)
,11,17)
document
document
.write(birthday) ;
.write(birthday) ;
birthday =
birthday =
new
new
Date(95
Date(95
,11,17,3,24,0)
,11,17,3,24,0)
document
document
.write(birthday) ;
.write(birthday) ;
document
document
.write(birthday.getDay()) ;
.write(birthday.getDay()) ;
document
document
.write(birthday.getDate()) ;
.write(birthday.getDate()) ;
birthday.setDate(25) ;
birthday.setDate(25) ;
document
document
.write(birthday) ;
.write(birthday) ;
40 40
10:34:01
10:34:01 Programmation Web 2012-2013Programmation Web 2012-2013
Tue Nov 02 2010 00:11:36 GMT+0100
Tue Nov 02 2010 00:11:36 GMT+0100
Sun Dec 17 1995 03:24:00 GMT+0100
Sun Dec 17 1995 03:24:00 GMT+0100
Sun Dec 17 1995 00:00:00 GMT+0100
Sun Dec 17 1995 00:00:00 GMT+0100
Sun Dec 17 1995 03:24:00 GMT+0100
Sun Dec 17 1995 03:24:00 GMT+0100
0
0
17
17
Mon Dec 25 1995 03:24:00 GMT+0100
Mon Dec 25 1995 03:24:00 GMT+0100
Dates : Exemples
Dates : Exemples
document
document
.write(birthday.getMonth()) ;
.write(birthday.getMonth()) ;
birthday.setMonth(10) ;
birthday.setMonth(10) ;
document
document
.write(birthday) ;
.write(birthday) ;
document
document
.write(birthday.getYear()) ;
.write(birthday.getYear()) ;
birthday.setYear(96) ;
birthday.setYear(96) ;
document
document
.write(birthday) ;
.write(birthday) ;
document
document
.write(birthday.getFullYear()) ;
.write(birthday.getFullYear()) ;
birthday.setFullYear(2010) ;
birthday.setFullYear(2010) ;
document
document
.write(birthday) ;
.write(birthday) ;
document
document
.write(
.write(
Date
Date
.parse(
.parse(
"Aug 9, 1995"))
"Aug 9, 1995"))
;
;
41 41
10:34:05
10:34:05 Programmation Web 2012-2013Programmation Web 2012-2013
11
11
Sat Nov 25 1995 03:24:00 GMT+0100
Sat Nov 25 1995 03:24:00 GMT+0100
95
95
Mon Nov 25 1996 03:24:00 GMT+0100
Mon Nov 25 1996 03:24:00 GMT+0100
1996
1996
Thu Nov 25 2010 03:24:00 GMT+0100
Thu Nov 25 2010 03:24:00 GMT+0100
807919200000
807919200000
42 42
10:34:10
10:34:10 Programmation Web 2012-2013Programmation Web 2012-2013