englercj / jquery-ajax-progress

Simple patch that adds a 'progress' callback to jquery Ajax calls

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jquery Ajax Progresss

A simple patch to jQuery that will call a 'progress' callback, using the XHR.onProgress event

Usage

Simply include the script on your page:

<script src="js/jquery.ajax-progress.js"></script>

Then, whenever you make an ajax request, just specify a progress callback:

$.ajax({
    method: 'GET',
    url: 'data/bird.json',
    dataType: 'json',
    success: function() { },
    error: function() { },
    progress: function(e) {
        //make sure we can compute the length
        if(e.lengthComputable) {
            //calculate the percentage loaded
            var pct = (e.loaded / e.total) * 100;

            //log percentage loaded
            console.log(pct);
        }
        //this usually happens when Content-Length isn't set
        else {
            console.warn('Content Length not reported!');
        }
    }
});

You can see it in action on the demo.html page.

Notes

About

Simple patch that adds a 'progress' callback to jquery Ajax calls

License:MIT License


Languages

Language:HTML 74.2%Language:JavaScript 25.8%