Alternating backgrounds?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Alternating backgrounds?

 
0
  #11
Jul 28th, 2005
Actually, using this type of mechanism, the images are loaded on demand....

For an example of ondemand loading of images, check out this page: http://homepages.ihug.co.nz/~jlittle/imgs/images.html

You'll see images are loaded when you click on them.. and if your connection is slow enough, you should also see a please wait message (though I've found this doesn't always seem to work so well in IE).

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <script type="text/javascript">
  4. var images = new Array("a.png", "b.png", "c.png");
  5.  
  6. function randomImageSelector(){
  7. var imageNode = document.getElementById("randomImage");
  8.  
  9. var index = Math.floor(Math.random() * images.length);
  10. imageNode.src = images[index];
  11. imageNode.alt = images[index];
  12. }
  13.  
  14. window.onload = function(){
  15. randomImageSelector();
  16. };
  17. </script>
  18.  
  19. <body>
  20.  
  21. <img src="placeHolder.png" id="randomImage"/>
  22.  
  23. </body>
  24. </html>

A way of 'always' getting a different image is to implement a time based seed... this can be calculated in to avoid random providing the same image each load... a large set of images to choose from will also help.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Alternating backgrounds?

 
0
  #12
Jul 28th, 2005
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <script type="text/javascript">
  4. var images = new Array("http://homepages.ihug.co.nz/~jlittle/imgs/NZULtWt3rdOpen8.jpg",
  5. "http://homepages.ihug.co.nz/~jlittle/imgs/OpenMensRace.jpg",
  6. "http://homepages.ihug.co.nz/~jlittle/imgs/vic_snr_8_2003.jpg",
  7. "http://homepages.ihug.co.nz/~jlittle/imgs/vic_8_2_2003.jpg");
  8.  
  9. function randomImageSelector(){
  10. var imageNode = document.getElementById("randomImage");
  11.  
  12. //var index = Math.floor(Math.random() * images.length);
  13.  
  14. // getTime returns number of ms since 1/1/1970 - regardless of how you tell the time.
  15. // since my image set here is quite small, this doesn't really provide a much different
  16. // response than the random implementation.
  17.  
  18. // to get a better result, you could try getSeconds or even getMinutes...
  19. // these return a number between 0-59 ...
  20. var index = (new Date().getTime())%images.length;
  21.  
  22. imageNode.src = images[index];
  23. imageNode.alt = images[index];
  24.  
  25. document.getElementById("ms").innerHTML = index;
  26. }
  27.  
  28. window.onload = function(){
  29. randomImageSelector();
  30. };
  31. </script>
  32.  
  33. <body>
  34.  
  35. <img src="" id="randomImage"/>
  36.  
  37. <div id="ms"></div>
  38.  
  39. </body>
  40. </html>
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Alternating backgrounds?

 
0
  #13
Jul 28th, 2005
Wonderful! Thanks for the posts, a_b.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Alternating backgrounds?

 
0
  #14
Jul 28th, 2005
you could even do away with the need for the array if you named your images sequentially. Using alpha_foobar's code to start, let's say you had 4 images. Name them 0.png, 1.png, 2.png and 3.png

change
var index = (new Date().getTime())%images.length;
to
var index = (new Date().getTime())%4;

and change
imageNode.src = images[index];
to
imageNode.src = index + ".png";

Then when you want to add another picture to the list, just name it 4.png and change
var index = (new Date().getTime())%4;
to
var index = (new Date().getTime())%5;
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 1
Reputation: sjaak is an unknown quantity at this point 
Solved Threads: 0
sjaak sjaak is offline Offline
Newbie Poster

Re: Alternating backgrounds?

 
0
  #15
Aug 28th, 2006
Originally Posted by alpha_foobar View Post
Actually, using this type of mechanism, the images are loaded on demand....

For an example of ondemand loading of images, check out this page: http://homepages.ihug.co.nz/~jlittle/imgs/images.html

You'll see images are loaded when you click on them.. and if your connection is slow enough, you should also see a please wait message (though I've found this doesn't always seem to work so well in IE).

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <script type="text/javascript">
  4. var images = new Array("a.png", "b.png", "c.png");
  5.  
  6. function randomImageSelector(){
  7. var imageNode = document.getElementById("randomImage");
  8.  
  9. var index = Math.floor(Math.random() * images.length);
  10. imageNode.src = images[index];
  11. imageNode.alt = images[index];
  12. }
  13.  
  14. window.onload = function(){
  15. randomImageSelector();
  16. };
  17. </script>
  18.  
  19. <body>
  20.  
  21. <img src="placeHolder.png" id="randomImage"/>
  22.  
  23. </body>
  24. </html>

A way of 'always' getting a different image is to implement a time based seed... this can be calculated in to avoid random providing the same image each load... a large set of images to choose from will also help.
******************************
I understand your answer is from quit a while ago, but since I'm new on this forum I'm responding just now. I too want to have alternating pictures when reloading in a non-server-based way, but realy alternating and no repeating of the same image. You write about a time based seed. Can you tell me more about that. Or is ther a totally different way to do it. After all, your method IS randomizing, and not alternating!
Thanx, Sjaak
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC