Drummy's Random Image Script Wizard



What is this?
This JavaScript will allow you to display one of several images on your website/forum/whatever.
Usage?
You enter the URLs of the images the script should choose from in the forum below. Then the Drummy Script Wizard gives you the complete code that you need with all instructions. Simple as that! Good or what? If you like it, please consider donating me a buck or two :)
Price?
FREE! But if you like it, you can donate a buck or two towards my further work here. :)
In order to get the code you need, simply enter the complete URLs (including http://) in the text-box below, one address per line.


Image Border (usually 0 is fine):

51 Responses to “JavaScript Random Image”

  1. jessica Says:

    you’re missing two closing parentheses in the document.write part. other than that, works beautifully. thanks dude.

  2. Drummy Says:

    Hi Jessica,
    thanks for the hint, I’ve updated the code now.
    And thanks for using the code

  3. Alex Says:

    Hey everything looks good. Im not really that experienced with implementing javascript, but I was wondering how do I implement it in the html? know where to put the actual javascript (Im actually linking it externally) but whats the way to reference wherever your’re placing it in the html?

  4. Drummy Says:

    Hi Alex,
    thanks for visiting

    Just place this code where you want the random image to appear, it’s as simple as that.
    So basically anywhere between and .
    I’ll be reworking the script to make it a little prettier. You will have one section to put between and and then one small line of code which you put where the random image should appear.

    If you have any more questions, go ahead and ask or e-mail me: email@drummy.org

  5. Drummy Says:

    I’ve just gone and done it!

  6. Kim Says:

    Nice, nice, nice just doing the trick, works fine, integrates fine in complexe site

  7. Drummy Says:

    Glad you like it

  8. Cristi B Says:

    Man thanks fot this wonderful script!! Good work! Keep it on !

  9. Drummy Says:

    Thanks Cristi, your complement is much appreciated!

  10. Smurfen Says:

    wonderful, just what i was looking for thanks!

  11. Drummy Says:

    Glad to help

  12. Yum Says:

    Is there a way I can add a specific link to each image?

  13. Drummy Says:

    Hi Yum,
    yes sure. Would you want to add one specific link any time a random image is displayed? Or do you want a different link for each image?

    If it’s just one link you want to display, it’s simple.

    Run the wizard on this page and enter the image URLs.
    Then follow step 1.

    In step two instead of

    <script language=”JavaScript”>show_image();</script>

    use:

    <a href=”http://yourlink.com”><script language=”JavaScript”>show_image();</script></a>

    Of course you replace http://yourlink.com with the link you need.
    If you need a different link for each image, let me know. Use the contact form on this site to send me the images and the links and I’ll write you the code.

  14. Yum Says:

    Hi Drummy yes I need a different link for each image displayed. Thanks for the help

  15. Yum Says:

    i’m just on the testing phase so go ahead and use dummy links for now…thanks.

  16. Drummy Says:

    Hi again Yum,
    I’ve put it on a seperate HTML page to make it easier (for me AND you lol).

    Check it out:
    http://www.drummy.org/picswithlinks.html

    If there’s anything that you don’t understand, let me know.

  17. Yum Says:

    thank you i’ll give it a shot…looks easy enough

  18. Ben Says:

    How can you make this work using the javascript unobtrusively? Separating it out of the HTML, and instead calling it in the head of the document

    Then calling it up in the body of the HTML page in one line. But what would the command be to call it into the page body?

    I can’t figure it out.

  19. Drummy Says:

    I’m not sure exactly what you mean. Can you give me an exmaple of what you’re talking about?

  20. Brennan Says:

    I’m trying to use this script on mutiple pages, and have them all have the same images, and allow the images to be easily changed. So I created an external .js file, and linked the script in the html files correctly, but I can’t seem to get it to work, do you have any sugguestions?

  21. Brennan Says:

    o nvm I got it

  22. Drummy Says:

    Glad you’ve worked it out.

  23. Dezz Says:

    Thank you very much for this - it’s great, just what I was looking for!

  24. Thomas Says:

    Hi,
    Is there a way to modify this script a bit to shows several random images on 1 page at the same time without repeat?

  25. Drummy Says:

    Sure. If you’d consider a small donation, I’d be happy to adjust the script accordingly.

    Jet let me know how many instances of the script you need (how many multiple images) and the URLs of the images you want to use.

  26. Drummy Says:

    PS: you can use the contact form to contact me.

  27. Glass Says:

    Thanks dude, works awsome.

  28. abgary Says:

    Thanks dude, it helped me to put a image gallery on sharepoint.

    i changed it a lil to get text on my image

    Imgtxt[1] = “This is a test”;
    document.write(”");
    document.write(Imgtxt[whichImg]);
    var Imgtxt=new Array(7)

    ur code helped a lot, thanks againn

  29. Drummy Says:

    Hey abgary,
    glad to have helped.

  30. Dylan Phelan Says:

    Hi there…

    Just wondering if you ended up adjusting the script so that it can be used a few time on the same page… I’ve tried but to no avail. I’d like to use say three different directories, each with say 3 images.

    Cheers,
    Dylan

  31. Drummy Says:

    Yes, I have done that for a project of mine. I don’t have the code anymore, but I’m sure I could do it again.
    E-mail me the URLs of the three image sets and I’ll get it sorted.

  32. Michele Says:

    Will this allow me to randomize a slideshow? Or is this just for individual images?

    I have about 35 pictures that my boss wants me to put into a randomized slideshow on our homepage. Right now, we have about 20 images on a non-randomized slideshow, but no one ever looks at that particular page for longer than a minute or two. So I’d like to be able to randomize the slideshow so that every time someone goes to our homepage, they see a different slide show. Is that what this code will do?

  33. Drummy Says:

    Nope. This one displays individual images randomly. For what you’re looking for, I’d probably go with using Flash or JavaScript / DHTML.

  34. Matt Says:

    First of all, thanks for this great script. It works great!

    Second, I was wondering if there was a way to randomize the same pictures using different layers on the same html page. For instance, someone goes to the website and they see one picture. They click on a tab to unhide another layer and a different picture is displayed from the same originally picked pics. Additionally, whenever a user clicks on a tab or a created hotspot which unhides a layer, a new randomized picture is displayed. Is this possible?

    Thanks again for the great script as it is now

  35. Rich Says:

    oops…the code got all screwy:


    elseif ($this_is_home_page) //home page
    {
    echo "";
    }
    ?>

  36. Drummy Says:

    You can implement it real easy. Just put it into the echo “”; statement.
    Like this:
    echo "<script….";

    Just make sure that you escape any double-quotes, so it looks a little like this:
    echo "<script language=\"JavaScript\">…";

    see what I mean? If it doesn’t work out, e-mail me through the contact form.

  37. Alex Says:

    Hey Thanks for the script it works great. I was interested in making the images printable. What would i need to add to the script and where to make each random image have the option of being printed when clicked?

  38. Drummy Says:

    Hm. I’m not too familiar with printing via JavaScript. But if you contact me this weekend (because otherwise, I’ll forget), I’ll look into it and see what I can do.

  39. Alex Says:

    thanks let me know if you can help

  40. Drummy Says:

    Hi Alex,
    I have found something. Not tested though.
    First, add the following code between <head> and </head>:

    <script>
    function makepage(src)
    {
    // We break the closing script tag in half to prevent
    // the HTML parser from seeing it as a part of
    // the *main* page.

    return "<html>\n" +
    "<head>\n" +
    "<title>Temporary Printing Window</title>\n" +
    "<script>\n" +
    "function step1() {\n" +
    " setTimeout(’step2()’, 10);\n" +
    "}\n" +
    "function step2() {\n" +
    " window.print();\n" +
    " window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad=’step1()’>\n" +
    "<img src=’" + src + "’/>\n" +
    "</body>\n" +
    "</html>\n";
    }

    function printme(evt)
    {
    if (!evt) {
    // Old IE
    evt = window.event;
    }
    var image = evt.target;
    if (!image) {
    // Old IE
    image = window.event.srcElement;
    }
    src = image.src;
    link = "about:blank";
    var pw = window.open(link, "_new");
    pw.document.open();
    pw.document.write(makepage(src));
    pw.document.close();
    }
    </script>

    Then, in every <img src…> add this bit:
    onClick="printme(event)"

    That should do the trick. But as I said, I haven’t tested this.

  41. Emilio Says:

    Hi,

    I am looking around, without success since now, to find a script for random images from a file, just naming the file, and NOT the pictured in it…
    It seems you are almost expert on such questions…
    would it be possible to help me?

    Thanks in advance

    Emilio

  42. Drummy Says:

    It must be possible to do, using AJAX. But I am far from an expert in that field. If I would want something like that, I would probably use PHP to do it, because I know my way around PHP way better.

  43. Dave Says:

    Did you ever make the script for multiple random images? I’d like to display 6 images from a list of about 10 or 12… in TD table cells with text underneath… and links. :-)

    if i had a basic script i could make it work in the setup I need. I just can’t find how to not duplicate a photo once it’s used once.

    how much? lol paypal?

  44. Drummy Says:

    Yeah I made a script with several instances of the random image. So I could make it again.
    As for how much (lol) - you decide. I don’t ask for money for my script (or reasonable modifications of it), but anyone that uses it may want to donate whatever amount they see fit.

    Did I understand you correctly, you want to have one set pool of random images and then you want 6 instances of the script to display a random image from that set pool of images. Is that correct?

    Send me your image URLs via the contact form and I’ll take it from there.

  45. nick Says:

    hi Drummy

    was just wondering how to set a specific size for all the images without making all the images actually that size

    thanks

  46. Shawn Says:

    This script is great!

    I’d like to add a link to one image but I have all of my images locally as opposed to pointing to them on a website. How would I add the link?

    Thanks!

    var img=new Array(4)
    img[0]=”images/index/top_prweek.jpg”;
    img[1]=”images/index/top_sabre.jpg”;
    img[2]=”images/index/top_impact.jpg”;
    img[3]=”images/index/top_coynexchange.jpg”; >>

  47. Shawn Says:

    Sorry - that last img I want to go to the link http://coynexchange.com in a new window

  48. Drummy Says:

    Hi nick and Shawn - I’m extremely busy right now and likely to forget your questions, lol.
    Could you guys please use the contact feature and send them to me that way?
    That way they land in my mailbox and I won’t forget them.

  49. Joe Says:

    Hey first off thanks for the great script it works great. I was just what I needed for my site. Now I have a quick question I noticed in the scripted that there’s a place to set a border. I set it to 1px and it works great on safari and Firefox, but has no luck with IE. Is there any way I can add a border to this that’ll make it work with IE? I really don’t want to have to put a border on every image I have when I could maybe do it in the code. If you could help me out with this that would be great. Thanks for the help.

  50. Drummy Says:

    Sure, just use CSS. Replace this:
    border=’0′

    With this:
    style=’border:1px solid;’ or something along the lines. Play around with it, till you like it.

  51. Joe Says:

    Nice thanks ya style css code did the trick. For some reason I had to change the apostrophe to one that wasn’t curved which i thought was weird lol. Thanks again for help.

Leave a Reply