Better way to code (1 Viewer)

Oldsoftboss

AWF VIP
Local time
Tomorrow, 07:43
Joined
Oct 28, 2001
Messages
2,499
I have spent today updating our home page (raining outside, nothing much else to do!)

http://members.dodo.com.au/predatorsoftware/index.html

I coded the background page to load a random image each time the page refreshes, but with my access experience, I feel the code is messy and could be better... Any suggestions on the following:


Code:
<script>
<!--
var backgr1="backgrounds/adelaide.jpg"
var backgr2="backgrounds/blackboys.jpg"
var backgr3="backgrounds/cathederal.jpg"
var backgr4="backgrounds/christmas.jpg"
var backgr5="backgrounds/creek.jpg"
var backgr6="backgrounds/dad.jpg"
var backgr7="backgrounds/emmylou.jpg"
var backgr8="backgrounds/fernery.jpg"
var backgr9="backgrounds/fire.jpg"
var backgr10="backgrounds/houseboats.jpg"
var backgr11="backgrounds/jetboat.jpg"
var backgr12="backgrounds/kookaburra.jpg"
var backgr13="backgrounds/muffin1.jpg"
var backgr14="backgrounds/muffin2.jpg"
var backgr15="backgrounds/river1.jpg"
var backgr16="backgrounds/tash1.jpg"
var backgr17="backgrounds/tash2.jpg"
var backgr18="backgrounds/wayne.jpg";

var cur=Math.round(18*Math.random())

if (cur<=1)
backgrnd=backgr1

else if (cur<=2)
backgrnd=backgr2

else if (cur<=3)
backgrnd=backgr3

else if (cur<=4)
backgrnd=backgr4

else if (cur<=5)
backgrnd=backgr5

else if (cur<=6)
backgrnd=backgr6

else if (cur<=7)
backgrnd=backgr7


else if (cur<=8)
backgrnd=backgr8


else if (cur<=9)
backgrnd=backgr9


else if (cur<=10)
backgrnd=backgr10


else if (cur<=11)
backgrnd=backgr11


else if (cur<=12)
backgrnd=backgr12


else if (cur<=13)
backgrnd=backgr13


else if (cur<=14)
backgrnd=backgr14


else if (cur<=15)
backgrnd=backgr15


else if (cur<=16)
backgrnd=backgr16


else if (cur<=17)
backgrnd=backgr17

else
backgrnd=backgr18

document.write('<body background="'+backgrnd+'" bgcolor="#FFFFFF">')

I feel I should be able to combine the "cur" with the "backgr" rather than using the long winded if statement.

Dave
 

Mile-O

Back once again...
Local time
Today, 22:43
Joined
Dec 10, 2002
Messages
11,316
I would suggest renaming your picture files so that they are simply numeric (1.jpg, 2.jpg., etc.)

You can then just run with two lines.

Code:
var cur = Math.round(18*Math.random());
document.write('<body background="backgrounds/'+cur+'.jpg" bgcolor="#ffffff">')
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 07:43
Joined
Oct 28, 2001
Messages
2,499
Thanks MileO

Thats what I was looking for,

Dave
 

dan-cat

Registered User.
Local time
Today, 22:43
Joined
Jun 2, 2002
Messages
3,433
I never use filenames in this way to shoehorn the desired outcome. Eventually you'll come across a situation where you'll need to keep the filename intact. Consider this approach:

Code:
function getRandomImage(index)
{
// take into account zero-indexing if needed
index = index - 1;
//build the array of image files
var myImages = ['dog.jpg','cat.jpg','mouse.jpg'];
//return item from array with parsed index
return myImages[index];
}

and in your output:

Code:
var cur = Math.round(18*Math.random());
document.write('<body background="backgrounds/'+getRandomImage(cur)+' bgcolor="#ffffff">')
 

Mile-O

Back once again...
Local time
Today, 22:43
Joined
Dec 10, 2002
Messages
11,316
The only big issue now, Boss, is that the pictures are rather large and affect the loading time of the page.
 

Minkey

Registered User.
Local time
Today, 21:43
Joined
Jul 7, 2004
Messages
661
Not to be too critical but you have the pictures set as the background image so if you view your site at 1024x768 the picture is fine, however if you view it at 1280x1024 (my main monitor resolution) it tiles which to be honest doesn't look that good.

You might want to concider placing the images in a frame so they don't tile at higher resolutions.
 

dan-cat

Registered User.
Local time
Today, 22:43
Joined
Jun 2, 2002
Messages
3,433
Code:
<body style="background-repeat: no-repeat">
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 07:43
Joined
Oct 28, 2001
Messages
2,499
Minkey said:
Not to be too critical but you have the pictures set as the background image so if you view your site at 1024x768 the picture is fine, however if you view it at 1280x1024 (my main monitor resolution) it tiles which to be honest doesn't look that good.

You might want to concider placing the images in a frame so they don't tile at higher resolutions.

My other computer has 1280x1024 resolution, so I also noticed the tiling issue.

Most of the images are under 200Kb, and as they will probably only ever viewed on our home computers, our broadband plan is high enough to alow them to load quickly. (around 5-6 sec), so to me thats not an issue.

I plan to make all the resolutions 1280x1024

Dave
 

Minkey

Registered User.
Local time
Today, 21:43
Joined
Jul 7, 2004
Messages
661
Oldsoftboss said:
they will probably only ever viewed on our home computers, our broadband plan is high enough to alow them to load quickly. (around 5-6 sec), so to me thats not an issue.

Ah ok but why use a hosted web server then - if you only going to use them at home (I assume you have a home network in place) then you could simply share the folder you have the files in and everyone can have a shortcut the the index.html file ?

Just a thought means you dont have to worry about loading times, host being down, uploading updates etc and of course you could give each member of your family their own sub site or different access rights much more easily.

As I said just a thought might make it easier ;)
 

Oldsoftboss

AWF VIP
Local time
Tomorrow, 07:43
Joined
Oct 28, 2001
Messages
2,499
Minkey said:
Ah ok but why use a hosted web server then - if you only going to use them at home (I assume you have a home network in place) then you could simply share the folder you have the files in and everyone can have a shortcut the the index.html file ?

Just a thought means you dont have to worry about loading times, host being down, uploading updates etc and of course you could give each member of your family their own sub site or different access rights much more easily.

As I said just a thought might make it easier ;)

Yes, but no.

When running java script from the local pc (C:\Web Site\index.html) every time the page is loaded, you get a warning saying that the page contains active content - and Do you want to load? etc
Pain in the ass

Dave
 

Users who are viewing this thread

Top Bottom