My problem is similar to this question but I need an answer in javascript. It is very simple to achieve this in jquery. I also debug the jquery file but it seems more confusing to me.
//if there is any ajax request
document.getElementById("loader").style.display="block";
//on success:
document.getElementById("loader").style.display="none";