Bonjour,
je suis un peut surpris d'un petit truc
Dans ma page j'ai ceci:
entre les balise <head> et </head>
ce code js, conbtient la fonction image aleatoire
Code : Sélectionner tout - Visualiser dans une fenêtre à part <script language="javascript" src="include/js/rondom_pictures_after_refresh.js"> </script>
Puis dans ma balise body j'ai ceci
Si je laisse comme ceci ca fonctione très bien
Code : Sélectionner tout - Visualiser dans une fenêtre à part <body onLoad="init();imagealeatoire()">
Par contre si je fait comme ceci
Ca marche plus
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <body onLoad="init()"> <script language="JavaScript"> imagealeatoire(); </script> ..... SUite du code...
Voici ma fonction
Le message d'erreur est le suivant
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 function makeArray(n) { this.length = n; for (var i=1; i<=n; i++) { this[i] = ""; } return this; } //initailise le lien de l'image function init(){ var ImgLink=document.createElement('a'); ImgLink.id='LienImage'; ImgLink.href='#'; ImgLink.style.textDecoration='none'; ImgLink.appendChild(document.getElementById('SlideShow')); document.getElementById('container').appendChild(ImgLink); } var j; function imagealeatoire() { j = Math.floor(3 * Math.random() + 1); document.getElementById("SlideShow").src = "img/research/"+image[j]+".jpg"; document.getElementById('LienImage').href="http://www.domaine.ch?id="+image[j]; document.getElementById('sloganTexte').innerHTML = txt[j]; } image = new makeArray(3); image[1]="30"; image[2]="18"; image[3]="24"; txt = new makeArray(3); txt[1] ='Analyzing the triggering of landslides using Self-Organized Criticality (SOC)' txt[2] ='Sensors development for water content measuring in soil' txt[3] ='Evaporation from porous media'
Mais pourquoi ca marche quand l'appel de la fonction est dans balise <body>, mais elle ne marche plus lorsqu'elle ne l'est plus?????document.getElementById("SlideShow") has no properties
[Break on this error] document.getElementById("SlideShow").src = "img/research/"+image[j]+".jpg";
En fait, mon petit code marche quand l'appel est dans la balise <body>, mais j'ai remarqué que l'image n'est pas changée forcément a tout le refresh. Des fois y a des "coups blanc". Auriez vous une idée comment optimiser ceci?
Merci
Partager