AboutFlex.net

flex,air,flash …

Archive for the ‘Javascript’ Category


This example shows you how easy is the comunication between Flex and Javascript:

The Flex Code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 

	creationComplete="{init()}"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="vertical" height="200" width="300" horizontalAlign="center" verticalAlign="middle">

	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;

			private function init():void{
				if(ExternalInterface.available) {
					ExternalInterface.addCallback('waitForJavascript',DisplayJavascriptText);
				}
			}

			private function sendToJavascript():void{

				if(ExternalInterface.available) {
					ExternalInterface.call('displayFlexText',textInput.text)
				}

			}
			private function DisplayJavascriptText(param:String):void{
				Alert.show(param)
			}
		]]>
	</mx:Script>
	<mx:TextInput text="Send this text to Javascript" id="textInput" width="220"/>
	<mx:Button click="{sendToJavascript()}" label="send" width="55"/>
</mx:Application>

The Javascript/HTML Code:

<script language="javascript" >
	function displayFlexText(param){
		alert(param)
	}
	function sendToFlex(){
		main.waitForJavascript(document.getElementById('textInput').value);
		//note that "main" is the id of the swf object
	}
</script>
<input id="textInput" type="text" value="Send this text to Flex" />
<input type="button" value="Send" onclick="sendToFlex()" />