Why doesn't this work in Firefox?

A

Ade

Hi,

I'm new to all this stuff so please go easy on me if this is a simple error
:)

This code is to swap two images on a page by use of a timer. It works in IE
but not in Firefox. Can someone please point out what's wrong?

Thanks,

Ade

<html>
<head>
<script language="javascript" type="text/javascript">
var Timer; // = setTimeout("swap()",1000);
var iPic;
var aPics=new Array();

iPic=0;
aPics[0]="img1.bmp";
aPics[1]="img2.bmp";

function swap()
{ if (iPic == 1)
iPic=0;
else
iPic=1;

document.all.myImage.src = aPics[iPic];
Timer = setTimeout("swap()",1000);
}
</script>
</head>
<body onload=swap();>
<img name="myImage" src="img1.bmp"></img>
</body>
</html>
 
R

rf

Ade said:
Hi,

I'm new to all this stuff so please go easy on me if this is a simple
error :)

This code is to swap two images on a page by use of a timer. It works in
IE but not in Firefox. Can someone please point out what's wrong?

Turn on the errorconsole, or install firebug, and look at the error FF will
give you.
 
A

Ade

rf said:
Turn on the errorconsole, or install firebug, and look at the error FF
will give you.
Richard,

Thanks for that, I didn't know that the error console was there. I'll try a
different method.

Ade
 
T

The Magpie

Ade said:
Richard,

Thanks for that, I didn't know that the error console was there. I'll try a
different method.
It will tell you to use document.getElementByID("myImage") and to set
an id rather than a name for the tagged element. Among other things,
perhaps.
 
T

Thomas 'PointedEars' Lahn

Ade said:
This code is to swap two images on a page by use of a timer. It works in IE
but not in Firefox. Can someone please point out what's wrong?
[...]
<html>
<head>

Your markup is not Valid. You should apply http://validator.w3.org/ and
make the necessary modifications if you want to have at least a realistic
chance of interoperable code. Note that despite the Validator recommending
XHTML due to W3C politics if the DOCTYPE declaration was omitted, you should
declare and validate against HTML 4.01 (preferably Strict) instead as XHTML
is not yet universally supported on the Web and probably not required in
your case.
<script language="javascript" type="text/javascript">

The `language' attribute is deprecated. It is syntactically invalid in HTML
4.01 Strict, XHTML 1.0 Strict and XHTML 1.1.
var Timer; // = setTimeout("swap()",1000);

`Timer' does not designate a constructor, so it should be `timer'.
var iPic;

It does not make sense here to declare the variable here and initialize it
elsewhere.

var iPic = 0;
var aPics=new Array();

iPic=0;
aPics[0]="img1.bmp";
aPics[1]="img2.bmp";

You might want to consider

var aPics = new Array("img1.bmp", "img2.bmp");
function swap()
{ if (iPic == 1)
iPic=0;
else
iPic=1;

Storing the toggle status in a boolean variable might be better and easier
to program:

var iPic = false;
// ...
iPic = !iPic;
// ...
+iPic
document.all.myImage.src = aPics[iPic];

Although recent Firefox version support `document.all' to a certain extent,
that is an MSHTML-proprietary feature. Instead, use

document.images["myImage"].src = aPics[iPic];

which is both standards-compliant and backwards-compatible, and therefore
most interoperable.
Timer = setTimeout("swap()",1000);

setTimeout() is a host-defined method of the host object referred to by
`window' and should be called so:

var timer = window.setTimeout("swap()", 1000);

Note that since JavaScript 1.2 (in Netscape Navigator 4.0) the method takes
a Function object reference as first argument, so you may as well write

var timer = window.setTimeout(swap, 1000);

You should also note that this fast an image swapping, like blinking text,
introduces an accessibility issue. The average simple reaction time of
humans is about one third of a second if the event is expected, and one
second if it is unexpected -- and you should consider that disabled and
aged people use the Web in greater proportions than other groups of people.


HTH

PointedEars
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top