Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从iframe到top.document.body的fireEvent_Javascript_Javascript Events - Fatal编程技术网

Javascript 从iframe到top.document.body的fireEvent

Javascript 从iframe到top.document.body的fireEvent,javascript,javascript-events,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.o

我有这个功能:

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;
};
我在Master.html中有一个Iframe,我想在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");

我该怎么做?

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

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

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


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

解决这个问题的方法是使用postMessage的HTML5

我在这里找到了一个例子:

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


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

不起作用。。。结果是一样的。我需要的是在母版页中运行一段代码,但从iframe页面启动。下面是一个完整的示例,说明了如何使用postMessage来控制客户端的超时。问题是我没有在同一页面上运行所有代码。这就是为什么我们需要postMessage。