I have an sandboxed iframe that doesn't allow changing location:
<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"></iframe>
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:
Unsafe JavaScript attempt to initiate navigation for frame with URL 'http://example.com' from frame with URL 'http://otherdomaian.com'. The frame attempting navigation of the top-level window is sandboxed, but the 'allow-top-navigation' flag is not set.
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?
EDIT:
I added a jsfiddle code (check the error in the console log)
I also tried to listen for an event with no success:
document.addEventListener('error', receiveMessage, true);
function receiveMessage(error) {
alert("iframe tried to unframe itself");
}
You can now do this with
allow-top-navigation-by-user-activation
I am new here so I don't have enough reputation to comment on answers and I apologize if I am doing this wrong, but the accepted solution will unfortunately not be able to accomplish what you are looking for.
I was able to demonstrate what I mean by having the script from the JSFiddle above run once the DOM is ready (there will be no alert but still an error in the console). Here's a little more detail on what happens currently with that fiddle:
The exception that is being caught has nothing to do with the iframe, it is actually a type error from trying to set the
src
property on anull
value.What you really want to do is catch the error inside of the iframe (when the sandboxed script tries to access
window.top
), but this is not possible because of the Same-origin policy. Btw, setting the"allow-same-origin"
sandbox flag only has any effect when the iframe content is being served from the sameorigin
as the top level document. E.g. as soon as thesrc
orlocation
of the iframe is changed to a differentorigin
, there's no way to touch anything inside.There are ways to communicate across
iframe
boundaries, such as withwindow.postMessage
or the older and hackier way of using the iframe'slocation.hash
, but I am assuming you can't affect the source of the page going into your iframe. (A nice developer would of course be open to suggestions and see that a feature like this could be useful.)The only way that I was able to catch this error without violating any browser security policies was to set the
allow-top-navigation
sandbox flag and then use thewindow.onbeforeunload
handler in the top level document to catch the navigation attempt from the childiframe
. I would never recommend this because the user experience is awful. There is no way to prevent the navigation without prompting the user about whether they want to leave the page or not. Proof of concept below:So unfortunately I can't find any ways to do this nicely in current browser implementations without help from your 3rd party content developer. I read some interesting things in the HTML5 spec that might allow us to do things like this in the future (and am unfortunately maxed out on my number of links I can insert here), so I would keep an eye out as things progress.
Example:
An with extra restrictions:
the sandbox attribute is supported in Internet Explorer 10, Firefox, Chrome, and Safari.
Definition and Usage
Differences Between HTML 4.01 and HTML5
Syntax
Attribute Values
javascript: is a kind of weird URI protocol. It works in some contexts, like , but not all - for instance, a window's location can not be set to such a URI. (While you can assign a javascript: URI to window.location as a really roundabout way of running a script, the window's location doesn't stay set to that value.)
To write content into an IFRAME, get a reference to the frame's document and write to it. Doing so will require that you set the allow-same-origin sandbox flag.
Live example: http://jsfiddle.net/wUvrF/1/