Keywords: jQuery | Ajax | loading image
Abstract: This article explains how to add loading indicators to jQuery Ajax calls using both manual show/hide methods and global event handlers.
Introduction
Ajax, short for Asynchronous JavaScript and XML, is a technique used in web development to send and receive data asynchronously from a server without refreshing the page. When performing Ajax requests, it's common to provide visual feedback to users, such as a loading image, to indicate that the request is in progress. In jQuery, this can be achieved through various methods.
Manual Show/Hide Method
One straightforward approach is to manually control the visibility of a loading element before and after the Ajax call. This method involves creating a hidden HTML element, such as a div containing an image, and toggling its display based on the Ajax request lifecycle.
<div id="LoadingImage" style="display: none;">
<img src="loading.gif" alt="Loading..." />
</div>
<script>
function ajaxCall() {
$("#LoadingImage").show(); // Show loading image before request
$.ajax({
type: "GET",
url: surl,
dataType: "jsonp",
cache: false,
jsonp: "onJSONPLoad",
jsonpCallback: "newarticlescallback",
crossDomain: "true",
success: function(response) {
$("#LoadingImage").hide(); // Hide on success
alert("Success");
},
error: function(xhr, status) {
$("#LoadingImage").hide(); // Hide on error
alert('Unknown error ' + status);
}
});
}
</script>
In this code, the <div id="LoadingImage"> is initially hidden. When the Ajax request is initiated via ajaxCall(), the loading image is shown using $("#LoadingImage").show(), and it is hidden in both the success and error callbacks using $("#LoadingImage").hide().
Global Event Method
Alternatively, jQuery provides global Ajax event handlers like ajaxStart and ajaxStop that can automatically manage loading indicators for all Ajax requests on the page. This method is useful when multiple Ajax calls need to be handled uniformly.
<div id="loading" style="display: none;">Loading...</div>
<script>
$(function() {
var loading = $("#loading");
$(document).ajaxStart(function() {
loading.show();
});
$(document).ajaxStop(function() {
loading.hide();
});
});
</script>
With this approach, whenever an Ajax request starts, the ajaxStart event is triggered, showing the loading element. When all Ajax requests complete, the ajaxStop event hides it.
Comparison and Best Practices
The manual method offers more control, as you can specifically target each Ajax call, but it requires code duplication if used in multiple places. The global event method simplifies management for multiple requests but may not be suitable if different loading indicators are needed for different parts of the page.
For simple scenarios with a single or few Ajax calls, the manual show/hide method is recommended due to its simplicity. In complex applications with numerous Ajax interactions, the global event method can improve code maintainability.
Conclusion
Implementing loading images in jQuery Ajax enhances user experience by providing visual feedback. Both methods described—manual control and global event handlers—are effective, with the choice depending on specific use cases. Developers should consider factors like code reusability and application complexity when selecting an approach.