我想从iframe调用父窗口JavaScript函数。
<script>
function abc()
{
alert("sss");
}
</script>
<iframe id="myFrame">
<a onclick="abc();" href="#">Call Me</a>
</iframe>
我想从iframe调用父窗口JavaScript函数。
<script>
function abc()
{
alert("sss");
}
</script>
<iframe id="myFrame">
<a onclick="abc();" href="#">Call Me</a>
</iframe>
当前回答
<a onclick="parent.abc();" href="#" >Call Me </a>
看到window.parent
返回当前窗口或子帧的父窗口的引用。
如果窗口没有父属性,则其父属性是对自身的引用。
当一个窗口在<iframe>, <object>,或<frame>中加载时,它的父窗口是包含嵌入该窗口的元素的窗口。
其他回答
最近,我不得不找出为什么这种方法也不起作用。
你想从子iframe调用的javascript需要在父iframe的头部。如果它在主体中,则脚本在全局作用域中不可用。
<head>
<script>
function abc() {
alert("sss");
}
</script>
</head>
<body>
<iframe id="myFrame">
<a onclick="parent.abc();" href="#">Click Me</a>
</iframe>
</body>
希望这能帮助到再次遇到这个问题的人。
我把这个作为一个单独的答案,因为它与我现有的答案无关。
这个问题最近在从引用子域的iframe访问父节点时再次出现,现有的修复不起作用。
这一次的答案是修改文件。父页面的域和iframe必须相同。这将使同源策略检查误以为它们共存于完全相同的域中(子域被视为不同的主机,同源策略检查失败)。
将以下内容插入到iframe中页面的<head>以匹配父域(根据您的doctype进行调整)。
<script>
document.domain = "mydomain.com";
</script>
请注意,这将在本地主机开发时抛出一个错误,所以使用如下检查来避免错误:
if (!window.location.href.match(/localhost/gi)) {
document.domain = "mydomain.com";
}
Ash Clarke为子域名提供的解决方案很好,但请注意,您需要包括文档。域名= "mydomain.com";在iframe页眉和父页眉中,如链接同源策略检查中所述
An important extension to the same origin policy implemented for JavaScript DOM access (but not for most of the other flavors of same-origin checks) is that two sites sharing a common top-level domain may opt to communicate despite failing the "same host" check by mutually setting their respective document.domain DOM property to the same qualified, right-hand fragment of their current host name. For example, if http://en.example.com/ and http://fr.example.com/ both set document.domain to "example.com", they would be from that point on considered same-origin for the purpose of DOM manipulation.
使用Firefox和Chrome浏览器,您可以使用:
<a href="whatever" target="_parent" onclick="myfunction()">
如果myfunction同时出现在iframe和parent中,父函数将被调用。
虽然其中一些解决方案可能有效,但没有一个符合最佳实践。许多分配全局变量,您可能会发现自己调用多个父变量或函数,导致混乱、易受攻击的命名空间。
为了避免这种情况,可以使用模块模式。在父窗口:
var myThing = {
var i = 0;
myFunction : function () {
// do something
}
};
var newThing = Object.create(myThing);
然后,在iframe中:
function myIframeFunction () {
parent.myThing.myFunction();
alert(parent.myThing.i);
};
这类似于Crockford的开创性文本“Javascript: the Good Parts”的继承章节中描述的模式。你也可以在w3的页面上了解更多Javascript的最佳实践。https://www.w3.org/wiki/JavaScript_best_practices#Avoid_globals