Script works in Firefox, not in IE

Discussion in 'Javascript' started by cjl, Jun 8, 2005.

  1. cjl

    cjl Guest

    Hey all:

    Just getting my feet wet with javascript. The following script works
    fine in Firefox, but won't run in IE. In fact, I've included this
    script as an external file in the <head> of my html file, and the
    remainder of the page doesn't load at all in IE. If I remove the
    <script>, the page loads in IE.

    var images = new Array();
    var firstImage = 2;
    var lastImage = 27;
    var timeout_state = null;
    var counter = 0;
    var running = 0;

    for (counter=firstImage;counter<=lastImage;counter++)
    {
    images[counter] = new Image();
    images[counter].src = counter + ".jpg";
    }

    counter = firstImage;

    function switchImage()
    {
    if(running == 0)
    {
    running = 1;
    animate();
    }
    else
    {
    running = 0;
    clearTimeout(timeout_state);
    timeout_state = null;
    }
    }

    function animate(element)
    {
    if (counter >= lastImage) {counter = firstImage;}
    document.getElementById('right').src = images[counter].src;
    counter++;
    timeout_state = setTimeout("animate()", 50);
    }

    Any help?

    -CJL
    cjl, Jun 8, 2005
    #1
    1. Advertising

  2. cjl

    ASM Guest

    cjl wrote:
    > Hey all:
    >
    > Just getting my feet wet with javascript. The following script works
    > fine in Firefox, but won't run in IE. In fact, I've included this
    > script as an external file in the <head> of my html file, and the
    > remainder of the page doesn't load at all in IE. If I remove the
    > <script>, the page loads in IE.



    > function animate(element)
    > {
    > if (counter >= lastImage) {counter = firstImage;}
    > document.getElementById('right').src = images[counter].src;


    try :
    document.getElementById('right').src = eval(images[counter]+'.src');

    try also putting a var right=''; at top of JS
    or prefer :
    document.getElementById(element).src = images[counter].src;
    and
    timeout_state = setTimeout("animate("+element+")", 50);

    IE doesn't like uknonwn objects and in your JS
    the page object 'right' is used but not yet seen

    > counter++;
    > timeout_state = setTimeout("animate()", 50);
    > }


    and overall don't run animate() before complete loading of the page
    -> preferable to put a JS for that just before </html>

    and let some time to IE it catches your 26 images


    --
    Stephane Moriaux et son [moins] vieux Mac
    ASM, Jun 8, 2005
    #2
    1. Advertising

  3. cjl

    cjl Guest

    Hey all:

    OK, the problem was with my html, not my javascript.

    I had:
    <script src="pre.js" >

    When I needed
    <script src="pre.js" ></script>

    I wasn't providing the closing tag, which IE 6 didn't like, but Firefox
    ignored.

    -CJL
    cjl, Jun 8, 2005
    #3
  4. On 08/06/2005 14:40, ASM wrote:

    > cjl wrote:
    >
    >> Just getting my feet wet with javascript. The following script works
    >> fine in Firefox, but won't run in IE.


    I cannot see anything in what you've posted that will not work in IE
    5.x+. Please post a URL to an example - it's much easier to diagnose.

    Some alternative code is included at the end of this post (though it is
    untested).

    [snip]

    > try :
    > document.getElementById('right').src = eval(images[counter]+'.src');


    Randomly throwing eval about will *NEVER* solve a problem.

    > try also putting a var right=''; at top of JS


    What do you think that will do?

    > or prefer :
    > document.getElementById(element).src = images[counter].src;
    > and
    > timeout_state = setTimeout("animate("+element+")", 50);


    That seems sensible, but it won't fix the OP's code.

    [snip]

    Mike


    function Animation(name, images, rate) {
    var current = 0,
    element = document.images[name],
    timer;

    function animate() {
    element.src = images[current];
    current = (current + 1) % images.length;
    timer = setTimeout(animate, rate);
    }

    if(!element || ('function' != typeof setTimeout)
    || ('function' != typeof clearTimeout))
    {
    return null;
    }

    animate.toString = function() {
    return 'Animation[' + name + ']();';
    };
    Animation[name] = animate;

    this.preload = function() {
    if('function' == typeof Image) {
    for(var i = 0, n = images.length, t; i < n; ++i) {
    t = new Image();
    t.src = images;
    }
    }
    };
    this.reset = function() {
    this.stop();
    current = 0;
    };
    this.start = function() {
    if(!timer) {
    timer = setTimeout(animate, rate);
    }
    };
    this.stop = function() {
    if(timer) {
    clearTimeout(timer);
    timer = null;
    }
    };
    }


    A usage example:

    var myImages = [],
    i = 2,
    n = 29,
    myAnimation;

    /* Define the array of images in the animation.
    * It could be performed in any way, but this
    * seems to suit your situation the best.
    */
    while(i <= n) {
    myImages[myImages.length] = i + '.jpg';
    }

    /* Create an Animation object. */
    myAnimation = new Animation('right', myImages, 50);

    /* If it was created properly, */
    if(myAnimation) {
    /* attempt to preload the frames */
    myAnimation.preload();
    /* and start the animation when the document
    * loads.
    */
    window.onload = function() {
    myAnimation.start();
    };
    }

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Jun 8, 2005
    #4
    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. goober
    Replies:
    6
    Views:
    109
    GĂ©rard Talbot
    May 27, 2006
  2. Rollie72

    Script works in Firefox but not IE

    Rollie72, Feb 21, 2007, in forum: Javascript
    Replies:
    1
    Views:
    115
  3. gray_slp
    Replies:
    3
    Views:
    115
    Geoffrey Summerhayes
    Jun 27, 2007
  4. Paul E. Schoen

    Script works correctly in Firefox, not in IE6

    Paul E. Schoen, Sep 8, 2007, in forum: Javascript
    Replies:
    1
    Views:
    77
    Paul E. Schoen
    Sep 8, 2007
  5. garey
    Replies:
    1
    Views:
    110
    Thomas 'PointedEars' Lahn
    Apr 30, 2009
Loading...

Share This Page