Set background due to screen resolution

H

hippo

Hi,

What i would like to have is the following:
User comes on page and script detects what resolution user have.
Due to screen resolution the background image of the page is changed.

I try the follwing but i an errors all the time (error: object
required). This is strange, without the if statement and implement the
code in a function the code works.

Got anyone a clue?

<html>
<head>
<title>mysample</title>
<SCRIPT language="JavaScript">
<!--
if (screen.width == 1024) {
document.body.style.backgroundImage='url(myimage.jpg)';
}
//-->
</SCRIPT>
</head>
<body>
blablabla
</body>
</html>
 
F

Fred Oz

hippo wrote:
[...]
I try the follwing but i an errors all the time (error: object
required). This is strange, without the if statement and implement the
code in a function the code works.

In which browser on what platform? It works in Safari, IE,
Firefox and Netscape on Mac (but not Opera 6).
Got anyone a clue?

The if statement is likely the issue - have you seen what
screen.width actually returns? Have you tried:

if ( self.screen.width == 1024) {


Some issues totally unrealted to your problem:

[...]
<SCRIPT language="JavaScript">

<script type="text/javascript">

The language attribute is depreciated, type is required.

Don't bother hiding scripts.
 
H

hippo

I do this on a winxp, ie6.0

I changed my code to:

<script type="text/javascript">
if ( self.screen.width == 1024) {
document.body.style.backgroundImage='url(terrein.jpg)';
}
</script>

and still the error: object required.
my screen is 1024. if i do any other value, no error occurs, but also
nothing happens.

any other clues?
 
F

Fred Oz

hippo said:
I do this on a winxp, ie6.0

I changed my code to:

<script type="text/javascript">
if ( self.screen.width == 1024) {
document.body.style.backgroundImage='url(terrein.jpg)';
}
</script>

and still the error: object required.
my screen is 1024. if i do any other value, no error occurs, but also
nothing happens.

any other clues?

OK. Finally got access to an XP box:

if (screen.width == 1280) {
document.body.style.backgroundImage='url(1.gif)';
}

works like a charm in every browser tested, including
IE 6.0.2800 on Windows XP SP1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top