Implement a loading indicator for a jQuery AJAX call


I have a Bootstrap modal which is launched from a link. For about 3 seconds it just sits there blank, while the AJAX query fetches the data from the database. How can I implement some sort of a loading indicator? Does twitter bootstrap provide this functionality by default?

EDIT: JS code for modal

<script type="text/javascript">
  $('div.divBox a').click(function(){
    var vendor = $(this).text();
    $('#myModal').on('show', function(){             
        type: "GET",
        url: "ip.php",
        data: "id=" + vendor,
        success: function(html){
          $(".modal-header h3").html(vendor);
            "sDom": "T<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
            "sPaginationType": "bootstrap",
            "oLanguage": {
              "sLengthMenu": "_MENU_ records per page"
            "aaSorting":[[0, "desc"]],
            "iDisplayLength": 10,
            "oTableTools": {
              "sSwfPath": "swf/copy_csv_xls_pdf.swf",
              "aButtons": ["csv", "pdf"]
  $('#myModal').on('hide', function () {
6/6/2018 3:43:34 PM

Accepted Answer

I'm guessing you're using jQuery.get or some other jQuery ajax function to load the modal. You can show the indicator before the ajax call, and hide it when the ajax completes. Something like

$('#someModal').get(anUrl, someData, function() { $('#indicator').hide(); });
8/14/2012 10:05:13 PM

There is a global configuration using jQuery. This code runs on every global ajax request.

<div id='ajax_loader' style="position: fixed; left: 50%; top: 50%; display: none;">
    <img src="themes/img/ajax-loader.gif"></img>

<script type="text/javascript">
    jQuery(function ($){

Here is a demo:


A loading indicator is simply an animated image (.gif) that is displayed until the completed event is called on the AJAX request. offers many options for generating loading images that you can overlay on your modals. To my knowledge, Bootstrap does not provide the functionality built-in.


This is how I got it working with loading remote content that needs to be refreshed:

$(document).ready(function () {
    var loadingContent = '<div class="modal-header"><h1>Processing...</h1></div><div class="modal-body"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div></div>';

    // This is need so the content gets replaced correctly.
    $("#myModal").on("", function (e) {
        var link = $(e.relatedTarget);

    $("#myModal2").on("", function (e) {

Basically, just replace the modal content while it's loading with a loading message. The content will then be replaced once it's finished loading.


This is how I realised the loading indicator by an Glyphicon:

<!DOCTYPE html>


    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>

        .gly-ani {
          animation: ani 2s infinite linear;
        @keyframes ani {
          0% {
            transform: rotate(0deg);
          100% {
            transform: rotate(359deg);

<div class="container">

    <span class="glyphicon glyphicon-refresh gly-ani" style="font-size:40px;"></span>