Detect or cancel iframe "break out"

Hi,

I would like to know how can I detect/prevent iframe break out. Like for example, then the iframe external source has a javascript like:

<script type="text/javascript">
  if (top.location.href != self.location.href)
     top.location.href = self.location.href;
</script>

How to detect/prevent it? I found the following through google, but is not working. This goes into the page containing the external source iframe.

window.onbeforeunload = function (evt) {
  var message = 'Are you sure you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}

Something I would like to point out, as I seen such responses in most google results regarding this… something along the lines of “you shouldn’t be showing external content that you do not own.”

I shouldn’t be using an external site within iframe? yes, I should! why? I would have consent from the author. But this check is necessary because I can’t tell otherwise if the author added that iframe breaker after manual site verification. :slight_smile:

Please help, lately all my sitepoint questions go unsolved :frowning:

Web browsers no longer allow that type of control over framed content.

But then the following still works, so no way to work around this?

<script type=“text/javascript”>
if (top.location.href != self.location.href)
top.location.href = self.location.href;
</script>

edit

In firefox, ie, and opera the iframe is not breaking out, but in safari and chrome it does, any suggestions?