Show "loading" image when clicking a link

Show "loading" image when clicking a link

There's an outgoing link on my website that is sometimes slow to respond. I want to show a spinning wheel image after clicking it. Here is how I do it:

  1. $("#thelink").click(function() {
  2.    $("#somediv").addClass('loading');
  3. });

There are 2 problems:

Firefox doesn't remove the "loading" class when going to another page and clicking the browser's Back button. Any ideas how to solve it?

Google Chrome doesn't add the class "loading". Instead it immediately opens the page targeted by the link. I solved this by adding a "delay()". Is this the best approach?


  1. $("#thelink").click(function() {
  2.    $("#somediv").addClass('loading').delay();
  3. });