I want to do something very simple. I have one button in my page.
If you are using ajax then (making it as simple as possible)
Add your loading gif image to html and make it hidden (using style in html itself now, you can add it to separate CSS):
Show the image when button is clicked and hide it again on success function
$('#buttonID').click(function(){
$('#img').show(); //<----here
$.ajax({
....
success:function(result){
$('#img').hide(); //<--- hide again
}
}
Make sure you hide the image on ajax error callbacks too to make sure the gif hides even if the ajax fails.