使用window.frames [name]进行访问时,iframe contentWindow未定义 [英] iframe contentWindow is undefined when use window.frames[name] to access

查看:1056
本文介绍了使用window.frames [name]进行访问时,iframe contentWindow未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用以下方式获取contentWindow,则该值未定义

 < html> 
< head>
< title> iframe test< / title>
< / head>
< body>
< iframe id =frame1src =frame1.htmlname =frame1>< / iframe>
< script>
document.body.onload = function(){
console.info(index loaded);
var frame1 = window.frames [frame1];
console.info(frame1.contentWindow);
}
< / script>
< / body>
< / html>

如果使用下面的其他方式,它可以正常工作:

  var frame1 = document.getElementById(frame1); 
console.info(frame1.contentWindow);

我在FF 29.0.1,chrome 34,IE11上测试过,它们的工作方式都是一样的。 / p>

所以我有两个问题:


  1. 为什么第一种方式无法获得contentWindow值

  2. iframe.contentWindow在所有浏览器中都兼容吗?


解决方案

  window.frames [frame1] ; 

contentWindow ,它会得到一个命名窗口,在你的情况下,它与

  document.getElementById(frame1)。contentWindow 

FIDDLE


If use following way to get the contentWindow, the value is undefined

<html>
<head>
    <title>iframe test</title>
</head>
<body>
    <iframe id="frame1" src="frame1.html" name="frame1"></iframe>
<script>
    document.body.onload = function() {
        console.info("index loaded");
        var frame1 = window.frames["frame1"];
        console.info(frame1.contentWindow);
    }
</script>
</body>
</html>

If use the other way like following, it works fine:

var frame1 = document.getElementById("frame1");
console.info(frame1.contentWindow);

I tested on FF 29.0.1, chrome 34, IE11, they all work the same way.

So I have two questions:

  1. why the first way can't get contentWindow value
  2. iframe.contentWindow is compatible in all browser?

解决方案

window.frames["frame1"];

is the contentWindow, it get's a named window, and in your case it's the same thing as

document.getElementById("frame1").contentWindow

FIDDLE

这篇关于使用window.frames [name]进行访问时,iframe contentWindow未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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