Javascript 在iframe中定义top
我有这样一个“弗兰肯斯坦”代码:Javascript 在iframe中定义top,javascript,html,iframe,Javascript,Html,Iframe,我有这样一个“弗兰肯斯坦”代码: <body> <iframe src="ThirdPartyPage"> <... id="foo"> <iframe> ... top.document.getElementById(foo) ... </iframe> </iframe> </body> ... top.document.getEleme
<body>
<iframe src="ThirdPartyPage">
<... id="foo">
<iframe>
...
top.document.getElementById(foo)
...
</iframe>
</iframe>
</body>
...
top.document.getElementById(foo)
...
有没有一种方法可以告诉javascript,顶部不是我的代码,而是第三方页面
编辑:这里的问题不是如何获得“foo”。这里的问题是,第三方页面试图从顶部获取“foo”,但现在有了新的iframe,第三方页面中的顶部不再是。您不能重写
top
,也不能使用getElementById(foo)访问任何DOM元素
从iframe
你不能重写top
,也不能使用getElementById(foo)
从你要找的iframe
而不是window.top.你要找的是window.top,而不是window.top.getElementById(foo)在第三方页面的新iframe之前工作。但是对于新的iframe,现在顶部不同了,并且正在破坏代码。getElementById(foo)在新的iframe之前在第三方页面上工作。但是有了新的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")