Gene
Gene

Reputation: 1527

Calling As function from js problem

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?

Upvotes: 0

Views: 538

Answers (3)

Fenton
Fenton

Reputation: 250812

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();
    }
    

Upvotes: 0

jcoder
jcoder

Reputation: 30035

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.

Upvotes: 1

MDCore
MDCore

Reputation: 18105

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!

Upvotes: 0

Related Questions