从iFrame中加载的外部页面访问IFrame元素

时间:2022-08-23 10:11:39

I would like access the IFrame element available in the main page from the IFrame source page using JavaScript

我想使用JavaScript从IFrame源页面访问主页面中的IFrame元素

Here is my main Page

这是我的主要页面

<html>
<body>
<IFrame id="page-container" src="http://*.com"
      width="200px" height="200px">
</IFrame>
</body>
</html>

Child Page

儿童页面

 <html>
   <head>
   <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"/>
   <script type="text/javascript" >
   $(document).ready(function(){
    var containerFrame= $('#page-container');
    //var containerFrame=document.frames["page-container"];
    //var containerFrame=document.frames["page-container"];
    //var containerFrame=document.parent.frames["page-container"];
   });
   </script>

   <body>
   <div>some content..</div>
   </body>
   </html>

I am getting undefined for all my tries.

我的所有尝试都未定义。

How it can be done? Is it possible?

怎么做?可能吗?

Edit: I tried loading a cross domain page.

编辑:我尝试加载跨域页面。

In IE i am getting error ' for security reason framing is not allowed' and also domains, protocols and port must match. Can we achieve this any way?

在IE中我收到错误'出于安全原因不允许框架',并且域,协议和端口必须匹配。我们可以做到这一点吗?

2 个解决方案

#1


2  

var containerFrame = $('#page-container', window.parent.document)

This should firstly reference the parent of the window and then look for the iframe div

这应首先引用窗口的父级,然后查找iframe div

#2


1  

You need to specify the context to search for #page-container in, which in this case will be the parent window.

您需要指定搜索#page-container in的上下文,在这种情况下,它将是父窗口。

var containerFrame = $('#page-container', window.parent.document);

#1


2  

var containerFrame = $('#page-container', window.parent.document)

This should firstly reference the parent of the window and then look for the iframe div

这应首先引用窗口的父级,然后查找iframe div

#2


1  

You need to specify the context to search for #page-container in, which in this case will be the parent window.

您需要指定搜索#page-container in的上下文,在这种情况下,它将是父窗口。

var containerFrame = $('#page-container', window.parent.document);