从iframe到top.document.body的fireEvent

时间:2012-04-13 00:05:47

标签: javascript javascript-events

我有这个功能:

function doSomething(e) {
    if (!e) var e = window.event;
    alert(e.type);
}

当HTML(Master.html)页面准备就绪时......我打电话给对方:

var iniciarTimeOut = function (e) {
    document.onload = doSomething;
    window.onmousedown = doSomething;
    window.onkeypress = doSomething;
};

我有一个iframe进入Master.html,我希望将事件检测到iframe.html但是在Master.html中触发事件我正在尝试这个:

document.onmousedown = top.document.body.fireEvent("onload");
document.onkeypress = top.document.body.fireEvent("onmousedown");
document.onload = top.document.body.fireEvent("onload");

我该怎么做?

3 个答案:

答案 0 :(得分:0)

我会在你的iframe中创建一个虚构事件,并将其附加到目标链接或框架。

您可能还会注意到,触发事件的方式取决于测试浏览器遵循的标准:

 // Attach the fictive event object to the target
    // MSIE
    if (document.createEventObject )
        YourTarget.fireEvent('onmouseup',oEvt) ;
    // W3C
    else
        YourTarget.dispatchEvent(oEvt) ;

请参阅更多详情here

希望它有帮助或者至少给你一个提示,因为我不确定是否完全了解你的需要。

答案 1 :(得分:0)

此问题的解决方案是使用postMessage的HTML5。

我在这里找到了一个例子: http://robertnyman.com/html5/postMessage/postMessage.html

答案 2 :(得分:0)

您可以在所需的iframe中创建包装器fireevent方法,并从该包装器方法调用windows fire事件。

从除此之外的任何iframe调用此包装器。它会在所需的iframe上触发事件。