title: Get Element bound to beforeunload Function
body: <p>I have a number of widgets loaded onto a widget grid. Each widget has a JavaScript method which will refresh every 'x' seconds. This is done by calling setInterval on the Javascript method for each widget. The setInterval is called against the window:</p>
  
  <pre><code>window.[FunctionName] = setInterval(FunctionName, RefreshTime * 1000);
  </code></pre>
  
  <p>where <code>FunctionName</code> is the name of the JavaScript method, <code>RefreshTime</code> is the time before refreshing in seconds.</p>
  
  <p>On removal of each widget, the name of the Javascript function is found, and the interval is cleared as below:</p>
  
  <pre><code>    $(".grid").on("click", ".panel-close", function () {
          var widget = $(this).parent();
          var jscrpt = $(widget).find(".RefreshMethod");
          if (jscrpt.length &gt; 0) {
              window[jscrpt.data("name")] = null;
              clearInterval(window[jscrpt.data("interval")]);
          }
          RemoveWidget(widget);
      })
  </code></pre>
  
  <p>Since the <code>jscrpt</code> element has the <code>name</code> attribute the name of the method can be found and the interval cleared when the widget is removed, preventing further refreshing. This works well.</p>
  
  <p>However, if the page is ajax loaded and the entire grid of widgets is removed, there is no remove event. I begun investigating using the <code>beforeunload</code> event as below:</p>
  
  <pre><code>    $(".grid .widget").bind("beforeunload", function (event) {
          var widget = $(this);
          var jscrpt = $(widget).find(".RefreshMethod");
          if (jscrpt.length &gt; 0) {
              window[jscrpt.data("name")] = null;
              clearInterval(window[jscrpt.data("interval")]);
          }
      })
  </code></pre>
  
  <p>This function is called when the elements are about to be cleared. This works when the widget is removed or if the elements are overwritten. However, using <code>$(this)</code> does not work - the object is a "beforeunload" type object. <code>event</code> is also undefined - I can't see anyway of identifying which widget is being unloaded. </p>
  
  <p>The question is, when I bind the <code>beforeunload</code> function to an element, when the function is triggered how do I identify which element has triggered the function?</p>