110

Does anyone know a work around to make animated GIF's continue to be animated after you click a link or submit a form on the page your on in IE? This works fine in other browsers.

Thanks.

mattt
  • 1,471
  • 4
  • 16
  • 24
  • 3
    The only right answer on here is @AnthonyWJones. For POSTs, actual submits (not ajax) nothing works. – P.Brian.Mackey Jun 01 '11 at 18:24
  • Actually that is not true at all. Using a timeout to show the image on the onsubmit event works just fine with form POST. – oldwizard Jan 24 '13 at 08:27
  • @P.Brian.Mackey - solution by j.davis worked for me for even a post request. However we are also using ajaxcontrol toolkit to make a call to server side method. It doesn't work in that case. – Ankur-m Jan 02 '14 at 06:54
  • possible duplicate of [Update Progress animated gif stops on postback](http://stackoverflow.com/questions/7185316/update-progress-animated-gif-stops-on-postback) – Jason Kresowaty Jul 22 '14 at 23:51
  • Check out Jourdan's answer [here](http://stackoverflow.com/questions/7185316/update-progress-animated-gif-stops-on-postback). I think it is a better solution and it works on downlevel IE too. – Jason Kresowaty Jul 22 '14 at 23:56

16 Answers16

99

The accepted solution did not work for me.

After some more research I came across this workaround, and it actually does work.

Here is the gist of it:

function showProgress() {
    var pb = document.getElementById("progressBar");
    pb.innerHTML = '<img src="./progress-bar.gif" width="200" height ="40"/>';
    pb.style.display = '';
}

and in your html:

<input type="submit" value="Submit" onclick="showProgress()" />
<div id="progressBar" style="display: none;">
    <img src="./progress-bar.gif" width="200" height ="40"/>
</div>

So when the form is submitted, the <img/> tag is inserted, and for some reason it is not affected by the ie animation issues.

Tested in Firefox, ie6, ie7 and ie8.

Michael
  • 8,362
  • 6
  • 61
  • 88
j.davies
  • 2,321
  • 1
  • 19
  • 24
  • 3
    Source: http://www.webproworld.com/graphics-design-discussion-forum/63262-gif-show-page-loading.html – j.davies Dec 15 '09 at 03:06
  • 8
    In your code you never actually perform a POST. Only the "onclick=" javascript call. I tried this with a POST as is expected when you click submit and it does not work. – P.Brian.Mackey Jun 01 '11 at 18:20
  • Unless you are returning false at the end of your onclick, the submit button will trigger a POST. That is what submit buttons do. – Frug Dec 14 '11 at 18:52
  • 2
    -1 This answer is WRONG too. It does not work in IE7 and IE8 the gif image FREZEES after form submission. – Marco Demaio Feb 04 '12 at 12:58
  • Please check answer about spin.js from Entendu which works when doing a POST without stopping animation – mr-euro Mar 16 '12 at 13:27
  • 2
    This worked for me using jQuery. The hack is pretty weird, I have to admit. This is how it worked in my example: `code`
    And then: `code` $("#img_content").html($("#img_content").html());
    – misaizdaleka Mar 16 '12 at 14:16
  • This worked for me... var $activity = $('#imgActivity'); $activity.attr('src', $activity.attr('src')); – Brian Mar 21 '13 at 16:02
  • Worked for me, the div was being created dynamically so I used var htmlstr = $("#Loading").html(); var load = document.getElementById("Loading"); load.innerHTML = htmlstr; load.style.display = ''; $("#Loading").show(); – Stuart Dobson May 22 '13 at 01:08
  • Works for me in IE10, but ironically does not work in Firefox 26.0 - whereas a normal img tag will animate just fine. – Jagd Jan 17 '14 at 23:48
  • This doesn't work for me on Chrome. Chrome tries to load the image again, but the GET request gets cancelled. I had this workaround working in IE for a few days though. But it stopped working today. I just can't figure out what's wrong. – Ankur-m Feb 07 '14 at 12:00
  • I don't really understand exactly why, but this works in IE8 and Chrome 35.0.1916.153 m. However it did not work with Firefox 17.0.10. Anyway, that is perfect in my case, as Firefox is not a company standard. (Updating to FireFox 24.6.0 did not work either btw). – jumps4fun Jul 04 '14 at 09:53
  • ^ I second that. This works on IE8, Chrome 39.x but still not working on Firefox 34.x – Khrys Dec 23 '14 at 16:53
  • thanks, @misaizdaleka. This is work like a charm. It seems since we refresh the html content of the div, IE think this is a new element (well... technically, it does since we remove it from the DOM and put it back at the same place). No need for unnecessary js files since we already use jQuery anyway. :) – Firanto Feb 11 '15 at 20:11
  • It works for mee to (in IE8 & IE11) if I FIRST initiate a POST (e.g. button click) and THEN update (re-create) the animated image (if needed, then even re-create it in a settimeout function with a small delay). – Jen-Ari Apr 23 '15 at 13:33
  • This works for me in all my browsers except firefox. – SwR Apr 28 '16 at 09:15
35

old question, but posting this for fellow googlers:

Spin.js DOES WORK for this use case: http://fgnass.github.com/spin.js/

Entendu
  • 1,235
  • 10
  • 12
  • Correction, it works but stops animating right before the page loads. So if it's a short load it looks like the animation freezes like in the original issue. – James McMahon Sep 12 '11 at 17:12
  • Great solution, working fine in ie11 so still seems all up to date and supported. – Adam Knights Feb 25 '14 at 15:27
  • spin.js unfortunately is quite CPU intensive. See [issues/8](https://github.com/fgnass/spin.js/issues/8), [issues/200](https://github.com/fgnass/spin.js/issues/200), [issues/215](https://github.com/fgnass/spin.js/issues/215) – user247702 Aug 13 '15 at 14:00
  • Spin.js isn't working for me on iPhone. It also freezes as soon as a the page is redirected to another one. – Ankur-m Dec 31 '15 at 09:58
18

IE assumes that the clicking of a link heralds a new navigation where the current page contents will be replaced. As part of the process for perparing for that it halts the code that animates the GIFs. I doubt there is anything you can do about it (unless you aren't actually navigating in which case use return false in the onclick event).

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
17

Here's a jsFiddle that works just fine on form submit with method="post". The spinner is added on form submit event.

$("form").bind("submit", onFormSubmit);

function onFormSubmit() {
    setTimeout(showSpinner, 1);
}

function showSpinner() {
    $("body").append($('<img id="spinner" src="spinner.gif" alt="Spinner" />'));
}

See jsFiddle code here

Test it in your IE browser here

oldwizard
  • 5,012
  • 2
  • 31
  • 32
  • 2
    Yes, this works great (only tested in IE 10 so far). But I cannot work out why animation is stopped when I preload the image in memory. I can preload in your fiddle and it works OK, just not in my code, argh. – Simon East Oct 07 '13 at 03:20
  • You did something wrong. :) Add the spinner image as an img tag to your page? I've updated the url. Might help. – oldwizard Oct 07 '13 at 09:29
  • IE is just a jerk it seems. Stops the animation when pre-loaded. When injected via jquery, animation will continue chugging along. Thanks for this solution. – Anthony May 30 '17 at 13:03
8

I came upon this post, and while it has already been answered, felt I should post some information that helped me with this problem specific to IE 10, and might help others arriving at this post with a similar problem.

I was baffled how animated gifs were just not displaying in IE 10 and then found this gem.

ToolsInternet OptionsAdvancedMultiMediaPlay animations in webpages

hope this helps.

funkymushroom
  • 2,079
  • 2
  • 26
  • 39
  • Nope, that does not solve the problem in **IE11** (but +1 for trying to help)! :( That is -- I have this option enabled and animated GIFs are not animated, when shown in `beforeunload` or `unload` event. – trejder Dec 15 '14 at 14:44
  • 1
    This has nothing to do with the problem posed in the question. – user247702 Aug 13 '15 at 14:17
2

I encountered this problem when trying to show a loading gif while a form submit was processing. It had an added layer of fun in that the same onsubmit had to run a validator to make sure the form was correct. Like everyone else (on every IE/gif form post on the internet) I couldn't get the loading spinner to "spin" in IE (and, in my case, validate/submit the form). While looking through advice on http://www.west-wind.com I found a post by ev13wt that suggested the problem was "... that IE doesn't render the image as animated cause it was invisible when it was rendered." That made sense. His solution:

Leave blank where the gif would go and use JavaScript to set the source in the onsubmit function - document.getElementById('loadingGif').src = "path to gif file".

Here's how I implemented it:

<script type="text/javascript">
    function validateForm(form) {

        if (isNotEmptyid(form.A)) {
            if (isLen4(form.B)) {        
                if (isNotEmptydt(form.C)) {
                    if (isNumber(form.D)) {        
                        if (isLen6(form.E)){
                            if (isNotEmptynum(form.F)) {
                                if (isLen5(form.G)){
                                    document.getElementById('tabs').style.display = "none";                
                                    document.getElementById('dvloader').style.display = "block";                
                                    document.getElementById('loadingGif').src = "/images/ajax-loader.gif"; 
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;   
    }               
</script>

<form name="payo" action="process" method="post" onsubmit="return validateForm(this)">
    <!-- FORM INPUTS... -->
    <input type="submit" name="submit" value=" Authorize ">

    <div style="display:none" id="dvloader">  
        <img id="loadingGif" src="" alt="Animated Image that appears during processing of document." />
        Working... this process may take several minutes.
    </div>

</form>

This worked well for me in all browsers!

lostphilosopher
  • 4,361
  • 4
  • 28
  • 39
  • Setting src worked for me as well in IE11. In my case, it is in combination with re-adding the img html first. I'm not sure why I need both. It is interesting to note that I am also setting 'display' (to block), as opposed to modifying 'visibility'. – Xtopher Aug 12 '16 at 17:11
2

Found this solution at http://timexwebdev.blogspot.com/2009/11/html-postback-freezes-animated-gifs.html and it WORKS! Simply re-load image before setting to visible. Call the following Javascript function from your button's onclick:

function showLoader()
{
   //*** Reload the image for IE ***
   document.getElementById('loader').src='./images/loader.gif';
   //*** Let's make the image visible ***
   document.getElementById('loader').style.visibility = 'visible';
}
Dan H.
  • 262
  • 4
  • 12
  • Although this does work in at least IE11 I've noticed some issues when I tested Firefox 85 running on various versions of Windows. This was on BrowserStack though so take that with a pinch of salt. – Bower Feb 27 '21 at 00:12
2

Here's what I did. All you have to to is to break up your GIF to say 10 images (in this case i started with 01.gif and ended with 10.gif) and specify the directory where you keep them.

HTML:

<div id="tester"></div>

JavaScript:

function pad2(number) {   
    return (number < 10 ? '0' : '') + number 
}
var 
    dirURL = 'path/to/your/images/folder',
    ajaxLoader = document.createElement('img');
ajaxLoader.className = 'ajax-image-loader';
jQuery(document).ready(function(){
    jQuery('#tester').append(ajaxLoader);
    set(0);
});
function set(i) {
    if (i > 10) i = 1;    
    img.src = dirURL + pad2(i) + '.gif';
    setTimeout(function() {
        set(++i);
    }, 100);    
}

This method works with IE7, IE8 and IE9 (althought for IE9 you could use spin.js). NOTE: I have not tested this in IE6 since I have no machine running a browser from the 60s, although the method is so simple it probably works even in IE6 and lower.

trejder
  • 17,148
  • 27
  • 124
  • 216
Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28
  • 1
    This workaround works, based on same principle of spin,js basically using a JS setTimeout to simulate a gif animation. – Marco Demaio Feb 04 '12 at 12:54
1

Building on @danfolkes' answer, this worked for me in IE 8 and ASP.NET MVC3.

In our _Layout.cshtml

<body style="min-width: 800px">
    <div id="progress">
        <div style="min-height: 200px">
        </div>
        <div id="throbber">
            <img src="..\..\Content\ajax-loader.gif" alt="Progress" style="display: block;
                margin-left: auto; margin-right: auto" />
        </div>
    </div>
    <div id="main">

<<< content here >>> ...

<script type="text/javascript" language="javascript">
    function ShowThrobber() {
        $('#main').hide();
        $('#progress').show();
        // Work around IE bug which freezes gifs
        if (navigator.appName == 'Microsoft Internet Explorer') {
            // Work around IE bug which freezes gifs
            $("#throbber").html('<img src="../../Content/ajax-loader.gif" alt="Progress" style="display: block; margin-left: auto; margin-right: auto"/>');
         }
</script>
<script type="text/javascript" language="javascript">
    function HideThrobber() {
        $('#main').show();
        $('#progress').hide();
    }
</script>
<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        HideThrobber();
    });
</script>

And in our navigation links:

<input type="submit" value="Finish" class="submit-link" onclick="ShowThrobber()"/>

or

@Html.ActionLink("DoSometthing", "MyController", new { Model.SomeProperty }, new { onclick = "ShowThrobber()" })
Kit
  • 2,089
  • 1
  • 11
  • 23
1

Related to this I had to find a fix where animated gifs were used as a background image to ensure styling was kept to the stylesheet. A similar fix worked for me there too... my script went something like this (I'm using jQuery to make it easier to get the computed background style - how to do that without jQuery is a topic for another post):

var spinner = <give me a spinner element>

window.onbeforeunload = function() {
  bg_image = $(spinner).css('background-image');
  spinner.style.backgroundImage = 'none';
  spinner.style.backgroundImage = bg_image;
}

[EDIT] With a bit more testing I've just realised that this doesn't work with background images in IE8. I've been trying everything I can think of to get IE8 to render a gif animation wile loading a page, but it doesn't look possible at this time.

CodeMonkey
  • 19
  • 2
  • Why you're reading CSS code using jQuery (`$(spinner).css('background-image')`), but instead of setting it the same way, you're using pure Javascript way (`spinner.style.backgroundImage`). Your code is not working for me in any browser. If I use directly your code, then I'm getting `cannot set backgroundImage property of undefined` error. If I change your code to set CSS using jQuery, code runs without any errors, but image is not reset and div / spinner element remains empty. I don't know, why? – trejder Dec 16 '14 at 12:02
  • **[Verified](http://stackoverflow.com/q/27505213/1469208)**! There is no chance, your code will work. No browser is **not** able (as of December 2014) to set `background-image` property in `unload` or `beforeunload` events. See [this question](http://stackoverflow.com/q/27505213/1469208) or [this jsFiddle](http://jsfiddle.net/trejder/sygyeqts/) for details. – trejder Dec 16 '14 at 13:45
0

Jquery:

$("#WhereYouWantTheImageToAppear").html('<img src="./Animated.gif" />');
danfolkes
  • 404
  • 4
  • 12
0

Just had a similar issue. These worked perfectly for me.

$('#myElement').prepend('<img src="/path/to/img.gif" alt="My Gif" title="Loading" />');

$('<img src="/path/to/img.gif" alt="My Gif" title="Loading" />').prependTo('#myElement');

Another idea was to use jQuery's .load(); to load and then prepend the image.

Works in IE 7+

0

Very, very late to answer this one, but I've just discovered that using a background-image that is encoded as a base64 URI in the CSS, rather than held as a separate image, continues to animate in IE8.

Mark
  • 1,009
  • 1
  • 9
  • 8
0

A very easy way is to use jQuery and SimpleModal plugin. Then when I need to show my "loading" gif on submit, I do:

$('*').css('cursor','wait');
$.modal("<table style='white-space: nowrap'><tr><td style='white-space: nowrap'><b>Please wait...</b></td><td><img alt='Please wait' src='loader.gif' /></td></tr></table>", {escClose:false} );
Matt Roy
  • 1,455
  • 1
  • 17
  • 27
0

I realize that this is an old question and that by now the original posters have each found a solution that works for them, but I ran across this issue and found that VML tags do not fall victim to this IE bug. Animated GIFs still move during page unload when placed on the IE browser using VML tags.

Notice I detected VML first before making the decision to use VML tags so this is working in FireFox and other browsers using normal animated GIF behavior.

Here's how I solved this.

<input class="myExitButton" type="button" value="Click me"  />

<div class="loadingNextPage" style="display:none" >
    <span style="left:-24px; POSITION: relative; WIDTH: 48px; DISPLAY: inline-block; HEIGHT: 48px" class="spinnerImageVml"><?xml:namespace prefix="rvml" ns = "urn:schemas-microsoft-com:vml" /><rvml:group style="POSITION: absolute; WIDTH: 48px; HEIGHT: 48px; rotation: 1deg" class="rvml" coordsize = "47,47"><rvml:image style="POSITION: absolute; WIDTH: 48px; HEIGHT: 48px; TOP: 0px; LEFT: 0px" class="rvml" src="/images/loading_gray.gif" coordsize="21600,21600"></rvml:image></rvml:group></span>
    <img class="spinnerImage" src="/images/loading_gray.gif" alt="loading..." />
    <div>Just one moment while we access this information...</div>
</div>

<script language="javascript" type="text/javascript">
    window.LoadProgress = (function (progress, $) {

        var getdialogHeight = function (height) {
            var isIE = navigator.userAgent.toLowerCase().indexOf('msie') > -1;
            if (isIE) {
                return height + 'px';
            }
            return height;
        };

        var isVmlSupported = function () {
            var a = document.body.appendChild(document.createElement('div'));
            a.innerHTML = '<v:shape id="vml_flag1" adj="1" />';
            var b = a.firstChild;
            b.style.behavior = "url(#default#VML)";
            var supported = b ? typeof b.adj == "object" : true;
            a.parentNode.removeChild(a);
            return supported;
        };

        var showAnimationDuringPageUnload = function () {
            if (isVmlSupported()) {
                $(".loadingNextPage > .spinnerImage").hide();
            }
            else {
                $(".loadingNextPage > .spinnerImageVml").hide();
            }
        };

        var openLoadingMessage = function () {
            $(".loadingNextPage").dialog({
                modal: true,
                closeOnEscape: true,
                title: 'Please wait...',
                closeText: 'x',
                height: getdialogHeight(200),
                buttons: {
                    "Close": function () {
                        $(this).dialog("close");
                    }
                }
            });
        };

        $('.myExitButton').click(function () {
            openLoadingMessage();
            showAnimationDuringPageUnload();
            window.location.href = 'http://www.stackoverflow.com';
        });


        return progress;
    })(window.LoadProgress || {}, window.jQuery);
</script>

Naturally, this relies on jQuery, jQueryUI and requires an animated GIF of some type ("/images/loading_gray.gif").

David
  • 233
  • 1
  • 3
  • 9
-1

I had this same problem, common also to other borwsers like Firefox. Finally I discovered that dynamically create an element with animated gif inside at form submit did not animate, so I developed the following workaorund.

1) At document.ready(), each FORM found in page, receive position:relative property and then to each one is attached an invisible DIV.bg-overlay.

2) After this, assuming that each submit value of my website is identified by btn-primary css class, again at document.ready(), I look for these buttons, traverse to the FORM parent of each one, and at form submit, I fire showOverlayOnFormExecution(this,true); function, passing clicked button and a boolean that toggle visibility of DIV.bg-overlay.

$(document).ready(function() {

  //Append LOADING image to all forms
  $('form').css('position','relative').append('<div class="bg-overlay" style="display:none;"><img src="/images/loading.gif"></div>');

  //At form submit, fires a specific function
  $('form .btn-primary').closest('form').submit(function (e) {
    showOverlayOnFormExecution(this,true);
  });
});

CSS for DIV.bg-overlay is the following:

.bg-overlay
{
  width:100%;
  height:100%;
  position:absolute;
  top:0;
  left:0;
  background:rgba(255,255,255,0.6);
  z-index:100;
}

.bg-overlay img
{
  position:absolute;
  left:50%;
  top:50%;
  margin-left:-40px; //my loading images is 80x80 px. This is done to center it horizontally and vertically.
  margin-top:-40px;
  max-width:auto;
  max-height:80px;
}

3) At any form submit, the following function is fired to show a semi-white background overlay all over it (that deny ability to interact again with form) and an animated gif inside it (that visually show a loading action).

function showOverlayOnFormExecution(clicked_button, showOrNot) 
{
    if(showOrNot == 1)
    {
        //Add "content" of #bg-overlay_container (copying it) to the confrm that contains clicked button 
        $(clicked_button).closest('form').find('.bg-overlay').show();
    }
    else
        $('form .bg-overlay').hide();
}

Showing animated gif at form submit, instead of appending it at this event, solves "gif animation freeze" problem of various browsers (as said, I found this problem in IE and Firefox, not in Chrome)

Luca Detomi
  • 5,564
  • 7
  • 52
  • 77