Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
Fixed the test page and the handler interfaces. Wrote more of the REA…
Browse files Browse the repository at this point in the history
…DME.
  • Loading branch information
kpozin committed Apr 23, 2011
1 parent 64d9587 commit db83471
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The plugin exposes the `onprogress` event in two ways:

### Global AJAX Event
As a [global AJAX event](http://docs.jquery.com/Ajax_Events#Global_Events) that you can subscribe to.
You can set a handler on any jQuery selection in the DOM, and it will get triggered for all AJAX requests.

$("#loading").bind("ajaxProgress", function(jqEvent, progressEvent, jqXHR) {
if (progressEvent.lengthComputable) {
Expand All @@ -33,4 +34,17 @@ jQuery, `progressEvent` is the native object conforming to the
[ProgressEvent interface](http://www.w3.org/TR/progress-events/#interface-progressevent), and `jqXHR` is the original
[wrapper around the XMLHttpRequest object](http://api.jquery.com/jQuery.ajax/#jqXHR).

### Local AJAX Event

### Local AJAX Event

You can also provide a handler for a specific `jQuery.ajax()` call by including a `progress` field in the options
object you pass to `jQuery.ajax()`.

$.ajax("/myfile", {progress: function(jqXHR, progressEvent) {
if (progressEvent.lengthComputable) {
console.log("Loaded " + (Math.round(progressEvent.loaded / progressEvent.total * 100) / 100)) + "%";
} else {
console.log(Loading...)
}
}});

8 changes: 6 additions & 2 deletions jquery.ajaxprogress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*!
* jQuery ajaxProgress Plugin v0.0.1
* jQuery ajaxProgress Plugin v0.5.0
* Requires jQuery v1.5.0 or later
*
* http://www.kpozin.net/ajaxprogress
*
* (c) 2011, Konstantin Pozin
Expand All @@ -15,14 +17,16 @@
return;
}

var NAMESPACE = ".ajaxprogress";

// Create global "ajaxProgress" event
$.fn.ajaxProgress = function (f) {
return this.bind("ajaxProgress", f);
};

// Hold on to a reference to the jqXHR object so that we can pass it to the progress callback.
// Namespacing the handler with ".ajaxprogress"
$("html").bind("ajaxSend.ajaxprogress", function(event, jqXHR, ajaxOptions) {
$("html").bind("ajaxSend" + NAMESPACE, function(event, jqXHR, ajaxOptions) {
ajaxOptions.__jqXHR = jqXHR;
});

Expand Down

0 comments on commit db83471

Please sign in to comment.