If I have a directory of pictures and I want a random pic to display on the page, how do I do that in HTML?

3 Answers

  • Zurahn
    Lv 4
    1 decade ago
    Favorite Answer

    You cannot do that with HTML, for a couple reasons.

    1. HTML is not a programming language per se. It does not have methods or conditional statements, it is merely for outputting and outputting alone. If you wanted to do scripting, you'd have to use a client-side language such as JavaScript.

    2. You can't (well, not can't, but for all intents and purposes both shouldn't and would have complications) do file access client-side.

    However, if you have a list of the files, you can do it in JavaScript

    Here is an example:

    <script type="text/javascript">

    var pics = new Array();

    pics[0] = "pic1.jpg";

    pics[1] = "pic2.jpg";

    pics[2] = "pic3.jpg";

    rand = Math.round(Math.random() * (pics.length-1));

    document.write('<img src="' + pics[rand] + '" />');


  • Anonymous
    1 decade ago

    you cant do with html. becoz html is not an language or script.

    for this you have to write any script like javascript, perl, vb, php or asp etc...

  • Anonymous
    1 decade ago

    check out the link below.

Still have questions? Get your answers by asking now.