1

如何从已注入页面的 JavaScript 调用 ActionScript 3 中的函数ExternalInterface.call()

我已经尝试了很多方法,但无济于事。

这是所有必要代码的简化版本:

public function myClass() {

    ExternalInterface.addCallback("callASFunction", myASFunction);

    var loadJS:XML =
    <script><![CDATA[

        function(oid){

            ob = document.getElementById(oid);

            if (!window.FB) { /* Include FB JS SDK */ }

            window.fbAsyncInit = function() {

                FB.init({ ... });

                FB.getLoginStatus(function(response) {
                 /* ob.callASFunction(); <= ERROR */
                    ob.callASFunction(response.status); /* <= FIX */
                });
            }
        }

    ]]></script>

    // Inject the JS into the page
    ExternalInterface.call(loadJS, ExternalInterface.objectID);

}

public function myASFunction(vars:String){

    // Do great things

}

评论

标有 的行在<= ERRORChrome 中抛出以下内容:

Uncaught Error: Error calling method on NPObject.

在 FF 中:

uncaught exception: Error in Actionscript. Use a try/catch block to find error.

我认为问题在于在调用ExternalInterface.addCallback()之前没有在 Flash 对象上附加侦听ob.callASFunction()器。

或者,也许我错过了一些东西。

任何指针将不胜感激。

4

1 回答 1

2

对此有两种可能的解决方法。

不要为被调用的 AS 方法强制使用参数

public function myASFunction(vars:String = null){ ... }

或者

始终将变量传递给方法

ob.callASFunction(response.status); /* <= FIX */

有点打脸的时刻。:{D


注意:我已经编辑了我的答案以反映 JS 修复,而不是 AS 修复。

于 2012-11-29T17:13:04.223 回答