Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Why doesn't this work in Firefox? (http://www.velocityreviews.com/forums/t934003-why-doesnt-this-work-in-firefox.html)

Ade 12-21-2007 12:59 PM

Why doesn't this work in Firefox?
 
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>



rf 12-21-2007 01:05 PM

Re: Why doesn't this work in Firefox?
 

"Ade" <adrian.nospam.birkett@blueyonder.co.uk> wrote in message
news:GMOaj.33318$036.15455@fe1.news.blueyonder.co. uk...
> 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.

--
Richard.



Ade 12-21-2007 01:09 PM

Re: Why doesn't this work in Firefox?
 

"rf" <rf@invalid.com> wrote in message
news:VSOaj.26161$CN4.12539@news-server.bigpond.net.au...
>
> "Ade" <adrian.nospam.birkett@blueyonder.co.uk> wrote in message
> news:GMOaj.33318$036.15455@fe1.news.blueyonder.co. uk...
>> 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.
>
> --
> Richard.
>

Richard,

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

Ade



The Magpie 12-21-2007 09:15 PM

Re: Why doesn't this work in Firefox?
 
Ade wrote:
> "rf" <rf@invalid.com> wrote in message
> news:VSOaj.26161$CN4.12539@news-server.bigpond.net.au...
>> "Ade" <adrian.nospam.birkett@blueyonder.co.uk> wrote in message
>> news:GMOaj.33318$036.15455@fe1.news.blueyonder.co. uk...
>>> 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.
>>
>> --
>> Richard.
>>

> 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.

Thomas 'PointedEars' Lahn 12-21-2007 10:19 PM

Re: Why doesn't this work in Firefox?
 
Ade wrote:
> 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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


All times are GMT. The time now is 04:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.