Click to See Complete Forum and Search --> : Need a little help please


bparker19
05-15-2003, 09:46 AM
I'm trying to get a script to detect screen resolution and select one of two background images (one for 800x600 and one for 1024x768).
I searched the forum and found a script in another thread that was said to work, but I'm clearly doing something wrong. Could someone take a look at it and offer some helpful feedback? I really don't know much about JS, but I would really like to get this to work.

Here is the script:
__________

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

var Pic = new Array()


Pic[0] = 'stratbkgrd3.gif' //800x600
Pic[1] = 'stratbkgrd1024.gif' //1024x768

if(screen.width==800)
{
document.body.background = Pic[0];
}
else if(screen.width==1024)
{
document.body.background = Pic[1];
}
</script>
____________

I am getting an error on the "document.body.background" part.

Thanks for any help! :)

Vladdy
05-15-2003, 09:52 AM
Before you get any further think about what does screen resolution setting have to do with the size of browser window client area :confused: :rolleyes:
As far as you script goes, a vital piece of information is missing here... where in HTML document did you put it???

bparker19
05-15-2003, 09:56 AM
It's currently in the head section of the page.

Vladdy
05-15-2003, 10:00 AM
That is what I thought, you are accessing document.body when it has not beed created...

bparker19
05-15-2003, 10:05 AM
Ahh...ok. So...let me revise my previous statement. I believe I said that I said that I don't know much about JS, but it should really say that I don't know anything about JS. Could you possibly help me out with this? How do you define it?

I'm sorry--I don't want to waste your time, and I really do appreciate you taking the time to respond.

Thanks,

Bill

Vladdy
05-15-2003, 10:16 AM
First I suggest you reconsider your stratedy, otherwise it's a waste of time. If you want to adjust the size of your background image to fit the page better, you should be concerned with the width of the browser client window, not the resolution of the screen. Not everyone browses in a full screen mode. I, for example, have resolution 1600x1200, but my browser window rarely ecxeeds 800 width.
Second, it seems like you are trying to load a pretty large image as a background. You should consider those on slow dialup connection who will have to wait for your image. The better approach is to let the page load with an appropriate background color, then add an image. This way your visitors have the content to go over while the image is being loaded.
Therefore the code you want to have will be something like
<head>
<script>
funciton addBGImage()
{ if(document.body.offsetWidth<900)
document.body.style.backgroundImage='url("smallBG.gif")';
else
document.body.style.backgroundImage='url("largeBG.gif")';
return;
}
</script>
</head>
<body onload="addBGImage()">