Javascript 在IFrame窗口中调用JS函数

Javascript 在IFrame窗口中调用JS函数,javascript,function,iframe,Javascript,Function,Iframe,我有一个JS函数,有时我想在主窗口的上下文中调用,有时在客户端窗口中调用。如下所示: var f = function() { alert(window.location); }; f(); // should show the location of the parent frame // DOESN'T WORK, but is intended to show the location of the first IFrame f.call(window.frames[0]); //

我有一个JS函数,有时我想在主窗口的上下文中调用,有时在客户端窗口中调用。如下所示:

var f = function() { alert(window.location); };

f(); // should show the location of the parent frame

// DOESN'T WORK, but is intended to show the location of the first IFrame
f.call(window.frames[0]);
// reference to window in iframe with id or name 'myIframe'
var win = window.frames['myIframe'];

// invoke function in win
win.getEvents();

谷歌搜索主要是向我展示如何调用在子框架中定义的函数。一、 但是,您希望获取在父帧中定义的函数并在子帧中执行它。为了消除显而易见的,将
窗口
参数添加到函数中不是一个可行的选项。

如果要调用父页面全局范围内的函数,请将
窗口
替换为
顶部

如果要调用父页面全局范围内的函数,请将
窗口
替换为
顶部
第页。

如果要在父窗口中但在窗口上下文中执行定义的函数,可以使用:


这两个窗口必须在同一个域中才能工作。

如果要执行父窗口中定义的函数,但要在窗口上下文中执行,可以使用:


这两个窗口必须在同一个域中才能工作。

您是否尝试过以下方法:

var f = function() { alert(window.location); };

f(); // should show the location of the parent frame

// DOESN'T WORK, but is intended to show the location of the first IFrame
f.call(window.frames[0]);
// reference to window in iframe with id or name 'myIframe'
var win = window.frames['myIframe'];

// invoke function in win
win.getEvents();

您是否尝试过以下方法:

var f = function() { alert(window.location); };

f(); // should show the location of the parent frame

// DOESN'T WORK, but is intended to show the location of the first IFrame
f.call(window.frames[0]);
// reference to window in iframe with id or name 'myIframe'
var win = window.frames['myIframe'];

// invoke function in win
win.getEvents();