Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Image that changes with every refresh?  (Read 3043 times)

0 Members and 1 Guest are viewing this topic.

Wuai Bel Ree

    Topic Starter


    Beginner

    Image that changes with every refresh?
    « on: April 02, 2012, 10:58:57 AM »
    I want a banner that changes to a different image each time the page is loaded. How do I get this?

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Image that changes with every refresh?
    « Reply #1 on: April 11, 2012, 07:06:48 AM »
    That is commonly done in JavaScript. Go to your favorite JavaScript hangout  and look for one that does it on refresh. There are many. Just Google it.

    Something like that can be done in PHP
    PHP Random Image Rotation

    Rob Pomeroy



      Prodigy

    • Systems Architect
    • Thanked: 124
      • Me
    • Experience: Expert
    • OS: Other
    Re: Image that changes with every refresh?
    « Reply #2 on: April 13, 2012, 09:40:17 AM »
    Here's a javascript solution I've used on one of my sites.  It uses MooTools - so you need to load that library first.

    Code: [Select]
    // Arrange random artwork - MooTools must already be loaded
    baseUrl = "http://some.domain.com/images/random/"; // web URL to the directory containing the images

    var choices = [
                   "image1.png",
                   "image2.png",
                   "image3.png",
                   "image4.png",
                   "image5.png"
                  ]; // an array containing all the images to be used in the random selection

    var artworkLocations = $$('.randomimg'); // this is the class name to apply to the div that will hold the image
    $each(artworkLocations, function(value, key){
      value.style.backgroundImage = "url("+ baseUrl + choices.getRandom()+")";
    });

    And the HTML:

    Code: [Select]
    <div class="randomimg"></div>
    Only able to visit the forums sporadically, sorry.

    Geek & Dummy - honest news, reviews and howtos