0

I'm using this code to play a swf with onclick command for an image. On the first click, I receive "flashMovie.Play is not a function" error. On the second click it works, and every time afterwards. Is this a conflict with the order of execution with other elements in the page? What is happening on the first click that makes the second click work properly? (this works in IE but not firefox)

Would putting some sort of timing delay on this possibly help? Any suggestions as to how I could try that?

<script language="JavaScript">
    function getFlashMovieObject(movieName)
    {
        if (window.document[movieName]) 
        {
            return window.document[movieName];
        }

        if (navigator.appName.indexOf("Microsoft Internet")==-1)
        {
            if (document.embeds && document.embeds[movieName])
                return document.embeds[movieName]; 
        }
        else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
        {
            return document.getElementById(movieName);
        }
    }

    function PlayFlashMovie(name)
    {
        var flashMovie=getFlashMovieObject(name);
        flashMovie.Play();     
    }
</script>
William Smith
  • 822
  • 1
  • 10
  • 23

1 Answers1

1

If your flash movie isn't loaded before your button is clicked, then you might encounter this problem. I don't think any kind of specific timing delay is a good idea; it would be better to figure out what exactly you are waiting for and then implement the correct handler.

For example, if you were waiting for the whole page to load before doing something, instead of having a 1 second delay, implement a document.onload handler, like so:

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

As a side note, you might consider using SWFObject, as it makes your life a lot easier with regards to Flash and JavaScript.

Student
  • 182
  • 1
  • 7
  • Thanks for the input. I am using swfobject, dynamic publishing. I think you are exactly correct. Somehow I need to get all of the flash movies to load (I have about six on the page). So on the first click, the movie loads, then on the second click it will play. I've tried many different things. Interestingly, it works fine in IE. I have looked into the click to activate issue, but haven't found a solution...and that was supposed to be more of an IE thing anyway. – William Smith Jun 16 '12 at 13:45
  • currently, on the first click, the movie loads, then on the second click it will play. I need it to play on the first click. – William Smith Jun 16 '12 at 22:56