Andrew Willis
Andrew Willis

Reputation: 2349

jQuery swfobject AS3 ExternalInterface not working

As you can tell by the title, I am having some trouble with AS3 ExternalInterface and jQuery / swfobject.

ActionScript :

if (ExternalInterface.available) {
    ExternalInterface.call('init');
    ExternalInterface.addCallback('testFunc', returnFunc)
}

function returnFunc():void {
    ExternalInterface.call('alertFunc');
}

jQuery:

function init() {
    alert('init');
    $('#swf_object').testFunc();
}

function alertFunc() {
    alert('finished');
}

Obviously that implies the object has the id 'swf_object'

I have also tried getting the object by the following:

document.getElementById('swf_object')
document.getElementById('swf_object')[0]
$('#swf_object')[0]

To no avail.

It's giving the first alert ('init') but then not doing the last one. I'm completely baffled and hope someone can point out my mistakes! (there's bound to be a massively obvious one somewhere)

Upvotes: 1

Views: 2989

Answers (1)

Cameron
Cameron

Reputation: 98736

The problem is that you're calling out to the JavaScript init() which calls the Flash testFunc() before you're made testFunc available (which happens only after the call out to init() completes).

To fix this, simply swap the two lines to this:

ExternalInterface.addCallback('testFunc', returnFunc);    // Needs to be available before it's used
ExternalInterface.call('init');

As for getting the Flash object in JavaScript, you can do it directly with document.getElementById('swf_object'), but it's possible using jQuery too:

var swf = $('#swf_object').get(0);    // Get the actual object without the jQuery wrapper

Upvotes: 2

Related Questions