JavaScript – Flash Communication

August 29, 2009 at 4:28 am | Posted in Flash, Java Script | 1 Comment
Tags:

I used to work with flash all the times, in fact we used it in almost every project through college. You can do amazing things with flash very easily and honestly I love action script[:D].
Whenever you work with flash you will find yourself need to make flash communicate with other applications.
In action script you can communicate with a host application (in my case the Browser)
through External Interface.

To do that you to write a little JavaScript Code:

First you need to get the name of the flash movie and as usual this name differs along with the browser you are using so here is a method that I found here to get you the name of the flash movie whatever the browser is:

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

// Now Call the function on your flash movie.

      function SendDataToFlashMovie(data) {
       var flashMovie = getFlashMovieObject("myFlashMovie");
       flashMovie.JStoAS(data);}


then you have to write alittel Action Script code:
Now you have to register a callback method to respond to your method call from javascript.



import flash.external.ExternalInterface;
ExternalInterface.addCallback("JStoAS", getDataFromJavaScript);
function getDataFromJavaScript(data:String):void {
// Dod whatever you want with data here
}

and that's how it's done [;)]

Beckham

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.