Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random background image onLoad works 70% of the time. need 110%

Status
Not open for further replies.

tellersix

Programmer
Oct 19, 2003
23
US
here is the script that almost works great. i am hoping something is wrong within the code, if not, is there a better way to achieve a different random background image on each load of the page. (i was also curious if the code might not be working due to the browser's inability to weed out that section of code, is there a way to skip to a line or atribute a specific line with a higher level of priority. thanks!

<script LANGUAGE=&quot;Javascript&quot;><!--
function bkgrnd() {} ; bg = new bkgrnd () ; n = 0

bg[n++] = &quot;back/draw/back001.gif&quot;
bg[n++] = &quot;back/draw/back002.gif&quot;
bg[n++] = &quot;back/draw/back003.gif&quot;
bg[n++] = &quot;back/draw/back004.gif&quot;
bg[n++] = &quot;back/draw/back005.gif&quot;
bg[n++] = &quot;back/draw/back006.gif&quot;

rnd = Math.floor(Math.random() * n) ;
tmp = '<BODY BACKGROUND=&quot;' +bg[rnd]+ '&quot;'
+' BGCOLOR=&quot;#FFFFFF&quot;>'
//--></SCRIPT>
</head>
<script LANGUAGE=&quot;Javascript&quot;><!--
document.write( tmp )
//--></SCRIPT>
 
this must work...

Known is handfull, Unknown is worldfull
 
Yep, its working fine for me (amended code to test it):
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<style type=&quot;text/css&quot;>
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
function bkgrnd() {} ; bg = new bkgrnd () ; n = 0;

bg[n++] = &quot;back/draw/back001.gif&quot;;
bg[n++] = &quot;back/draw/back002.gif&quot;;
bg[n++] = &quot;back/draw/back003.gif&quot;;
bg[n++] = &quot;back/draw/back004.gif&quot;;
bg[n++] = &quot;back/draw/back005.gif&quot;;
bg[n++] = &quot;back/draw/back006.gif&quot;;

function getRandom(){
	rnd = Math.floor(Math.random() * n);
	tmp = 'rnd was: ' + rnd + ', background is:' + bg[rnd];
	return tmp;
}
//-->
</script>
</head>
<body>
<input type=&quot;button&quot; value=&quot;get random bg&quot; onclick=&quot;alert(getRandom());&quot; />
</form>
</body>
</html>

What kind of error are you getting 30% of the time?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
thanks for replying, and testing my code i apreciate it. i am not getting error codes, 30% of the time the image is not loading and i am getting a blank white background. you can see what i am saying at my site very much a work in progress but you can see what i am saying. thanks!
 
try going to and refreshing a bunch of times - didn't get a blank bg once.

I did get it almost every time when viewing same page in your forced pop up window, so I guess there must be some sort of script or directory conflict or something.

The solution's easy though: don't use the popup. It's painful, annoying, and will cause more harm (folks not looking at ur site) than good (folks constrained to the dimensions you have designed to).

I'm certain there is a way to achieve your fixed background / design decision without using a popup or window resize. Try starting a thread in the HTML & CSS forum if you're stuck.

Here's some reading to convince you :):
and its sidebar:
Good luck!

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
to be honest, i am happy to admit that my mistake was very stupid, if it means my problem is solved, and it is. thank you very much to everyone that replied. b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top