How do I call a function in ActionScript 3 from JavaScript that has been injected into the page using ExternalInterface.call()?
I’ve tried this a bunch of ways, to no avail.
Here is a simplified version of all the necessary code:
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
}
Comments
The line marked with <= ERROR throws the following in Chrome:
Uncaught Error: Error calling method on NPObject.
And in FF:
uncaught exception: Error in Actionscript. Use a try/catch block to find error.
I believe that the issue is that the ExternalInterface.addCallback() is not attaching a listener on the Flash object before the ob.callASFunction() is called.
Or, perhaps I’m missing something.
Any pointers would be much appreciated.
There are two possible fixes to this.
Don’t force a parameter for the called AS method
or
Always pass a variable to the method
Bit of a facepalm moment.
:{D
Note: I’ve edited my answer to reflect the JS fix, but not the AS one.