javascript to swf Beyond 101

The communication between Flash and HTML is manipulated by Javascript/ActionScript.

The 101 #1:

Javascript->Flash: window.document.myMovie.SetVariable(variable_name, value);

Flash -> Javascript: getURL(‘javascript:function_name()’);

The 101 #2:

Flash DOM:

/* This utility function resolves the string movieName to a Flash object reference based on browser type. */
function getMovieName(movieName) {
if (navigator.appName.indexOf(“Microsoft”) != -1) {
return window[movieName]
}
else {
return document[movieName]
}
}

The 101 #3:

When using “SetVariable” to pass values to swf, swf object is “labeled” as “myMovie” in HTML by the following two tags. <object id=”myMovie”…><<embed name=”myMovie”…>.

In the following statement:
window.document.myMovie.SetVariable(variable_name, value)

variable_name is type of String and value could be anything but when actually passed into swf, this value is converted to type of String in Flash. So any Object would become “Object”. Practically I have been using JASON (a special type of Array), Array and String. Use AS “watch()” function to invoke the subsequent functions to handle the arrived data. The following example demonstrate value passed from JASON object in HTML to Flash and used by Flash as Model/data.

// swf files
var swfs:String = “”;
//url requested when a banner was clicked
var links:String = “”;
//target of links, either “_self” or “_blank”
var targets:String = “”;

var slidesTotal;
//
function main() {
slidesTotal = swfs.length;
//a number from 0 to slidesTotal-1
rdmStart = Math.round(Math.random()*(slidesTotal-1));
genSlide(rdmStart);
}

function onData(){
main();
}

watchCallback = function (id, oldval, newval):Array {
var newvalArr = newval.split(“,”);
onData();
return newvalArr;
};
this.watch(“swfs”, watchCallback);
this.watch(“links”, watchCallback);
this.watch(“targets”, watchCallback);

swfs/links/targets originally resided in a JASON object in Javascript and were pared as 3 separate arrays before passed into Flash.

Beyond 101:

The adobe/macromedia site demonstrates how to pass variables by a user event (a button click) on the HTML. In most of my projects, I want instead to use JASON data to initialize the whole application. So I need the data in Flash as soon as swf is available.

To invoke the function call that passes the data, use something like:
window.onload = function(){parseJSONToFlash();};

Unfortunately it only works for IE (v6 on PC), not Firefox (v2 on PC).

The reason was probably the swf was not totally “initialized” when the “onload” get’s called. The trick was to use the following. So instead, we call the javascript in Flash. That guarantees that swf object is already available when the data arrives.

var callJas:String = ‘javascript:parseJSONToFlash()’;

getURL(callJas);

Also note that using this trick eliminates the need to invoke the javascript function in window.onload.

References:

Scripting with Flash 5(old school but most effective)
Using JavaScript with Flash Player(sept, 2005)
External API documentation
The quite popular tutorial on debut of External API at adobe.com
Flash Developer Center: Javascript API

Advertisements

One Response to “javascript to swf Beyond 101”

  1. samy Says:

    how can i call a swf javascipt?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: