SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to load random background image each day with jquery?

    i've got some code - using jquery & javascript - that loads a random background image into a div element. the code is below. the trouble is, it loads a random image every time the page is loaded, when what i want is to just pick a random image ONCE each day

    anyone any idea of how i'd go about doing this?

    Code:
    <script type="text/javascript">	
       $(document).ready(function() {
          if ($(window).width() > 1200) {		
             var randomImages = ['bgimage0_big', 'bgimage1_big', 'bgimage2_big', 'bgimage3_big'];
             var rndNum = Math.floor(Math.random() * randomImages.length);
             $("div#branding").css({ background: "url(css/images/" + randomImages[rndNum] + ".jpg) no-repeat" }); 
          } else  {
             var randomImages = ['bgimage0_small', 'bgimage1_small', 'bgimage2_small', 'bgimage3_small'];
             var rndNum = Math.floor(Math.random() * randomImages.length); 
             $("div#branding").css({ background: "url(css/images/" + randomImages[rndNum] + ".jpg) no-repeat" }); 
          }
       });
    </script>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    One idea would be that when your page first loads, you could set a cookie which would expire at midnight.
    Then in the above script, add a line so that whenever it is called, it checks for the presence of the cookie.
    If the cookie exists, do nothing. Otherwise, display a random image and set another cookie.
    This should help: http://stackoverflow.com/questions/4...ry-at-midnight

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's perfect, cheers

    [ now just to make brain do the implementation thing ]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •