[thelist] flipping backgrounds of elements in IE/NS ??

James Aylard webmaster at equilon-mrc.com
Mon Jul 9 18:24:41 CDT 2001


Moniruvwe,

> I tried to write a javascript function that changes the background image
of
> an element like a td or a div.  I wrote the following code, but i'm having
> trouble getting the actual image reference for an element.  Can anyone
offer
> any help?  Thanks.

    A couple of things right off the top (not a complete answer, though):

* You set up an object-detection mechanism at the start of your script, but
then never use it.

* Your object detection for document.getElementById will catch IE 5+, and
not just Netscape 6. Also, understand that this script will *not* work for
Netscape 4.x under any condition -- you can't dynamically swap the
background-image without some serious additional coding that probably won't
be worth the effort.

* When used in a script, hyphenated CSS properties (e.g., background-image)
lose the hyphen and any word that was preceded by the hyphen is capitalized
(e.g., background-image becomes backgroundImage).

* In your event handlers, you pass an argument to your function, changebg,
but your function isn't doing anything with the argument. In JavaScript, you
can pass arguments to a function without formally declaring them as
parameters of the function (as you are doing), but in your case there is no
good reason not to formally declare them.

    Please don't take this in the wrong light, but it is clear that you are
very new to JavaScripting. I recommend that you refer to some JavaScript
tutorials (such as those at WebMonkey [1]) before you attempt something this
complex. Once you have the basics down, you will have a better foundation
for doing something like this.

James Aylard

1. http://hotwired.lycos.com/webmonkey/programming/javascript/





More information about the thelist mailing list