2012-04-13 18 views
0

我有這樣的功能:fireEvent到top.document.body

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"); 

我該怎麼做?

回答

0

此問題的解決方案是使用postMessage的HTML5。

我發現這個範例: http://robertnyman.com/html5/postMessage/postMessage.html

+0

這是一個完整的使用postMessage控制Clien-Side中的TimeOut的實現示例http://forums.ext.net/showthread.php?18578-How-To-Timeout-(Client-Side)-using-postMessage- (HTML5)-with-Countdown-alert&p = 80171#post80171 – Equiman 2012-04-23 15:22:31

0

我會在您的iframe中創建一個虛構事件並將其附加到目標鏈接或框架。

正如你可能也注意到,觸發事件的方式取決於標準其次是測試瀏覽器:

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

請查看更多詳情here

希望它可以幫助或至少給你一個提示,因爲我不確定完全瞭解您的需求。

+0

不起作用...有相同的結果。我需要的是在母版頁中運行代碼,但從iframe頁面中啓動。 – Equiman 2012-04-17 20:04:10

0

您可以創造在所需的iframe的包裝fireevent方法,並從包裝方法調用Windows火災事件。

從除此之外的任何iframe中調用此包裝。它會在所需的iframe上觸發事件。

+0

問題是我沒有遍佈同一頁面。這就是爲什麼需要postMessage。 – Equiman 2013-10-17 19:46:25

相關問題