Posts Tagged ‘ExternalInterface API’

ExternalInterface and Flex

November 3rd, 2011 by aabhaanjan
ExternalInterface and Flex :
The ExternalInterface API enables a communication between SWF and JavaScript.
Here is an simple example :

The ExternalInterface API enables a communication between SWF and JavaScript.

Here is an simple example :

Flex Side :

[xml]<?xml version=”1.0″ encoding=”utf-8″?>

<s:Application

xmlns:fx=”http://ns.adobe.com/mxml/2009″

xmlns:mx=”library://ns.adobe.com/flex/mx”

xmlns:s=”library://ns.adobe.com/flex/spark”>

<s:layout>

<s:HorizontalLayout/>

</s:layout>

<fx:Script>

import flash.external.ExternalInterface;

public function callASFunction():void {

var msg:String;

if (ExternalInterface.available) {

msg = ExternalInterface.call(‘javaScriptFunction’,’ Communication between JS and App done’);

} else {

msg = “Not available”;

}

}

</fx:Script>

<s:Button label=”Show Output” click=”callASFunction()”/>

</s:Application>[/xml]

Html side :

[xml]

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

<!– saved from url=(0014)about:internet –>

<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>

<head>

<title>ExternalInterface Example</title>

<script type=”text/javascript” src=”swfobject.js”></script>

<script type=”text/javascript”>

var swfVersionStr = “0”;

var xiSwfUrlStr = “”;

var flashvars = {};

var params = {};

params.quality = “high”;

params.bgcolor = “#ffffff”;

params.allowscriptaccess = “sameDomain”;

var attributes = {};

attributes.id = “ExternalInterfaceExample”;

attributes.name = “ExternalInterfaceExample”;

attributes.align = “middle”;

swfobject.embedSWF(

“ExternalInterfaceExample.swf”, “flashContent”,

“100%”, “100%”,

swfVersionStr, xiSwfUrlStr,

flashvars, params, attributes);

</script>

</head>

<SCRIPT LANGUAGE=”JavaScript”>

function javaScriptFunction(msg) {

alert(msg);

return “successful”;

}

</SCRIPT>

<body>

<div id=”flashContent”/>

</body>

</html>

[/xml]

Tags: , , , , , , , , , , , , , ,