An open API service indexing awesome lists of open source software.

https://github.com/englercj/jquery-ajax-progress

Simple patch that adds a 'progress' callback to jquery Ajax calls
https://github.com/englercj/jquery-ajax-progress

Last synced: about 1 year ago
JSON representation

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

Awesome Lists containing this project

README

          

## Jquery Ajax Progresss

A simple patch to jQuery that will call a 'progress' callback, using the
[XHR.onProgress](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress) event

### Usage

Simply include the script on your page:

```html

```

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

```javascript
$.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

- This will not work using the `file://` protocol, see [XMLHttpRequest - Monitoring Progress](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress) for more info.