title: Foolproof way to detect if this page is INSIDE a cross-domain iframe
body: <p>An answer to "<a href="https://stackoverflow.com/questions/12381334/foolproof-way-to-detect-if-iframe-is-cross-domain"><strong>Foolproof way to detect if iframe is cross domain</strong></a>" describes a method to test if an iframe on a page points to a same-domain or cross-domain page, working around different browsers' interpretations of the cross-domain policy and avoiding error messages that would interrupt a user or stop javascript.</p>
  
  <p>I'm looking to do the equivalent of this but <em>from</em> the child page inside the iframe testing whether it is <em>inside</em> a cross-domain iframe or not. </p>
  
  <p>It's possible to <a href="https://stackoverflow.com/questions/3420004/access-parent-url-from-iframe">access (same domain) info about the parent</a> with the <code>parent</code> global e.g. <code>parent.document.location</code>, but is there a <strong>reliable way to do this cross-browser without crashing into errors</strong> when it detects that the parent is cross-domain? </p>
  
  <hr>
  
  <p>For handy testing, <strong>here's a <a href="http://jsbin.com/loyuy/1/edit" rel="noreferrer">jsbin</a> inside a <a href="http://jsfiddle.net/Ke3Z6/" rel="noreferrer">jsfiddle</a></strong> that crashes into an error when trying to test if <code>parent.location.host</code> is accessible. Is there a reliable way to get something usable like <code>false</code> telling us that this is a cross-domain parent, instead of an error? </p>
  
  <p>Is there a <code>try...catch</code> variant which will be robust cross-browser? Or maybe some clever trick using a return value from <code>parent.postMessage()</code>? (although the parent page cannot  be edited)</p>
  
  <hr>
  
  <p>Use case: imagine embedable content that is to be embedded on pages on its own site, and by third parties. If the page is on a same-domain iframe, we hide branding and links back to the original site because the user is already here. If they access the page from a 3rd party iframe or load the page directly, we show the branding and credits so they can see the source of the content. </p>
  
  <hr>
  
  <p>To clarify, I know about the same-domain policy and I don't care <em>what</em> the cross browser domain is, I'm just looking to write a simple but reliable <code>if</code> condition like <code>if( crossDomainParent() ){ /* do something */}</code></p>