random div background

Posted on 2008-11-06
Medium Priority
Last Modified: 2012-06-27
Hello, I need some help getting a random background for a div. I found the following javascript below but it is not working. I want the image to change on page reload. What is wrong with the code?

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<title>Untitled page</title>
<style type="text/css">
#midimage {
     height: 400px;
     width: 400px;
     margin: 0px;
     padding: 0px;
     float: left;
     background-image: url(one.jpg);
       color: white;
<script type="text/javascript">
    var bgs = new Array('one.jpg', 'two.jpg', 'three.jpg', 'four.jpg');
    var colors = new Array('#000000', '#FFFFFF', '#CCCCCC', '#FF0000'); //Corresponds to the entry at the same position in the bgs array.
    function randomizeBG() {
        var el = document.getElementById('midimage');
        var i = Math.floor(Math.random() * bgs.length);
        var bg = "url(" + bgs[i] + ")";
        var col = colors[i];
        el.style.setProperty('background-image', bg, null);
        el.style.setProperty('color', col, null);


<div id="midimage">&nbsp;</div>
Question by:gogetsome
  • 2
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 22902502
You're not calling the function. Try the following:
<body onload="randomizeBG()">

Open in new window


Author Comment

ID: 22904524
Sorry, but that did not work. The page error message  = error: Object doesn't support this property or method.

Author Closing Comment

ID: 31514235
My bad, in fact it does work.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question