random div background

Posted on 2008-11-06
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" "">
<html xmlns="" 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];'background-image', bg, null);'color', col, null);


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

    Accepted Solution

    You're not calling the function. Try the following:
    <body onload="randomizeBG()">

    Open in new window


    Author Comment

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

    Author Closing Comment

    My bad, in fact it does work.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now