I would like to implement a loading image for my jquery ajax code (this is when the jquery is still processing) below is my code:
$.ajax({ type: "GET", url: surl, dataType: "jsonp", cache : false, jsonp : "onJSONPLoad", jsonpCallback: "newarticlescallback", crossDomain: "true", success: function(response) { alert("Success"); }, error: function (xhr, status) { alert('Unknown error ' + status); } });
How can I implement a loading image in this code. Thanks
You should do this using jQuery.ajaxStart
and jQuery.ajaxStop
.
jQuery.ajaxStart
jQuery.ajaxStop
<div id="loading" style="display:none">Your Image</div> <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> <script> $(function () { var loading = $("#loading"); $(document).ajaxStart(function () { loading.show(); }); $(document).ajaxStop(function () { loading.hide(); }); $("#startAjaxRequest").click(function () { $.ajax({ url: "http://www.google.com", // ... }); }); }); </script> <button id="startAjaxRequest">Start</button>
Try something like this:
<div id="LoadingImage" style="display: none"> <img src="" /> </div> <script> function ajaxCall(){ $("#LoadingImage").show(); $.ajax({ type: "GET", url: surl, dataType: "jsonp", cache : false, jsonp : "onJSONPLoad", jsonpCallback: "newarticlescallback", crossDomain: "true", success: function(response) { $("#LoadingImage").hide(); alert("Success"); }, error: function (xhr, status) { $("#LoadingImage").hide(); alert('Unknown error ' + status); } }); } </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With