Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Subject: javascript background random image

Original Message
Name: BigShow
Date: April 23, 2008 at 10:07:39 Pacific
Subject: javascript background random image
OS: xp
CPU/Ram: pentium
Model/Manufacturer: dell
Comment:
I need to make a script that will randomly choose a background image from 4 different images when the page loads and displays it, everytime it reloads it will be a different image.I think I need to use javascript and create a random number assign it to a variable then create an array of these 4 backgrounds, then create a function that chooses the array with that random number, then in the body have the onload function load it.

Here is what i have, it doesnt work.

any ideas


function get_random()
{
var ranNum= Math.floor(Math.random()*4);
return ranNum;
}

var whichImage = function get_random();

var back=new Array(4)
back[0]="bg1a.jpg";
back[1]="bg2a.jpg";
back[2]="bg3a.jpg";
back[3]="bg4a.jpg";



</script>

</head>
<body onload="javascript:back[whichImage]">


Report Offensive Message For Removal

Response Number 1
Name: BigShow
Date: April 23, 2008 at 11:34:44 Pacific
Subject: javascript background random image
Reply: (edit)
I figured it out, just in case anyone needs the script,

<SCRIPT LANGUAGE="JavaScript">

random_number= Math.floor(Math.random()* 4+1 );
// Loads the appropriate image and text color based on random number.
if (random_number==1) {
image="bg1a.jpg";
}
if (random_number==2) {
image="bg2a.jpg";
}
if (random_number==3) {
image="bg3a.jpg";
}
if (random_number==4) {
image="bg4a.jpg";
}

// End -->
</SCRIPT>
</head>

<body onload="document.body.background = image">
</body>
</html>



Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: javascript background random image 

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software



Version Tracker Pro
Keep your software current and secure, effortlessly

Click Here for a Free Scan

Driver Agent
Automatically find the latest drivers for your computer.
Click Here for a Free Scan



The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC