IE onbeforeunload没有触发ExtenralInterface回调

时间:2010-02-12 16:53:40

标签: javascript actionscript-3 javascript-events externalinterface

我在html容器中嵌入了swfobject的Flash电影。 通过ExternalInterface我已经注册了一个javascript函数来触发我的flash应用程序的回调。

ExternalInterface.addCallback("notifyClose", notifyOnClose );

将javascript函数添加为事件listerner以触发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。

它是沙盒限制之王吗?只是糟糕的代码?

1 个答案:

答案 0 :(得分:-1)

问题是下面代码中的“on”在attachEvent()中。

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, !@phrogz.net
//*** 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]();
}