Giter VIP home page Giter VIP logo

jquery-ajaxprogress's Introduction

Description

This jQuery plugin provides access to the onprogress event of the XMLHttpRequest object available in modern browsers.

This allows you to get the information needed to visually show download progress when your web application is making a large AJAX request.

The code has been tested in recent versions of Chrome/Chromium and in Firefox 4.

Dependencies

This plugin requires jQuery 1.5 or higher.

How to use

Add the script to the page's list of <script> tags after the jQuery reference.

In your page code, check the value of jQuery.support.ajaxProgress. If this value is false, then the browser doesn't support the onprogress event, so you'll have to write fallback code (for example, showing a generic progress spinner instead of a percentage).

The onprogress event passes to its handlers a native object conforming to the ProgressEvent interface:

The ProgressEvent object

The interface has the following fields

  • {Boolean} lengthComputable - true if the size of the response is known. This is usually known if the server sends a Content-Length header.
  • {Number} loaded - the number of bytes loaded so far
  • {Number} total - the total number of bytes in the response.

The plugin exposes the onprogress event in two ways:

Global AJAX Event

As a global AJAX event 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) {
        $(this).text("Loaded " + (Math.round(progressEvent.loaded / progressEvent.total * 100)) + "%");
    } else {
        $(this).text("Loading...");
    }
});

The handler signature is function ( jqEvent, progressEvent, jqXHR ), where jqEvent is the the event object created by jQuery, progressEvent is the native ProgressEvent object described above, and jqXHR is the original wrapper around the XMLHttpRequest object.

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)) + "%");
    } else {
        console.log("Loading...");
    }
}});

jquery-ajaxprogress's People

Contributors

kpozin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.