I am trying to display an loading indicator for every ajax request, I working in a rails 3 app.
HTML:
<div id="loading-indicator">
<%= image_tag("loading.gif", :id => "loading-indicator", :style => "display:none") %>
</div>
CSS:
#loading-indicator {
position: absolute;
left: 10px;
top: 10px;
}
loading.js: which I placed in assest/javascripts/
$(document).ready(function(){
$(document).ajaxSend(function(event, request, settings) {
$('#loading-indicator').show();
});
$(document).ajaxComplete(function(event, request, settings) {
$('#loading-indicator').hide();
});
});
My application.js looks like this:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
It dosent work, nothing shows up. Any help would be appreciated.
I usually have this piece of code saved to situations like these:
— EDIT —
This will not work with the latest versions of jQuery. As of jQuery 1.8, the
.ajaxStart()method should only be attached todocument. The code should therefore look as follows: