<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; call()</title>
	<atom:link href="http://blog.flexexamples.com/tag/call/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting JavaScript variables into your Flex applications using the ExternalInterface API</title>
		<link>http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/</link>
		<comments>http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 04:46:42 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[call()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/</guid>
		<description><![CDATA[<p>The following example shows an easy way to get JavaScript variables into your Flex applications without having to write a single line of JavaScript or edit your HTML template using the Flash Player ExternalInterface API.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_4/bin/srcview/source/main.mxml.html">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows an easy way to get JavaScript variables into your Flex applications without having to write a single line of JavaScript or edit your HTML template using the Flash Player ExternalInterface API.</p>
<p>Full code after the jump.</p>
<p><span id="more-592"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_4/bin/srcview/source/main.mxml.html">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.Alert;

            private function init():void {
                var userAgent:String = ExternalInterface.call("navigator.userAgent.toString");
                Alert.show(userAgent, "navigator.userAgent:");
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button label="Display user agent"
                click="init();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_4/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_4/bin/main.html" width="100%" height="200"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Getting JavaScript variables into your Flex applications using the ExternalInterface API on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/',contentID: 'post-592',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'call()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/04/11/getting-javascript-variables-into-your-flex-applications-using-the-externalinterface-api/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Returning values from JavaScript in your Flex applications using the ExternalInterface API</title>
		<link>http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/</link>
		<comments>http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 01:21:12 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[call()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/</guid>
		<description><![CDATA[<p>In a previous example, <a href="http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/">&#8220;Calling JavaScript functions from your Flex applications using the ExternalInterface API&#8221;</a>, we saw how we could call JavaScript functions from our Flex application.</p> <p>The following example shows us how we can return a value from a JavaScript function call and use the data in our Flex application.</p> <p>Full code after [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous example, <a href="http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/">&#8220;Calling JavaScript functions from your Flex applications using the ExternalInterface API&#8221;</a>, we saw how we could call JavaScript functions from our Flex application.</p>
<p>The following example shows us how we can return a value from a JavaScript function call and use the data in our Flex application.</p>
<p>Full code after the jump.</p>
<p><span id="more-555"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_3/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.utils.ObjectUtil;

            private function callJavaScript():void {
                var keys:Array = "appCodeName,appName,appVersion,cookieEnabled,language,platform,systemLanguage,userAgent,userLanguage".split(",");
                var arr:Array = ExternalInterface.call("getNavigatorObject", keys);
                dataGrid.dataProvider = arr;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button label="Get browser navigator object"
                click="callJavaScript();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid"
            variableRowHeight="true"
            width="100%"
            height="100%" &gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="key"
                    width="120" /&gt;
            &lt;mx:DataGridColumn dataField="value"
                    wordWrap="true" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p class="download">/src/externalInterface.js</p>
<pre class="code">
// JavaScript
/** http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/ */
function getNavigatorObject(keys) {
    var arr = [];
    var i;
    var len = keys.length;
    var key;
    var value;
    for (i=0; i&lt;len; i++) {
        key = keys[i];
        value = navigator[key];
        arr.push({key:key, value:value});
    }

    return arr;
}
</pre>
<p class="download">/html-template/index.template.html</p>
<pre class="code">
&lt;head&gt;
...
<strong style="color:red;">&lt;script language=&quot;JavaScript&quot; src=&quot;externalInterface.js&quot;&gt;&lt;/script&gt;</strong>
&lt;/head&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_3/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test_3/bin/main.html" width="100%" height="350"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Returning values from JavaScript in your Flex applications using the ExternalInterface API on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/',contentID: 'post-555',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'call()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/03/11/returning-values-from-javascript-in-your-flex-applications-using-the-externalinterface-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking to see if the ExternalInterface API is available in Flex</title>
		<link>http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/</link>
		<comments>http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 16:07:09 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[available]]></category>
		<category><![CDATA[call()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/</guid>
		<description><![CDATA[<p>The following example shows how you can check to see whether your Flex application is in a container that supports the ExternalInterface API.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_available_test/main.mxml">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();"&#62; &#60;mx:Script&#62; &#60;![CDATA[ import mx.controls.Alert; private function init():void { if (ExternalInterface.available) [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can check to see whether your Flex application is in a container that supports the ExternalInterface API.</p>
<p>Full code after the jump.</p>
<p><span id="more-553"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_available_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.Alert;

            private function init():void {
                if (ExternalInterface.available) {
                    ExternalInterface.call("alert",
                            "ExternalInterface is available");
                } else {
                    Alert.show("ExternalInterface is not available");
                }
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button id="button"
                label="ExternalInterface.available"
                click="init();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_available_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_available_test/bin/main.html" width="100%" height="200"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Checking to see if the ExternalInterface API is available in Flex on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/',contentID: 'post-553',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'available,call()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/03/10/checking-to-see-if-the-externalinterface-api-is-available-in-flex/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Calling JavaScript functions from your Flex applications using the ExternalInterface API</title>
		<link>http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/</link>
		<comments>http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 17:40:54 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[call()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/</guid>
		<description><![CDATA[<p>The following example shows how you can call a JavaScript function from your Flex applications using the ExternalInterface class and the static ExternalInterface.call() method.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test/main.mxml">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"&#62; &#60;mx:Script&#62; &#60;![CDATA[ private function callJavaScript():void { ExternalInterface.call("sayHelloWorld"); } ]]&#62; &#60;/mx:Script&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can call a JavaScript function from your Flex applications using the ExternalInterface class and the static <code>ExternalInterface.call()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-552"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private function callJavaScript():void {
                ExternalInterface.call("sayHelloWorld");
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Button label="Say 'Hello World'"
            click="callJavaScript();" /&gt;

&lt;/mx:Application&gt;
</pre>
<p>Then, in your HTML document, add the following snippet between the &lt;head&gt; and &lt;/head&gt; tags:</p>
<pre class="code">
&lt;script language="JavaScript" type="text/javascript"&gt;
    function sayHelloWorld() {
        alert("Hello World, from JavaScript");
    }
&lt;/script&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/ExternalInterface_call_test/bin/main.html" width="100%" height="100"></iframe></p>
<p class="construction">If you are using Flex Builder, you should edit the &#8220;index.template.html&#8221; document in the /html-template/ folder. This file is used to create the main HTML file in the /bin-debug/ and /bin-release/ folders. If you edit the HTML files in the /bin-debug/ and /bin-release/ folders directly, the changes may get overridden if you clean your project. It is also important to note that your changes to index.template.html may get overwritten if you change your compiler Flex SDK version or change settings in your project&#8217;s HTML wrapper (such as changing &#8220;Use Express Install&#8221; or &#8220;Enable integration with browser navigation&#8221;.</p>
<p class="new">Or, instead of putting your JavaScript code directly into the HTML page, you may want to put it in an external .JS file and include that file in your HTML file.</p>
<p class="information">/src/scripts.js</p>
<pre class="code">
function sayHelloWorld() {
    alert("Hello World, from JavaScript");
}
</pre>
<p class="information">/html-template/index.template.html</p>
<pre class="code">
&lt;script src="scripts.js" language="JavaScript"&gt;&lt;/script&gt;
</pre>
<p>So far, so good, but what if you wanted to pass parameters to the JavaScript function? Well, consider the following JavaScript method:</p>
<pre class="code">
// JavaScript
function sayString(str) {
    alert(str);
}
</pre>
<p>The previous JavaScript method takes a single parameter, <code>str</code>, which is a string value which will be displayed using the JavaScript <code>alert()</code> method. If you want to pass a parameter to the custom JavaScript <code>sayString()</code> method from ActionScript, simply pass additional parameters to the static <code>ExternalInterface.call()</code> method, as shown in the following snippet:</p>
<pre class="code">
// ActionScript 3.0
private function callJavaScript():void {
    ExternalInterface.call("sayString", "Hello World, from ActionScript");
}
</pre>
<p>OK, your next question may be &#8220;That is pretty useful, but that seems like overkill just to use a builtin JavaScript method from Flex. Do I really need to create a custom JavaScript method wrapper for something simple like the <code>alert()</code> method&#8221;? And actually, you don&#8217;t. The previous JavaScript method (<code>sayString()</code>) could be deleted, and the ActionScript snippet could be simplified to the following:</p>
<pre class="code">
// ActionScript 3.0
private function callJavaScript():void {
    ExternalInterface.call("alert", "Hello World, from ActionScript");
}
</pre>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Calling JavaScript functions from your Flex applications using the ExternalInterface API on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/',contentID: 'post-552',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'call()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

