How to manage an event unload
<p>I want to insert in my page an event that trigger when the window close or change except that in the case that has been triggered by a submit button of a particular submit which bring in an analogus page. I tried the follow but it doesn't work. (prova is the name of submit).</p>

<pre><code>$(window).bind("unload", function (event) {
  alert('1');
  if (event.target.tagName != "prova") {
    alert('2');
    var postData = {
      'chat_message': "I left"
    };
    $.ajax({
      type: "POST",
      dataType: "json",
      url: base_url + "chat/ajax_add_message/" + chat_id + "/" + user_id,
      data: postData,
      success: function (data) {
        get_messages();
      }
    });

  }
});    
</code></pre>
