PDA

View Full Version : random background image error


surreal5335
07-24-2008, 09:29 PM
I got this code from a tutorial, but when I load it, it tells that:

document.write( tmp ) is not defined. which is found in the <body>

I dont particularly feel this is correct, but also not sure what it should look like?


<head>
<<script language="javascript" type="text/javascript">

function bkgrnd() { ; bg = new bkgrnd () ;
n = 0
bg[n++] = "http://i95.photobucket.com/albums/l136/surreal5335/RE%20site/house6.png"
bg[n++] = "http://i95.photobucket.com/albums/l136/surreal5335/RE%20site/house1.png"
bg[n++] = "http://i95.photobucket.com/albums/l136/surreal5335/RE%20site/house3.png"
bg[n++] = "http://i95.photobucket.com/albums/l136/surreal5335/RE%20site/house4.png"
bg[n++] = "http://i95.photobucket.com/albums/l136/surreal5335/RE%20site/house5.png"

rnd = Math.floor(Math.random() * n) ;
tmp = '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'
}
</script>
</head>


<body>
<script language="javascript" type="text/javascript">

document.write(tmp)

</script>
</body>


Also is there a way to allow css to control the positioning and so forth in the body section? Idealy in a <table> tag

Thanks a lot

rnd me
07-24-2008, 09:50 PM
you need to run bkgrnd() first to create the tmp variable...'



also, n should not be 0, but bg.length

surreal5335
07-24-2008, 10:11 PM
so would that look like this?


document.write(bkgrnd(tmp))


Or would I need quotation marks as well?

rnd me
07-24-2008, 10:22 PM
bkgrnd()
document.write(tmp)

surreal5335
07-24-2008, 11:18 PM
I have made thechanges now it seems I am getting this error:

too much recursion
bg = new bkgrnd ()

Not entirely sure what it means

rnd me
07-24-2008, 11:55 PM
I have made thechanges now it seems I am getting this error:

too much recursion
bg = new bkgrnd ()

Not entirely sure what it means

re-write time:


<head>
<script language="javascript" type="text/javascript">

function bkgrnd() {
var bg = [];
var n=0;

var base = http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'
}
</script>
</head>


<body>
<script language="javascript" type="text/javascript">

document.write( bkgrnd() )

</script>
</body>

surreal5335
07-25-2008, 04:14 PM
Thanks a lot that got it to work great! One question, how would I go about using css to control its positioning, scrolling, repeat, and so forth?

Can I simply plug in the function into the style="background-image url(\'document.write( bkgrnd() )\')?

Im guessing not seeing as I need to have javascript tags surrounding the function in order to work.

Any suggestions?

rnd me
07-26-2008, 01:37 AM
the simplest way is to use CSS classes describing the styles you want. you can then change the class in javascript, thus applying all the styles in one call.

example:
<style>
.boldRed { font-weight-bold; color: red; }
</style>


<script>

document.body.className = "boldRed";
</script>

surreal5335
07-29-2008, 02:31 PM
Thanks for all your help in the matter.

I have made all the changes but for some reason, The <script> in the <body> tag is not showing the background image.

I am not getting any errors right now.

This is what I have:


function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return '<body style="background: #fff url(\' + bg[rnd] + \') no-repeat fixed 80% 100%">'


}

</script>
</head>
<html>
<body>

<script language="javascript" type="text/javascript">

document.write( bkgrnd() )

</script>

rangana
07-30-2008, 04:23 AM
Remove highlighted:

<body style="background: #fff url(\' + bg[rnd] + \') no-repeat fixed 80% 100%">

surreal5335
07-30-2008, 04:57 PM
I tried that, but one thing I should point out is I am echoing this code with php using single quotes. I was told that the ' needed to be escaped using "\" I have made changes to code and now I am getting a parse error, so it sounds like that php is having problems again.

Here is what I have now:

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/surreal/public_html/jsheadtag.php on line 54


<?php
echo'
function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

/*line 54*/

return \'<body style="background: #fff url(\\' + bg[rnd] + \\') no-repeat fixed 80% 100%">\'


}


</script>';
?>

rangana
07-31-2008, 01:12 AM
From the snippet, I should say you missed to have an open <script> tag.
Yes, the quotes are erroneous, should be this:

<?php echo'<script type="text/javascript">
function bkgrnd() {
var bg = [];
var n=0;
var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";

var rnd = Math.floor(Math.random() * bg.length) ;
return \'<body style="background: #fff url(\' + bg[rnd] + \') no-repeat fixed 80% 100%">\'
}
</script>';
?>


Hope that helps.