SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Random Image

    Hello all,

    I need some help with a rotating image. What I want is the image to rotate only when the page initially loads.

    Here is the page. The image is next to the main section of text in the center.
    http://www.atlantaantiquegallery.com

    Script link:
    http://www.atlantaantiquegallery.com/imageRotate.js

    What is happening now is the image is swapping after the page loads. I want the image to load randomly as the page loads.

    Any ideas on what I did wrong in the script?

    Thanks in advance.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is happening because the window's "load" event only happens after absolutely everything has loaded, including images. So that's why you see the image that's in the hard-coded HTML first, and then the random image appears after everything has loaded.

    This would be MUCH better if you do it on the server side. It would be trivial with PHP:

    PHP Code:
    shuffle($arr); // $arr is the array of images, like the one in your JS
    echo '<img src="' $arr[0][0] . '" alt="' $arr[0][1] . '">'
    Then you would see no flicker. If you insist on doing it with javascript, you would need to place the javascript in the <body> just before the <img> tag (not nice) or using these events which happen before window.onload.

    I'd definitely go with doing it on the server side.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Raffles,

    I am going the server side with some php i found on the web also. I am a .net programmer so I am always a little weary about messing with server side code from another language I do not fully understand.

    http://www.atlantaantiquegallery.com/index3.php

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Much better.


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
  •