title: javascript iframe unload
body: <p>I have an iframe in my page , which this iframe is embedding a site which is not on my server.</p>
  
  <p>I'm looking for a way to trigger a function before the iframe is redirecting to. for example when the user is clicking in a link inside the iframe and the iframe is redirecting , i want to trigger a function BEFORE it's redirecting.</p>
  
  <p>i have this code so far:</p>
  
  <pre><code>document.getElementById("ifm").addEventListener("unload", trig());
  function trig(){
      alert('2');
  }
  </code></pre>
  
  <p>this code is alerting only once when the iframe is first loading.</p>
  
  <p>i was trying also with the "onbeforeunload" event and it gives the same result.</p>
  
  <p>question: how can i alerting BEFORE the iframe getting redirecting?</p>