加载的iframe [英] iframe that loads

查看:83
本文介绍了加载的iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将文件加载到iframe中时,当文件使用body标签时如何访问已加载文件的html项?

示例:
itest.html:

when I load a file into an iframe, how do I access the html-items of the loaded file when the file uses body tag?

Example:
itest.html:

<html>
<head>
<script>
function loadFrame()
 {var frm=document.getElementById('frmtest');
 frm.src="itest1.html";    // this alert works fine
 alert(frm.contentDocument.getElementById('whatever').innerHTML);
 frm.src="itest2.html";    // this alert does not work
 alert(frm.contentDocument.getElementById('whatever').innerHTML);
 }
</script>
</head>
<body>
<iframe id="frmtest"></iframe>
<button onclick='loadFrame();'>click me</button>
</body></html>
itest1.html:            <span id="whatever">THIS IS ME</span>
itest2.html:<html><body><span id="whatever">THIS IS ME</span></body></html>



加载itest1后的警报向我显示了跨度的innerHTML.
但是,加载itest2(使用应用程序中必不可少的html和body标签)后,警报显示我无法访问这些项目.
有人知道我如何访问itest2.html的项目吗?

谢谢Eke



The alert after loading itest1 shows me the innerHTML of the span.
However after loading itest2 (which uses html and body tags that are essential in my application) the alert shows that I can not access the items.
Does anybody know how I can acces the items of itest2.html?

Thanks Eke

推荐答案

这取决于iframe和页面的域.

如果域相同,则页面可以通过element.documentElement或类似方式访问iframe.
It depends on domains of both iframe and page.

If domain is the same, page can access iframe through element.documentElement or something like that.


我发现了真正的罪魁祸首:计时!!!但不是解决方案.

I found the real culprit: timing!!! But not the solution.

function loadFrame()
  {var frm=document.getElementById(''frmtest'');
  frm.src=''itest1.html'';
  if (frm.contentDocument)  
  alert(frm.contentDocument.getElementById(''whatever'').innerHTML);
  else  alert(frm.contentWindow.document.getElementById(''whatever'').innerHTML);
 }



仅适用于我介绍的微小示例itest1.html,但
从不获取更丰富的itest1.html.

但是,它在加载后插入元素之后并在访问元素之前,总是在插入延迟后起作用,如以下示例所示:



works only for the tiny example itest1.html that I presented, but
never for a more substantial itest1.html.

However it always works after inserting a delay after loading and before accessing the elements as in the following example:

function loadFrame()
 {var frm=document.getElementById(''frmtest'');
  frm.src=''itest1.html'';
  alert(''delay''): // INSERTED DELAY AFTER LOADING OF itest1.html
  if (frm.contentDocument)
    alert(frm.contentDocument.getElementById(''whatever'').innerHTML);
  else    alert(frm.contentWindow.document.getElementById(''whatever'').innerHTML); 
 }



显然,itest1.html中的元素(例如id = whatever)在命令frm.src = ...之后并不立即存在,而只是在延迟之后才存在.
因此,也许我应该将我的问题改写为如何访问加载到iframe中的文档的元素",改为何时(如何)访问....".

有人认识到这一发现,您如何处理呢?

在此先感谢...... Eke



Apparently the elements in itest1.html (such as id=whatever) do not exist immediately after the command frm.src=..., but only after a delay.
So maybe I should have rephrased my problem as "how to access the elements of a document loaded into an iframe" into "when(how) to access....".

Anybody recognizes this observation and how did you deal with it?

Thanks in advance......Eke


这篇关于加载的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆