/    Sign up×
Community /Pin to ProfileBookmark

Need some fresh eyes to figure this one out

I have a script that loads random backgrounds every time the page is reloaded (or refreshed). It works really well — except it randomly produces a BLANK page. I’ve tried to figure it out for the past three weeks. I even passed it through several “javascript code testers” without resolve.
Here is the WHOLE PAGE:

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>REFRESH PAGE TEST</title>
<script type=”text/javascript”>
var backgr = [‘bg0.png’,’bg1.png’,’bg2.png’,’bg3.png’,’bg4.png’,’bg5.png’,’bg6.png’,’bg7.png’,’bg8.png’,’bg9.png’,’bg10.png’,’bg11.png’,’bg12.png’,’bg13.png’];
var cur = Math.round((backgr.length)*Math.random());
document.write(‘<body background=”‘+backgr[cur]+'”>’);
var backgrounds = [‘bg0.png’,’bg1.png’,’bg2.png’,’bg3.png’,’bg4.png’,’bg5.png’,’bg6.png’,’bg7.png’,’bg8.png’,’bg9.png’,’bg10.png’,’bg11.png’,’bg12.png’,’bg13.png’],
bg,
cur = Math.round((backgrounds.length)*Math.random());
document.body.style.backgroundImage=backgrounds[cur];
document.body.style.backgroundPosition=”top center”; // can be changed to anything else (e.g. bottom center, top left, center right…)
document.body.style.backgroundRepeat=”no-repeat”;
</script>
<style type=”text/css”>
<!–
body {background-repeat: no-repeat;}
–>
</style>

</head>
<body onload=”onloadBg”>
Page content….bla, bla, bla
</body>
</html>
[/code]

I’d give you a URL to look at; but, I’m using it to build a DVD for my grandaughter’s Christmas present.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Oct 07.2009 — [LIST]
  • [*]Math.random returns a number between 0 and 1

  • [*]backgr.length returns the lenght of an array

  • [*]An array has an index that starts at 0

  • [/LIST]


    With those points do you see the problem with

    [CODE]backgr[backgr.length * 1]
    [/CODE]


    Eric
    Copy linkTweet thisAlerts:
    @DOW9991authorOct 07.2009 — I still don't see the problem... Please excuse my BLINDNESS
    Copy linkTweet thisAlerts:
    @A1ien51Oct 07.2009 — Run this:
    [CODE]alert(backgr[backgr.length * 1])
    [/CODE]


    Eric
    Copy linkTweet thisAlerts:
    @DOW9991authorOct 08.2009 — I decided just to redo the code; Eric. I made it real simple and can add as many images as I want. I just have to remember to change the value in the "get.ElementById" to 1< than the actual number of images.

    I just got finished testing it and it seems to work just fine...

    Here's the new code:
    <i>
    </i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;script type="text/javascript"&gt;
    MyImages=new Array();
    MyImages = ['bg0.png','bg1.png','bg2.png','bg3.png','bg4.png','bg5.png','bg6.png','bg7.png','bg8.png','bg9.png','bg10.png','bg11.png','bg12.png','bg13.png'];
    function newImage()
    {
    document.getElementById("mainImage").setAttribute("src", MyImages[Math.round(Math.random()*13)])
    }
    &lt;/script&gt;
    &lt;style type="text/css"&gt;
    body {background-repeat:no-repeat;}
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body onload="newImage()";&gt;
    //I used this so JavaScript doesn't report an error
    &lt;img id="mainImage" /&gt; <br/>
    &lt;/body&gt;
    Copy linkTweet thisAlerts:
    @A1ien51Oct 08.2009 — Is it that hard to do
    [CODE]
    var cur = Math.floor((backgr.length-1)*Math.random());[/CODE]



    Eric
    Copy linkTweet thisAlerts:
    @DOW9991authorOct 08.2009 — No... I just thought since I was taking a closer look at it I'd just tweak it a bit here and there and came up with a simplified version...

    Your suggestion works really great, and I appreciate your time and effort in steering me to look in the right place, Eric. Now I have 2 methods for doing what I set out to accomplish...

    Thanks again!
    ×

    Success!

    Help @DOW9991 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...