views:

425

answers:

1

I have a Flash movie embeded with swfobject in a html container. Through ExternalInterface I have registered a javascript function to fire callback to my flash app.

ExternalInterface.addCallback("notifyClose", notifyOnClose );

The javascript function is added as an event listerner to fire onbeforeunload.

<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>

I have tested in FF and IE. Firefox works as expected but not in IE. In IE I get notified in Flash with the onload message, but not onbeforeunload.

Is it some king of sandbox restriction? Just bad code?

A: 

The problem is the "on" in your code below in the 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);
        }

Try something like the code below for your Event Listener code and see the following link for more information: 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]();
}
Todd Moses
@Tood Moses: Tnxs for the reply. Are you suggesting that the event type 'onbeforeunload' is incorrect? If so, what do you say is the right event type? I'm checking your link now.
goliatone
The onbeforeunload is not incorrect. The word 'on' before it is in the function. It seems the IE has a problem with the 'on' in the attachEvent function.
Todd Moses
I'm a bit confused, could you provide me any reference explaining why ie get's 'confused' with the 'on'? It seems to me that the code you provided is intended to be some sort of cross-browser-legacy-browser helper method...
goliatone