如何从已使用 ExternalInterface.call()
注入(inject)页面的 JavaScript 调用 ActionScript 3 中的函数?
我已经尝试了很多方法,但无济于事。
这里是所有必要代码的简化版本:
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
}
评论
标有<= ERROR
的行在 Chrome 中抛出以下内容:
Uncaught Error: Error calling method on NPObject.
在 FF 中:
uncaught exception: Error in Actionscript. Use a try/catch block to find error.
我认为问题在于 ExternalInterface.addCallback()
在 ob.callASFunction()
之前未在 Flash 对象上附加监听器被称为。
或者,也许我遗漏了什么。
如有任何指点,我们将不胜感激。
最佳答案
有两种可能的修复方法。
不要强制调用 AS 方法的参数
public function myASFunction(vars:String = null){ ... }
或
始终将变量传递给方法
ob.callASFunction(response.status); /* <= FIX */
有点捂脸的时刻。 :{D
注意:我已经编辑了我的答案以反射(reflect) JS 修复,但不是 AS 修复。
关于javascript - 使用注入(inject) JS 的 ExternalInterface.call() 调用 AS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630297/