Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

index.html

5 réponses
Avatar
ANDBRETON
Bonjour,

j'ai =E9crit une page index.html dans l'espoir d'afficher une photo
al=E9atoirement =E0 chaque chargement ou rafraichissement.
Je suis chez free, disons : http://toto.free.fr/

si je tape http://toto.free.fr/ : tout va bien, une photo s'affiche.
si je tape http://toto.free.fr/index.html : pas de photo, seulement le
texte de la balise alt.

voici le code :

...
<script language=3D"JavaScript">
<!--
function randomImg(){
var tabImg =3D new Array();
tabImg[0] =3D "gal/a016.jpg";
tabImg[1] =3D "gal/a120.jpg";
tabImg[2] =3D "gal/a135.jpg";
tabImg[3] =3D "gal/a022.jpg";
tabImg[4] =3D "gal/a007.jpg";
document.getElementById('image').src +=3D tabImg[Math.round
(Math.random()*4)];
}
//-->
</script>
</head>
<body onload=3D"randomImg()">
<img src=3D"" alt=3D"Photos" id=3D"image" />
</body>
...

o=F9 me suis fourvoy=E9??

Merci

5 réponses

Avatar
Olivier Miakinen
Le 17/06/2009 08:21, ANDBRETON a écrit :

[...]

si je tape http://toto.free.fr/ : tout va bien, une photo s'affiche.
si je tape http://toto.free.fr/index.html : pas de photo, seulement le
texte de la balise alt.

[...]

tabImg[4] = "gal/a007.jpg";
document.getElementById('image').src += tabImg[Math.round(Math.random()*4)];

[...]

où me suis fourvoyé??



Je parie que l'attribut src prend par défaut l'URL de la page, et que
donc faire « += » donne la chose suivante selon l'URL saisie :
http://toto.free.fr/
-> http://toto.free.fr/gal/aNNN.jpg
http://toto.free.fr/index.html
-> http://toto.free.fr/index.htmlgal/aNNN.jpg

--
Olivier Miakinen
Avatar
ANDBRETON
On 17 juin, 09:04, Olivier Miakinen <om+ wrote:
Le 17/06/2009 08:21, ANDBRETON a écrit :



> [...]

> si je tapehttp://toto.free.fr/: tout va bien, une photo s'affiche.
> si je tapehttp://toto.free.fr/index.html: pas de photo, seulement le
> texte de la balise alt.

> [...]

>    tabImg[4]  = "gal/a007.jpg";
>    document.getElementById('image').src += tabImg[Math.round(Math .random()*4)];

> [...]

> où me suis fourvoyé??

Je parie que l'attribut src prend par défaut l'URL de la page, et que
donc faire « += » donne la chose suivante selon l'URL saisie :
 http://toto.free.fr/
  ->http://toto.free.fr/gal/aNNN.jpg
 http://toto.free.fr/index.html
  ->http://toto.free.fr/index.htmlgal/aNNN.jpg

--
Olivier Miakinen



Merci

Effectivement, je viens de le vérifier avec FireBug

Une solution ??
Avatar
Bruno Desthuilliers
ANDBRETON a écrit :
On 17 juin, 09:04, Olivier Miakinen <om+ wrote:
Le 17/06/2009 08:21, ANDBRETON a écrit :



[...]
si je tapehttp://toto.free.fr/: tout va bien, une photo s'affiche.
si je tapehttp://toto.free.fr/index.html: pas de photo, seulement le
texte de la balise alt.
[...]
tabImg[4] = "gal/a007.jpg";
document.getElementById('image').src += tabImg[Math.round(Math.random()*4)];
[...]
où me suis fourvoyé??


Je parie que l'attribut src prend par défaut l'URL de la page, et que
donc faire « += » donne la chose suivante selon l'URL saisie :
http://toto.free.fr/
->http://toto.free.fr/gal/aNNN.jpg
http://toto.free.fr/index.html
->http://toto.free.fr/index.htmlgal/aNNN.jpg




Merci

Effectivement, je viens de le vérifier avec FireBug

Une solution ??



Heu.... Utiliser des chemins absolu pour les images, et une affectation
simple au lieu d'une affectation augmentée ?

tabImg[4] = "/gal/a007.jpg";
document.getElementById('image').src = tabImg[Math.round(Math.random()*4)];
Avatar
ANDBRETON
On 17 juin, 12:34, Bruno Desthuilliers <bruno.
wrote:
ANDBRETON a écrit :



> On 17 juin, 09:04, Olivier Miakinen <om+ wrote:
>> Le 17/06/2009 08:21, ANDBRETON a écrit :

>>> [...]
>>> si je tapehttp://toto.free.fr/:tout va bien, une photo s'affiche.
>>> si je tapehttp://toto.free.fr/index.html:pas de photo, seulement le
>>> texte de la balise alt.
>>> [...]
>>>    tabImg[4]  = "gal/a007.jpg";
>>>    document.getElementById('image').src += tabImg[Math.round(Ma th.random()*4)];
>>> [...]
>>> où me suis fourvoyé??
>> Je parie que l'attribut src prend par défaut l'URL de la page, et qu e
>> donc faire « += » donne la chose suivante selon l'URL saisie :
>>  http://toto.free.fr/
>>   ->http://toto.free.fr/gal/aNNN.jpg
>>  http://toto.free.fr/index.html
>>   ->http://toto.free.fr/index.htmlgal/aNNN.jpg

> Merci

> Effectivement, je viens de le vérifier avec FireBug

> Une solution ??

Heu.... Utiliser des chemins absolu pour les images, et une affectation
simple au lieu d'une affectation augmentée ?

tabImg[4]  = "/gal/a007.jpg";
document.getElementById('image').src = tabImg[Math.round(Math.random()* 4)];



Super,

Merci.
Avatar
Dr J R Stockton
En fr.comp.lang.javascript
0yqd.googlegroups.com>, 16 Juin 2009 23:21:56, ANDBRETON
a ecrit:

function randomImg(){
var tabImg = new Array();
tabImg[0] = "gal/a016.jpg";
tabImg[1] = "gal/a120.jpg";
tabImg[2] = "gal/a135.jpg";
tabImg[3] = "gal/a022.jpg";
tabImg[4] = "gal/a007.jpg";
document.getElementById('image').src += tabImg[Math.round
(Math.random()*4)];
}



AUSSI :

function randomImg(){
var tabImg = ["a016", "a120", "a135", "a022", "a007"]
document.getElementById('image').src += // += ?
"gal/" + tabImg[Math.floor(Math.random()*5)] + ".jpg"
}

pour l'égalité des probabilités et aussi pour la brièveté.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.