javascript: image aléatoire sélectionnée lors de l’actualisation

Donc, j’ai un site dans lequel j’ai une zone de description et je lui donne une description aléatoire lors de l’actualisation en utilisant le code suivant:

 var description = new Array (); description[0] = "I can change"; description[1] = "Isn't it cool"; description[2] = "these are just to show you guys"; description[3] = "another thing"; var size = description.length var x = Math.floor(size*Math.random()) document.write(description[x]);  

Maintenant, ma question est la suivante: si je voulais que les images aléatoires soient rafraîchies au lieu d’une description aléatoire, comment le ferais-je? Je suppose que cela prendra un peu de temps et peut-être quelques ajouts, mais je ne suis pas vraiment sûr.

Merci!

Que diriez-vous:

HTML:

  

JS:

 var description = [ "http://static.ddmcdn.com/gif/lightning-gallery-17.jpg", "http://static.ddmcdn.com/gif/lightning-gallery-18.jpg", "http://static.ddmcdn.com/gif/lightning-gallery-19.jpg", "http://static.ddmcdn.com/gif/lightning-gallery-20.jpg", "http://static.ddmcdn.com/gif/lightning-gallery-21.jpg" ]; var size = description.length var x = Math.floor(size*Math.random()) document.getElementById('image').src=description[x]; 

Pas besoin de jQuery.

http://jsfiddle.net/mohammadAdil/SvswX/

  

script –

 var image = new Array (); image[0] = "http://placehold.it/20"; image[1] = "http://placehold.it/30"; image[2] = "http://placehold.it/40"; image[3] = "http://placehold.it/50"; var size = image.length var x = Math.floor(size*Math.random()) $('#random').attr('src',image[x]);