Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Simple Image Rotation (JavaScript)


There are many times when you will need a simple no-fuss image rotation script that randomly displays a single image from a pre-defined image collection.  Banner rotation scripts are usually used for the larger sites, but these can be cumbersome and totally un-necessary when all you want to do is switch between a few images and don't need them to be shown with advanced features such as sequential access, stat-logging, etc.   The following tutorial details a simple JavaScript method of achieving this goal, and should work in all client browsers that support JavaScripting (i.e. 99.5% of browsers in use today).

To get started, simply copy & paste the following code into your page where you want the random images to be displayed:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var how_many_ads = 2;
var now = new Date()
var sec = now.getSeconds()
var ad = sec % how_many_ads;
ad +=1;
if (ad==1) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}
if (ad==2) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}
document.write('<a href=\"' + url + '\" target=\"_blank\">');
document.write('<img src=\"' + banner + '\" width=')
document.write(width + ' height=' + height + ' ');
document.write('alt=\"' + alt + '\" border=0></a>');
// End -->
</SCRIPT>

This code may look complex at first, but look at it a little closer and you'll realize that it is in fact stupendously easy to comprehend.  To add more banners into the rotation find the bit of code with the variable that says  how_many_ads = 2; and change the number 2 to the desired number of images in your rotation.

Now look for the code below - you will see it repeated twice, with with a different 'ad' value:

if (ad==1) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";

Now all you need to do is change the information in this code chunk to reflect your new image data.  For example, if you want four images in the rotation, your code will now look as follows (areas in bold show the alterations):

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var how_many_ads = 4;
var now = new Date()
var sec = now.getSeconds()
var ad = sec % how_many_ads;
ad +=1;
if (ad==1) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}
if (ad==2) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}
if (ad==3) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}
if (ad==4) {
url="URL GOES HERE";
alt="ALT TEXT";
banner="IMAGE URL HERE";
width="NUMBER";
height="NUMBER";
}

document.write('<a href=\"' + url + '\" target=\"_blank\">');
document.write('<img src=\"' + banner + '\" width=')
document.write(width + ' height=' + height + ' ');
document.write('alt=\"' + alt + '\" border=0></a>');
// End -->
</SCRIPT>

Now all you have to do is enter in the values, upload the changes to a site and open it in your browser - its as simple as that!   Just remember that the URLs in the image settings *MUST* be qualified properly (i.e. http://www.yourdomain.com/images/yourimage.jpg), and state height/width in pixels.   If you have any problems just comment in this thread and I will try my best explain it a little more.   Enjoy!

- Tutorial written by allstar

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  hateregistering (#57936)
Date: Fri Nov 20, 2009. 19:15:44

Post #1 of 1

very nice tutorial.

now can you do one or explain further how to make this into a slideshow - i.e. it rotates the images automatically without having to refresh the page.

thanks

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

Warps & Vortexes
Warps & Vortexes
- Adobe Photoshop -
Custom Shapes
Custom Shapes
- Adobe Photoshop -
'Out Of Bounds' Im...
'Out Of Bounds' Im...
- Adobe Photoshop -
A Simple Flash Gal...
A Simple Flash Gal...
- Macromedia Flash -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 867 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1037 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1042 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1919 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1816 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14417 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2712 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2753 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3427 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2344 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2484 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2792 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673