changer l'image en javascript
Bonjour,
J'essaye de changer une image lorsque la page s'affiche en javascript. J'ai suivi un tuto mais cela ne fonctionne pas...
Je comprends pas...
Code:
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 38 39 40 41 42 43 44
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans nom</title>
<script type="text/javascript">
function changerImage(img, src)
{
// on crée l'objet
var image = new Image();
// événements : cas d'erreur
image.onerror = function()
{
alert("Erreur lors du chargement de l'image");
}
image.onabort = function()
{
alert("Chargement interrompu");
}
image.src = src;
// événement : une fois le chargement terminé
image.onload = function()
{
img.src = image.src;
img.width = image.width;
img.height = image.height;
}
// on modifie l'adresse de l'objet "image", ce qui lance le chargement
image.src = src;
}
var monImage = document.getElementById("monImage");
changerImage(monImage, "image2.png");
</script>
</head>
<body>
<img id="monImage" src="image.png"/>
</body>
</html> |