Why doesn't this work in Firefox?

Discussion in 'Javascript' started by Ade, Dec 21, 2007.

  1. Ade

    Ade Guest

    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>
     
    Ade, Dec 21, 2007
    #1
    1. Advertising

  2. Ade

    rf Guest

    "Ade" <> wrote in message
    news:GMOaj.33318$...
    > 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.
     
    rf, Dec 21, 2007
    #2
    1. Advertising

  3. Ade

    Ade Guest

    "rf" <> wrote in message
    news:VSOaj.26161$...
    >
    > "Ade" <> wrote in message
    > news:GMOaj.33318$...
    >> 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
     
    Ade, Dec 21, 2007
    #3
  4. Ade

    The Magpie Guest

    Ade wrote:
    > "rf" <> wrote in message
    > news:VSOaj.26161$...
    >> "Ade" <> wrote in message
    >> news:GMOaj.33318$...
    >>> 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.
     
    The Magpie, Dec 21, 2007
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Dec 21, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  2. John Kotuby
    Replies:
    5
    Views:
    604
    John Kotuby
    Sep 25, 2008
  3. John Kotuby
    Replies:
    0
    Views:
    370
    John Kotuby
    Sep 24, 2008
  4. Replies:
    3
    Views:
    140
  5. Man-wai Chang

    Why doesn't this page work with Firefox?

    Man-wai Chang, May 1, 2007, in forum: Javascript
    Replies:
    0
    Views:
    80
    Man-wai Chang
    May 1, 2007
Loading...

Share This Page