bgx:components logo
© 2004 -2005
Bernhard Gaul



Retrieving JavaScript Expressions from Flash
Flash

The Task

I came up with the following because I was asked to permit a Flash application to run only if it is embedded in a HTML (ASP,PHP,...) file at a specific location. Therefore I not only had to request the location of the hosting document and pass it to a variable in Flash but also do it in a way that does not show that variable name in the HTML. Ideally it would use a random variable name that Flash generates anew every time the request takes place.

As a side effect I created a simple functionality that allows me to retrieve just about any JavaScript expression easily from within Flash.

Below is a sample that shows both the retrieval of evaluated JavaScript expressions as well as the use of variables with random names. You can find explanation and code further down the page.

The Working Sample

The Solution Part 1 - Retrieving Arbitrary JavaScript Expressions

There are plenty of examples on the Internet that show how to communicate from JavaScript to Flash. In short the main requirement is:

  • you must have an id attribute in your object tag and a name attribute in your embed tag to identify the Flash file.
    [Note: if you enter a name in the properties inspector using Dreamweaver MX this will also add a name attribute to the object tag, which in turn gives you troubles using the script below in Firefox. So make sure that there is no name attribute in the object tag.]

You can then identify the Flash file with JavaScript and set variables within it. See the Related Links on the right for detailed information on how that works.

What I have done to request JavaScript values from within Flash is to write a very short function that takes as parameter a JavaScript expression that you would want to have evaluated plus a target path identifying the variable in Flash to which the result should be returned.

The full JavaScript code looks as follows:

var movieName = "getJSVars";//this is the name of the movie

//function to retrieve the movie object in the HTML page
function thisMovie(mName)
{
    if (navigator.appName.indexOf("Microsoft") !=-1)
    {
        return window[mName];
    }
    else
    {
        return document[mName];
    }
}

//function called by Flash to retrieve values
function getValue(value,target)
{
    thisMovie(movieName).SetVariable(target,value);
}

You can then request the evaluation of any JavaScript expression from within Flash using e.g.

getUrl("javascript:getValue(document.location,'/:myVar')");

Note that the JavaScript expression is not in quotes. You really can send the expression as such, not a string that represents the expression.

About the syntax of the return variable: "/:myVar" identifies a variable called myVar in the _root timeline. You can identify variables in MovieClips e.g. like "/instanceName/instanceName:myVar"

Part 2 - Using a Random Variable

The following is a code snippet that creates a random variable name, a variable at _root level with that name and then requests document.location to be returned to that variable from JavaScript:

//create random variable name
var randomVarName:String = ("v" + Math.random()).split(".").join("");
//create a variable with that name at root level
_root[randomVarName] = "";
//call JavaScript to retrieve the value
getUrl("javascript:getValue(document.location,'/:"+randomVarName + "')");

Below is the code for the sample shown above that lets you evaluate diverse JavaScript expressions using the described approach. You can also download the source for this example. Notice the interval I set before retrieving the value. This is necessary as it will take a few milliseconds before the value is set.

Resources

Download the source file
(ZIP file, 251 kB)


Related links

Macromedia documentation "Scripting with Flash"

Brajeshwar's comprehensive example of commands


 

Code for the Example on this Page

Additional JavaScript code on the HTML page:

document.cookie = "flashTest=Cookie for Flash";

function getDouble(numVal)
{
    return numVal * 2;
}

Flash code:

//add JavaScript strings to the dropdown
ddlCommands.addItem("document.location");
ddlCommands.addItem("document.lastModified");
ddlCommands.addItem("document.title");
ddlCommands.addItem("document.cookie");
ddlCommands.addItem("document.body.clientHeight");
ddlCommands.addItem("navigator.platform");
ddlCommands.addItem("navigator.userAgent");
ddlCommands.addItem("new Date()");
ddlCommands.addItem("getDouble(3)");
ddlCommands.addItem("getDouble(15)");


btnGetValue.onRelease = function()
{
	//create random variable name
	var randomVarName:String = ("v" + Math.random()).split(".").join("");
	//create a variable with that name at root level
	_root[randomVarName] = "";
	//call JavaScript to retrieve the value
	getUrl("javascript:getValue(" + ddlCommands.selectedItem.label + ",'/:"+randomVarName + "')");
	//set interval to give Flash some time to receive
	//the value before checking it
	getVarInterval = setInterval(applyValue,100,randomVarName);
}

function applyValue(randomVarName:String)
{
clearInterval(getVarInterval);
tbResult.text = "<b>var name:</b>&#09;" + randomVarName + newline;
tbResult.text += "<b>result:</b>&#09;&#09;" +_root[randomVarName];
}

If you have any comments or suggestions please let me know at info@bgxcomponents.com.