最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - prevent iframe from changing parent location - Stack Overflow

programmeradmin1浏览0评论

Suppose contains <iframe src="">

Sometimes b/bar will break out of its frame: the top-level window will redirect away from a/foo into b/bar. I don't know how b is doing this.

I thought it wasn't possible for a cross-domain iframe to interfere with the parent unless the parent cooperates via postMessage. Is changing the window location exempt from this?

If that is what's happening, how can a prevent this redirect? I control a but cannot modify b.

If that's not what's happening, how can I find out what b is doing to achieve the redirect?

It would be acceptable to force b to load itself into a new window, or sabotage its access to the top or parent objects. Other degradations of b's behaviour may be acceptable.

Suppose http://a./foo contains <iframe src="http://b./bar">

Sometimes b./bar will break out of its frame: the top-level window will redirect away from a./foo into b./bar. I don't know how b. is doing this.

I thought it wasn't possible for a cross-domain iframe to interfere with the parent unless the parent cooperates via postMessage. Is changing the window location exempt from this?

If that is what's happening, how can a. prevent this redirect? I control a. but cannot modify b..

If that's not what's happening, how can I find out what b. is doing to achieve the redirect?

It would be acceptable to force b. to load itself into a new window, or sabotage its access to the top or parent objects. Other degradations of b.'s behaviour may be acceptable.

Share Improve this question edited Mar 30, 2012 at 17:10 spraff asked Mar 30, 2012 at 16:36 spraffspraff 33.5k27 gold badges135 silver badges255 bronze badges 3
  • Of possible interest may be this classic question – Pointy Commented Mar 30, 2012 at 16:40
  • Interesting, although the basic anti-frame-busting 204 trick doesn't work in my FF11 (all navigations away are blocked). – spraff Commented Mar 30, 2012 at 16:58
  • The page ultimately will win; even if it can't bust your frame buster buster, it can decide to hide itself or redirect to something objectionable. – Pointy Commented Mar 30, 2012 at 17:04
Add a ment  | 

4 Answers 4

Reset to default 3

Parent can use sandbox attribute on iframe:
http://www.w3schools./TAgs/att_iframe_sandbox.asp

Then iframe will no longer be able to redirect parent.

What may be happening is a link inside b. has the attribute Target='_parent'. This would make the window/iframe parent update.

If you control b. - ensure all your targets are _self.

that's called 'frame busting'. Supposedly there are techniques to stop that ( http://seclab.stanford.edu/websec/framebusting/index.php ).

Maybe instead of an iframe you could use ajax or similar to load the frame's content into your page?

An ajax solution could work if I create a./proxy.php which will use cURL to fetch b..

Pros:

  • can change or remove any script
  • can adjust b. content (preview mode)
  • can get around cross-domain iframe restrictions

Cons:

  • cookies/sessions on b. won't be available
  • b. may require scripts to create its content
  • burden of bandwidth
  • coding effort to make hrefs work
发布评论

评论列表(0)

  1. 暂无评论