在 Chrome 中,无法访问子 iframe 或 iframe 父级的任何变量或函数
这与这个问题类似,但范围更广一些。
我只是在本地打开这些页面,它们位于同一个文件夹中。
index.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>TestIndex</title>
<script type="text/javascript">
function init()
{
alert("child.childvar: " + child.childvar); //works in FF, IE, not Chrome
alert("frames['child'].childvar: " + frames['child'].childvar); //works in FF, IE, not Chrome
alert("document.getElementById('child').contentWindow['childvar']: " + document.getElementById('child').contentWindow['childvar']); //works in FF, IE, not Chrome
child.childfunc(); //works in FF, IE, not Chrome
frames['child'].childfunc(); //works in FF, IE, not Chrome
document.getElementById('child').contentWindow['childfunc()']; //doesn't work in anything
}
var parentvar = 7;
function parentfunc()
{
alert("In parentfunc");
}
window.onload = init;
</script>
</head>
<body>
<iframe id="child" name="child" src="child.html">Your browser does not support iframes</iframe>
</body>
</html>
child.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>TestChild</title>
<script type="text/javascript">
function init()
{
alert("parent.parentvar: " + parent.parentvar); //works in FF, IE, not Chrome
parent.parentfunc(); //works in FF, IE, not Chrome
}
var childvar = 5;
function childfunc()
{
alert("In childfunc");
}
window.onload = init;
</script>
</head>
<body>
</body>
</html>
我似乎无法在 Chrome 中的页面与其 iframe 内容之间实现任何通信。我确实阅读了我链接到的问题的答案,但我真的不知道用户脚本/内容脚本是什么,所以我不知道这些问题与我的相关性如何。
我想我真正的问题是:我到底如何从 iframe 页面获取值到父页面!?
This is similar to this question, although a bit broader.
I'm just opening these pages locally, and they sit in the same folder.
index.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>TestIndex</title>
<script type="text/javascript">
function init()
{
alert("child.childvar: " + child.childvar); //works in FF, IE, not Chrome
alert("frames['child'].childvar: " + frames['child'].childvar); //works in FF, IE, not Chrome
alert("document.getElementById('child').contentWindow['childvar']: " + document.getElementById('child').contentWindow['childvar']); //works in FF, IE, not Chrome
child.childfunc(); //works in FF, IE, not Chrome
frames['child'].childfunc(); //works in FF, IE, not Chrome
document.getElementById('child').contentWindow['childfunc()']; //doesn't work in anything
}
var parentvar = 7;
function parentfunc()
{
alert("In parentfunc");
}
window.onload = init;
</script>
</head>
<body>
<iframe id="child" name="child" src="child.html">Your browser does not support iframes</iframe>
</body>
</html>
child.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>TestChild</title>
<script type="text/javascript">
function init()
{
alert("parent.parentvar: " + parent.parentvar); //works in FF, IE, not Chrome
parent.parentfunc(); //works in FF, IE, not Chrome
}
var childvar = 5;
function childfunc()
{
alert("In childfunc");
}
window.onload = init;
</script>
</head>
<body>
</body>
</html>
I can't seem to achieve any communication at all between a page and its iframe's content in chrome. I did read the answers to the question I linked to, but I don't really know what userscripts/content scripts are, so I don't know how relevant that questions was to mine.
I guess my actual question is: how the hell do I get values from an iframe'd page into the parent page!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,在本地文件系统上,javascript 帧间通信不起作用。将文件放在服务器上,它可能会按预期运行。
Apparently javascript inter-frame communication doesn't work when on the local file system. Put the files on a server and it will likely function as expected.