简体   繁体   English

我无法从iframe中调用父函数

[英]i'm unable to call the parent function from within iframe

i've seen the other posts in regard to this, but the following method is not working for some reason. 我已经看到了与此有关的其他帖子,但是由于某种原因,以下方法无效。

onclick="parent.testing();" 

now for the full story. 现在查看全文。 i have an index.html that houses all the JS files. 我有一个index.html,其中包含所有JS文件。 also within the index, i have an empty 'div' tag. 同样在索引中,我有一个空的'div'标签。 with jQuery, i'm appending a page1.html file to the empty 'div'. 使用jQuery,我将page1.html文件附加到空的“ div”。 so the index kinda looks like this: 所以索引有点像这样:

<html>
<head>
<script files>
<script>
ready { function testing(){do stuff;} }
</script>
</head>
<body>
<div><iframe src="page1.html" (added via jQuery)></div>
</body>
</html>

now in the page1.html file, i'm trying to call a function when a link is clicked. 现在在page1.html文件中,当我单击链接时,我正在尝试调用一个函数。 but i'm receiving the following error: 但我收到以下错误:

Uncaught TypeError: Cannot call method 'testing' of undefined 

i've also tried the following but they didn't work: 我也尝试了以下方法,但是它们没有起作用:

onclick="window.testing();"
onclick="window.frames[0].testing();" 

Your parent page script isn't valid JavaScript, but assuming ready { ... } : 您的父页面脚本不是有效的JavaScript,但假设ready { ... }

<script>
ready { function testing(){do stuff;} }
</script>

...is a simplification supposed to represent a document ready handler you are declaring testing() as a local function within that ready handler so it can only be accessed from within that ready handler. ...是一种简化形式,它代表了一个文档就绪处理程序,您正在将testing()声明为该就绪处理程序中的本地函数,因此只能从该就绪处理程序中对其进行访问。 You need to make testing() global in the parent page: 您需要在父页面中将testing()全局:

<script>
$(document).ready({ /* your on ready stuff here */ });

function testing(){ /* do stuff; */ }
</script>

...and then you should be able to call it from the iframe using parent.testing() . ...然后您应该可以使用parent.testing()从iframe调用它。

If I understand your question, your problem it's your function is not called, right? 如果我理解您的问题,那么您的问题就是您的函数没有被调用,对吧? So maybe a solution is to put this in your "page1.html" 因此,也许一个解决方案是将其放在您的“ page1.html”中

<script type="text/javascript" src="your_js_file"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM