Search code examples
javascripthtmlcachingcache-manifest

Alert upon completion of Manifest cache with javascript


How do I alert a success message when cache.manifest has download all the files in a html5 app? (Bascially success alert on complete cache download)!

Thanks


Solution

  • Well thanks to the very well thought article from Garden Gnome http://gardengnomesoftware.com/wiki/Cache_Manifest_File

    I found the answer. All you have to do it create a div which will display the status of cache by adding this to your page:

    <div id="cachestatus" style="position:fixed;left: 2px;top: 2px; width: 150px;height:18px;color: #ff0000;padding: 1px 3px; opacity:1; z-index:100; font-family:Arial, Helvetica, sans-serif; opacity:0.2;"></div>
    

    and just right after this div add the following java-script:

    <script type="text/javascript">
    
        var cacheStatus  = document.getElementById('cachestatus');
        cacheStatus.innerHTML="cache status";
    
    
        if (navigator.onLine) {
            window.applicationCache.addEventListener('updateready', function(e) {
                if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
                    window.applicationCache.swapCache();
                    cacheStatus.innerHTML="update ready!";
                    if (confirm('A new version of this page is available. Reload?')) {
                        window.location.reload();
                    }
                }
            }, false);
            window.applicationCache.addEventListener('cached', function(e) {cacheStatus.innerHTML="cache is ready!"; },false);
            window.applicationCache.addEventListener('noupdate', function(e) { cacheStatus.innerHTML="cache is up to date!"; },false);
            window.applicationCache.addEventListener('downloading', function(e) { cacheStatus.innerHTML="downloading..."; },false);
            window.applicationCache.addEventListener('error', function(e) { cacheStatus.innerHTML="error"; },false);
            window.applicationCache.update();
        } else {
            cacheStatus.innerHTML="offline";
        }
    </script>  
    

    status of the cache will be displayed in the DIV.