views:

314

answers:

3

I have a swf file that is not controlled by me. The swf expects a javascript call to set some variables after initialization.

The swf is embedded using the swfobject and I'm trying to call the as function right after the embed. This appears to be too soon because I get an error. Everything else should be fine since calling the as function manually via firebug does not produce the error.

So the question is how do I call the function when the embed is complete?

+1  A: 

Are you doing this while the page is still loading? Or from am onload handler? If it's inline javascript I would suggest doing it in the onload handler from javascript which you can do like this -

window.onload = function() { // your code here }

it will run your code once the page is fully loaded.

This doesn't guarentee that the flash is initialised though. You could do that by having the flash make a callback to javascript once it is ready, but you said that the swf is not in your control. All I can really think of us using the onload method to make sure the page is finished loading, and then insert a short delay before trying to use it. Look at the setTimeout javascript function for that. Not a great solution though.

John Burton
The flash is inside a complex single page interface ajax app. Built on a "framework" I've designed and implemented myself. So window onload is not an option here. The thought of using setTimeout had occurred to me. It's not a pretty solution but the problem is defining the length of the delay
Gene
Are there any functions on the flash to retrieve the data items you set? You could keep setting them with a short timeout until they "stick". Not at all pretty though
John Burton
the initial problem/error is the as function for setting does not exist. Sorry could have been more clear about that. Anyways as I could edit the flash if absolutely necessary I won't be using a timeout based solution as that would smell like a hack too much ;)
Gene
Yes, sounds like editing the flash is by far the best solution
John Burton
A: 

I found some code for checking whether the function exists yet. In summary:

if (typeof yourFunctionName == 'function') {
    yourFunctionName();
}

Does that work for you? If it does then you can just wrap in a while loop. A bit less nasty than a setTimeOut!

MDCore
This could work with a set timeout loop. Using that in a while loop would consume all available processing resources until the while loop ends. So that's not really usable. It would also open a possibility for infite loop if something went wrong
Gene
Yes, in a straight while loop it would definitely be an infinite loop because JavaScript is single-threaded and therefore the body of a while loop must contain sufficient code to alter its condition's result or have an explicit break.
Lee Kowalkowski
A: 

When integrating Flash and HTML / JavaScript, there are several common approaches, which have been designed to eliminate this problem.

1) Pass in the variables as flashvars. They will be available to the flash movie immediately.

2) When the flash has loaded, it should call out to your page, normally there is a contract that defines the methods you can / must implement for the flash movie to call. For example, it would state that it will call "MovieLoaded()" when the flash file has loaded, and you could then put any scripts dependent on the movie being loaded within this method...

function MovieLoaded() {
    doSomething();
}
Sohnee