Q:

How to listen for the http request by A html element?

I use an <a> element to download a file in HTML page.
It takes some seconds, so I want to show a loading by listening to the http response.
Is there a way to show loading when clicked <a> element, and close loading when download completed?
This is what I want: 
<a href="https://..." download>download file</a>

A:

There are two ways :

Use ajax for listening event or receive data from Back-end :

$(document).ready(function(){
  $("button").click(function(){
    $.ajax({url: "/getData", success: function(result){
      $("#div1").html(result);
    }});
  });
});

In addition , you can use socket.io to listen event from Back-end .

At server send event :
socket.emit('sendData', 'Your Data');

At client listen event :
 socket.on('sendData', function(data) {
        console.log(data); // Your Data
      });

