Search code examples
javascriptfunctiontriggersgifanimated-gif

javascript trigger when gif animation is done


I have a site with a not-repeating gif on it, so it looks like it is loading. I would like a javascript script that detects when the gif is done animating, and then redirects to another site.

Is this possible? I can't just set it to like 5 seconds, because diffrent computers/tablets load it in diffrent times.

If you would like to see it in action, then please go ahead and visit https://www.frossblock.dk/misc/loading - It works fine on computers as far as i know, but on tablets/phones it does nt have time enough to complete the full animation.


Solution

  • Start a timer equal to the GIF duration when the GIF has loaded;

    <img onload="redirect();" style="position:absolute;top:0;bottom:80px;right:0;left:0;margin:auto;" src="../images/loading.gif"" width="150" height="150" />
    

    And in the <head> element:

    <script type="text/javascript">   
    function redirect() 
    {  
      window.setTimeout(function() {
        window.location="../index";
      }, 6700);   
    } 
    </script>