2

I am developing a flash application for a website I have no direct access to. The flash application is supposed to call a javascript function on the website, defined by the website publisher. I got advised to check for the existance of the javascript object before calling its' function from actionscript:

var ok:Boolean = ExternalInterface.call(function() { 
    return typeof customObject !== \'undefined\' 
}

If I then continue with:

if (ExternalInterface.available && ok) {
    ExternalInterface.call('customObject.doSomething', someStr);
}

Will this if's condition always be false, because the call that gets saved into ok has possibly not finished before I use the check, or is the ExternalInterface.call instantenious? In other words, would I somehow have to wait for the result of the first call before determining if I can savely assume the existance of customObject.

Edit: Updated code as suggested in comments:

if (ExternalInterface.available) {
  var ok:Boolean = ExternalInterface.call('function() { return typeof customObject !== \'undefined\' }');
  if (ok) {
    ExternalInterface.call('customObject.doSomething', someStr);
  } else {
    .. do some fallback
  }
} else {
  .. do some fallback
}
5
  • Well, the check determines, if I can rely on it working for real program logic. Commented Feb 10, 2012 at 21:03
  • In my real application, the if clause has an else part, performing a less desireable, but still valid, action - I want that to only happen when I am positive that calling that specific javascript object via ExternalInterface does not work for one reason or the other. Commented Feb 10, 2012 at 22:51
  • Do you mean I should capsule the var ok:Boolean... into a if (ExternalInterface.avaialble) {... clause? Commented Feb 11, 2012 at 16:14
  • Yes, true. So in that case I could just drop the ExternalInterface.available part from the second check and instead attach it before the var ok:.... Commented Feb 12, 2012 at 13:43
  • The whole javascript function call should have been in quotes, right. Commented Feb 13, 2012 at 10:33

2 Answers 2

4

For robustness, you need to check whether the function is there too:

var ok:Boolean = ExternalInterface.call(function() {
    if (typeof(customObject) === 'object' && typeof(customObject.doSomething) === 'function') {
        return true;
    }
}

ExternalInterface.call is synchronous, so you should find that it will wait until this bit is finished until moving on to the next.

Sign up to request clarification or add additional context in comments.

2 Comments

Good point to also check the function, although I trust the website publisher party on making sure of that. The real issue for me was the question of sync, so that's for you answer. I understand that his means I can indeed use the check right after the first call, as the code will wait for the result.
@wvxvw Agreed - no parens are needed and you can leave out the if() and just do return followed by the whole statement, but I'd argue it reads slightly better this way because the flow is totally unambiguous. Strict equality is also not necessary, but it's a good habit to get into to use it by default: most of the time you will want the types of variables to be the same anyway and it can sometimes highlight hard to spot bugs.
1

The ExternalInterface.call method will be available as soon as the swf file has loaded (whereas your JavaScript file or variable might not be there at that moment). So what you wanna do is use the ExternalInterface.addCallback method to bind your function in ActionScript to another one in your JavaScript code.

Here's the documentation of that feature.

1 Comment

Valid point, but actually no concern in this specific case; I know that the javascript is there before the swf, IF it is there :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.