admin管理员组文章数量:1134083
Some websites have code to "break out" of IFRAME
enclosures, meaning that if a page A
is loaded as an IFRAME
inside an parent page P
some Javascript in A
redirects the outer window to A
.
Typically this Javascript looks something like this:
<script type="text/javascript">
if (top.location.href != self.location.href)
top.location.href = self.location.href;
</script>
My question is: As the author of the parent page P
and not being the author of the inner page A
, how can I prevent A
from doing this break-out?
P.S. It seems to me like it ought to be a cross-site security violation, but it isn't.
Some websites have code to "break out" of IFRAME
enclosures, meaning that if a page A
is loaded as an IFRAME
inside an parent page P
some Javascript in A
redirects the outer window to A
.
Typically this Javascript looks something like this:
<script type="text/javascript">
if (top.location.href != self.location.href)
top.location.href = self.location.href;
</script>
My question is: As the author of the parent page P
and not being the author of the inner page A
, how can I prevent A
from doing this break-out?
P.S. It seems to me like it ought to be a cross-site security violation, but it isn't.
Share Improve this question asked Dec 15, 2008 at 19:50 Jason CohenJason Cohen 83k26 gold badges110 silver badges114 bronze badges 1- I don't think you can do much at all... if you are not the author of the frame content. – scunliffe Commented Dec 15, 2008 at 19:53
10 Answers
Reset to default 139With HTML5 the iframe sandbox attribute was added. At the time of writing this works on Chrome, Safari, Firefox and recent versions of IE and Opera but does pretty much what you want:
<iframe src="url" sandbox="allow-forms allow-scripts"></iframe>
If you want to allow top-level redirects specify sandbox="allow-top-navigation"
.
I use the sandbox
attribute on the iframe
element:
allow-forms
allow form submissionallow-popups
allows popupsallow-pointer-lock
allows pointer lockallow-same-origin
allows the document to maintain its originallow-scripts
allows JavaScript execution, and also allows features to trigger automaticallyallow-top-navigation
allows the document to break out of the frame by navigating the top-level window
Top navigation is what you want to prevent, so leave that out and it will not be allowed. Anything left out will be blocked
ex.
<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="http://www.example.com"</iframe>
Try using the onbeforeunload property, which will let the user choose whether he wants to navigate away from the page.
Example: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload
In HTML5 you can use sandbox property. Please see Pankrat's answer below. http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/
After reading the w3.org spec. I found the sandbox property.
You can set sandbox=""
, which prevents the iframe from redirecting. That being said it won't redirect the iframe either. You will lose the click essentially.
Example here: http://jsfiddle.net/ppkzS/1/
Example without sandbox: http://jsfiddle.net/ppkzS/
I know it has been a long time since question was done but here is my improved version it will wait 500ms for any subsequent call only when the iframe is loaded.
<script type="text/javasript">
var prevent_bust = false ;
var from_loading_204 = false;
var frame_loading = false;
var prevent_bust_timer = 0;
var primer = true;
window.onbeforeunload = function(event) {
prevent_bust = !from_loading_204 && frame_loading;
if(from_loading_204)from_loading_204 = false;
if(prevent_bust){
prevent_bust_timer=500;
}
}
function frameLoad(){
if(!primer){
from_loading_204 = true;
window.top.location = '/?204';
prevent_bust = false;
frame_loading = true;
prevent_bust_timer=1000;
}else{
primer = false;
}
}
setInterval(function() {
if (prevent_bust_timer>0) {
if(prevent_bust){
from_loading_204 = true;
window.top.location = '/?204';
prevent_bust = false;
}else if(prevent_bust_timer == 1){
frame_loading = false;
prevent_bust = false;
from_loading_204 = false;
prevent_bust_timer == 0;
}
}
prevent_bust_timer--;
if(prevent_bust_timer==-100) {
prevent_bust_timer = 0;
}
}, 1);
</script>
and onload="frameLoad()"
and onreadystatechange="frameLoad();"
must be added to the frame or iframe.
Since the page you load inside the iframe can execute the "break out" code with a setInterval, onbeforeunload might not be that practical, since it could flud the user with 'Are you sure you want to leave?' dialogs.
There is also the iframe security attribute which only works on IE & Opera
:(
In my case I want the user to visit the inner page so that server will see their ip as a visitor. If I use the php proxy technique I think that the inner page will see my server ip as a visitor so it is not good. The only solution I got so far is wilth onbeforeunload. Put this on your page:
<script type="text/javascript">
window.onbeforeunload = function () {
return "This will end your session";
}
</script>
This works both in firefox and ie, thats what I tested for. you will find versions using something like evt.return(whatever) crap... that doesn't work in firefox.
try:
<iframe sandbox=" "></iframe>
Maybe this would be useful for someone. As an owner of the parent page I didn't want the iframe to change it's location, so I subscribed for the onload
event of the iframe and correct it's src
if it has changed. The iframe blinks, but that was ok for my case.
var iframe = document.getElementById("iframeId");
var iframeLoaded = 0;
var originalUrl = "https://example.com";
iframe.onload = function () {
iframeLoaded++;
if(iframeLoaded > 1) {
iframeLoaded = 0;
iframe.setAttribute(originalUrl);
}
};
By doing so you'd be able to control any action of the framed page, which you cannot. Same-domain origin policy applies.
本文标签: javascriptHow to prevent IFRAME from redirecting toplevel windowStack Overflow
版权声明:本文标题:javascript - How to prevent IFRAME from redirecting top-level window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736776761a1952380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论