View Full Version : Better way to code


Oldsoftboss
01-19-2007, 09:55 PM
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:


<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
01-23-2007, 03:53 AM
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.

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

Oldsoftboss
01-23-2007, 10:46 AM
Thanks MileO

Thats what I was looking for,

Dave

Oldsoftboss
01-23-2007, 11:04 PM
And it works !!!

dan-cat
01-24-2007, 03:15 AM
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:

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:

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

Mile-O
01-24-2007, 03:18 AM
The only big issue now, Boss, is that the pictures are rather large and affect the loading time of the page.

Minkey
01-24-2007, 09:39 AM
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
01-24-2007, 04:58 PM
<body style="background-repeat: no-repeat">

Oldsoftboss
01-25-2007, 06:05 PM
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
01-25-2007, 10:36 PM
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
01-27-2007, 12:50 PM
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