2

我有一個Flash電影嵌入在一個HTML容器swfobject。 通過ExternalInterface我已經註冊了一個JavaScript函數來觸發我的Flash應用程序的回調。IE onbeforeunload不發射ExtenralInterface回調

ExternalInterface.addCallback("notifyClose", notifyOnClose); 

JavaScript函數作爲事件偵聽器添加以觸發onb​​eforeunload。

<script language="JavaScript"> 

     function getSWF(movieName) { 
      if (navigator.appName.indexOf("Microsoft") != -1){ 
       return window[movieName]; 
      }else { return document[movieName];} 
     }   

     var bye = function() { 
      getSWF('flashContainer').notifyClose('> WE ARE CLOSING APP!'); 
      //alert('WE ARE CLOSING APP!.'); 
     }; 

     var hola = function(){ 
      getSWF('flashContainer').notifyClose('> WE ARE opening APP!'); 
      alert('WE ARE opening APP!.'); 
     }; 

     if(window.addEventListener) { 
      window.addEventListener('load', hola,false); 
      window.addEventListener('beforeunload', bye, false); 
     } else if (window.attachEvent) { 
      window.attachEvent('onload', hola); 
      window.attachEvent('onbeforeunload', bye); 
     } 

</script> 

我已經在FF和IE中測試過。 Firefox按預期工作,但不在IE中。 在IE中,我在Flash中收到onload消息通知,但不是onbeforeunload。

這是沙箱限制的一些王嗎?只是不好的代碼?

+0

另外,如果我有一個Ajax調用替換再見功能ExternalInterface調用,它也不起作用。 – goliatone 2010-02-13 11:07:15

回答

-1

問題是您的代碼在attachEvent()中的「on」。

if(window.addEventListener) { 
      window.addEventListener('load', hola,false); 
      window.addEventListener('beforeunload', bye, false); 
     } else if (window.attachEvent) { 
      window.attachEvent('onload', hola); 
      window.attachEvent('onbeforeunload', bye); 
     } 

嘗試類似下面的代碼事件偵聽器代碼,並請參閱下面的鏈接瞭解更多信息:http://bytes.com/topic/javascript/answers/147027-addeventlistener-function-ie

//*** This code is copyright 2003 by Gavin Kistner, [email protected] 
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt 
//*** Reuse or modification is free provided you abide by the terms of that license. 
//*** (Including the first two lines above in your source code satisfies the conditions.) 


//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted 
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false); 

function AttachEvent(obj,evt,fnc,useCapture){ 
    if (!useCapture) useCapture=false; 
    if (obj.addEventListener){ 
     obj.addEventListener(evt,fnc,useCapture); 
     return true; 
    } else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc); 
    else{ 
     MyAttachEvent(obj,evt,fnc); 
     obj['on'+evt]=function(){ MyFireEvent(obj,evt) }; 
    } 
} 

//The following are for browsers like NS4 or IE5Mac which don't support either 
//attachEvent or addEventListener 
function MyAttachEvent(obj,evt,fnc){ 
    if (!obj.myEvents) obj.myEvents={}; 
    if (!obj.myEvents[evt]) obj.myEvents[evt]=[]; 
    var evts = obj.myEvents[evt]; 
    evts[evts.length]=fnc; 
} 
function MyFireEvent(obj,evt){ 
    if (!obj || !obj.myEvents || !obj.myEvents[evt]) return; 
    var evts = obj.myEvents[evt]; 
    for (var i=0,len=evts.length;i<len;i++) evts[i](); 
} 
+0

@Tood Moses:Tnxs的答覆。你是否暗示事件類型'onbeforeunload'不正確?如果是這樣,你說什麼是正確的事件類型?我正在檢查你的鏈接。 – goliatone 2010-02-15 16:05:51

+0

onbeforeunload不正確。它在函數中之前是'on'。看來IE在attachEvent函數中的'on'有問題。 – 2010-02-15 16:11:03

+0

我有點困惑,你能否給我提供任何參考,解釋爲什麼ie'與''混淆'?在我看來,您提供的代碼旨在成爲某種跨瀏覽器的傳統瀏覽器輔助方法...... – goliatone 2010-02-19 16:21:44