从 iframe 外部调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5140696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:07:19  来源:igfitidea点击:

call javascript function from outside an iframe

javascriptiframe

提问by Hector Barbossa

I have one entire html openning inside an iframe that contains a javascript function getData().Now I am not sure how to call getData() from outside that frame.Also is it possible to call it from an external javascript file ?

我在 iframe 中打开了一个完整的 html,其中包含一个 javascript 函数 getData()。现在我不确定如何从该框架外部调用 getData()。另外是否可以从外部 javascript 文件中调用它?

回答by johnhunter

You can get a reference to the frame window object from the window.frames property. See https://developer.mozilla.org/en/DOM/window.frames

您可以从 window.frames 属性获取对框架窗口对象的引用。请参阅https://developer.mozilla.org/en/DOM/window.frames

UPDATE:

更新:

You can access the global context of a named iframe with window[framename]. e.g:

您可以使用window[framename]. 例如:

<iframe src="data.html" name="data"></iframe>

<script>
var myData = window.data.getData();
</script>

Although you will need to make sure the iframe has loaded.

尽管您需要确保 iframe 已加载。

In jQuery you can use the contents method if you want access to the iframe DOM:

在 jQuery 中,如果你想访问 iframe DOM,你可以使用 contents 方法:

$("iframe").contents()

All this is assuming the frame hosted within the same domain.

所有这些都假设框架托管在同一域中

UPDATE[2]:

更新[2]:

You asked if it is possible to call the getDatafunction from an external js file. The answer is yes (if I understand you correctly). Here is an example:

您询问是否可以getData从外部 js 文件调用该函数。答案是肯定的(如果我理解正确的话)。下面是一个例子:

<html>
<head>
    <meta charset="utf-8">
    <title>parent page</title>
</head>
<body>

    <iframe src="data.html" name="data"></iframe>
    <script src="getdata.js"></script>

</body>
</html>

Then in the getdata.jsfile you have:

然后在getdata.js您的文件中:

var dataFrame = window.data;

// when the frame has loaded then call getData()
dataFrame.onload = function () {
    var myData = dataFrame.getData();
    // do something with myData..
}

Hope this answers your question :)

希望这能回答你的问题:)

回答by Kumar

in these cases you name your iframe and the main body that uses/launches frame and then use parent.objectname, in JS everything is Object and you should be able to call getData()

在这些情况下,您命名 iframe 和使用/启动框架的主体,然后使用 parent.objectname,在 JS 中一切都是 Object,您应该能够调用 getData()

a quick googling led me to this -> http://www.esqsoft.com/javascript_examples/iframe_talks_to_parent/

一个快速的谷歌搜索让我找到了这个 -> http://www.esqsoft.com/javascript_examples/iframe_talks_to_parent/

回答by Lalit Kumar Maurya

In certain situation there could be a neccessity of calling a javascript function inside an iframe from the parent document, and vice versa ie; calling a javascript function in parent document from the iframe.

在某些情况下,可能需要从父文档调用 iframe 内的 javascript 函数,反之亦然,即;从 iframe 调用父文档中的 javascript 函数。

For example;the parent document have an iframe with id attribute ‘iFrameId‘, and the function ‘functionInIframe()‘ is defined in that iframe document. Following code can call that iframe function from the parent document itself.

例如; 父文档有一个带有 id 属性“iFrameId”的 iframe,函数“functionInIframe()”在该 iframe 文档中定义。以下代码可以从父文档本身调用该 iframe 函数。

document.getElementById('iFrameId').contentWindow.functionInIframe();

And following code can call the function defined in parent document(functionInParent()) from the iframe itself.

并且以下代码可以从 iframe 本身调用父文档(functionInParent())中定义的函数。

parent.functionInParent();

This way javascript can interact between parent document and iframe.

这样 javascript 就可以在父文档和 iframe 之间进行交互。

This is the original post.

这是原帖