IE onbeforeunload 不触发ExternalInterface 回调

发布于 2024-08-22 02:46:12 字数 1416 浏览 7 评论 0原文

我有一个在 html 容器中嵌入了 swfobject 的 Flash 影片。 通过 ExternalInterface 我注册了一个 Javascript 函数来触发对我的 Flash 应用程序的回调。

ExternalInterface.addCallback("notifyClose", notifyOnClose );

Javascript 函数被添加为事件侦听器以触发 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>

我已经在 Firefox 和 IE 中进行了测试。 Firefox 按预期工作,但在 IE 中却不行。 在 IE 中,我在 Flash 中收到 onload 消息通知,但没有收到 onbeforeunload 消息。

这是某种沙箱限制吗?只是糟糕的代码?

I have a Flash movie embedded 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 listener 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 Firefox 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 kind of sandbox restriction? Just bad code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-08-29 02:46:12

问题是下面的 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]();
}

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]();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文