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

javascript - Access container of parent iframe element (Iframe inside iframe) - Stack Overflow

programmeradmin4浏览0评论

I have next structure of elements:

<div class="container">
  <iframe class="outer">
    ..
      <iframe class="inner">
        actual markup with scripts here
      </iframe>
  </iframe>
</div>

using pure javascript, i've meneged to get out of "inner" iframe into outer one:

//this is HTML tag of ".outer" iframe
var parent = window.parent.document.getElementsByTagName('html')[0];

but still i need to get to ".container" element in order to manipulate it. Can anyone tell me how to get ".container" element from script inside ".inner" element??

I have next structure of elements:

<div class="container">
  <iframe class="outer">
    ..
      <iframe class="inner">
        actual markup with scripts here
      </iframe>
  </iframe>
</div>

using pure javascript, i've meneged to get out of "inner" iframe into outer one:

//this is HTML tag of ".outer" iframe
var parent = window.parent.document.getElementsByTagName('html')[0];

but still i need to get to ".container" element in order to manipulate it. Can anyone tell me how to get ".container" element from script inside ".inner" element??

Share Improve this question asked Feb 14, 2014 at 13:12 KamiliusKamilius 58716 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Maybe this:

var outer = window.parent;

var mainWindow = outer.parent;

var container = mainWindow.document.getElementsByClassName('container')[0];

but dont forget that your iframes have to be on the same domain.

I would try

var containerElement = window.frames.top.top.document.getElementsByClassName('container')[0];

But be careful: If you try to break out of your frame and want to access a document with a different domain, ports or protocol, your browser (hopefully) will return an exception like this:

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

发布评论

评论列表(0)

  1. 暂无评论