Tuesday, June 16, 2009

Using JavaScript and Flash Integration Kit



The JavaScript and Flash Integration Kit allows developers to get the best of the Flash and HTML worlds by enabling JavaScript to invoke ActionScript functions, and vice versa............
more information can be obtained from below link :


Download link for the Classes and Javascript files below :


I found this technique pretty useful. It works well and we can communicate between JavaScript and Flash quite easily and with more flexibility as it supports passing of Objects and arrays.

For communication we need to import JavaScriptProxy Class.

A sample below :

import com.macromedia.javascript.JavaScriptProxy;

var proxy:JavaScriptProxy = new JavaScriptProxy(_root.lcId, this);

insertBtn.onRelease = function()
{
proxy.call("InsertFunction", nametxt.text, infotxt.text);
}

function displayfromJS(values:Object)
{
infotxt.text = values.Name;
}

Link for the source fla for the code above : http://www.box.net/shared/6mr5v77r4v

"proxy.call("InsertFunction", nametxt.text, infotxt.text)" this line calls a function on the html page with name "insertFunction". It is also taking two parameters here along with function call.

Lets look at the Html file


"example.htm"


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Sample</title>
<script type="text/javascript" src="JavaScriptFlashGateway.js"></script>
<script type="text/javascript">
var lcId = new Date().getTime();
var flashProxy = new FlashProxy(lcId, "JavaScriptFlashGateway.swf");

function InsertFunction(item1,item2) {

alert("Text file will only be created in C:\ if you are using IE = "+item1);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("c:\\Test.txt", true);
fh.WriteLine("Name : "+item1);
fh.Close();
}

function transferItemToFlash() {
var toFlashVal = document.listForm.sendVal.value;
flashProxy.call("displayfromJS", {Name:toFlashVal,Val:"121008250"});
}
</script>
</head>

<body>

<table border="0" cellpadding="0" cellspacing="5" width="400">
<tr>
<td width="116" valign="top">
<script type="text/javascript">
var sample = new FlashTag("main.swf", 216, 120, "7,0,14,0");
sample.setFlashvars("lcId=" + lcId);
sample.write(document);
</script>
</td>
<td width="269" valign="top">
<form name="listForm">
<input type="text" name="sendVal"/>
<input type="Button" value="transfer" onclick="javascript:transferItemToFlash();" />
</form>
</td>
</tr>
</table>

</body>
</html>

As you can notice html file has two javascript functions, "InsertFunction" which is called from flash and "transferItemToFlash" which is used to send data into flash.

var lcId = new Date().getTime();
var flashProxy = new FlashProxy(lcId, "JavaScriptFlashGateway.swf");

Above lines are required create an instance of the FlashProxy JavaScript class and pass in the unique ID you just created, and the path to the JavaScriptFlashGateway.swf file. Further details given at below link:


The final source files along with the required classes can be downloaded from below link.


-Nitin-

No comments:

Using FormBuilder, Validators & FormGroup in Angular 2 to post data to Node server (node.js)

This blog explains usage of  FormBuilder , Validators , FormGroup classes in Angular 2 projects. In this post we will create HTML form a...