在iframe中定义top

define top in iframe

本文关键字:top 定义 iframe      更新时间:2023-09-26

我有一个"弗兰肯斯坦"代码,如下所示:

<body>
  <iframe src="ThirdPartyPage">
    <... id="foo">
    <iframe>
      ...
      top.document.getElementById(foo)
      ...
    </iframe>
  </iframe>
</body>

有没有办法对javascript说顶部不是我的代码,而是ThirdPartyPage?

编辑:这里的问题不在于如何获得"foo"。这里的问题是,第三方页面正在字符串中从顶部获取"foo",但现在有了新的iframe,顶部就不再是第三方页了。

//To get the parent node i.e. ThirdPartyPage
document.getElementById("yourFrameId").parentNode 
//Also you could get the elements within the ThirdPartyPage as follows (returns an array of elements)
document.getElementById("yourFrameId").parentNode.getElementsByTagName("tagName")

您不能重写top,也不能使用getElementById(foo)iframe 访问任何DOM元素

您要找的是window.parent,而不是window.top.