title: html5 javascript- How to catch attempt to initiate navigation out of sandboxed iframe?
body: <p>I have an sandboxed iframe that doesn't allow changing location:</p>
  
  <pre><code>&lt;iframe sandbox="allow-forms allow-popups allow-pointer-lock allow-same-origin allow-scripts" class="iframe visible" src="thesource.html" width="100%" scrolling="auto" frameborder="0"&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>If the iframe tries to unframe itself or change location I see a blank page because the browser stops the iframe's operation. This is the log from Chrome:</p>
  
  <blockquote>
    <p>Unsafe JavaScript attempt to initiate navigation for frame with URL '<a href="http://example.com" rel="noreferrer">http://example.com</a>' from frame with URL '<a href="http://otherdomaian.com" rel="noreferrer">http://otherdomaian.com</a>'. The frame attempting navigation of the top-level window is sandboxed, but the 'allow-top-navigation' flag is not set.</p>
  </blockquote>
  
  <p>That is great but I want to catch this so if it happens I'll move to the next iframe. So how do I catch this attempt?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I added a <a href="http://jsfiddle.net/62xfT/" rel="noreferrer">jsfiddle code</a> (check the error in the console log)</p>
  
  <p>I also tried to listen for an event with no success:</p>
  
  <pre><code>document.addEventListener('error', receiveMessage, true);
  function receiveMessage(error) {
      alert("iframe tried to unframe itself");
  }
  </code></pre>