Think Labs is an ongoing effort by Seven2 to provide research and educational opportunities in the web development and mobile field. To see what we’ve been cookin’ up, check out our blog postings.

Created by
Seven2 Login

Team Think Labs | Writing Javascript from Flash
single,single-post,postid-2833,single-format-standard,ajax_fade,page_not_loaded,,,wpb-js-composer js-comp-ver-4.2.3,vc_responsive

Writing Javascript from Flash

A lot of Flash developers know about ExternalInterface and how you can use it to talk between javascript on the webpage and your Flash application. Something that I stumbled upon was being able to embed javascript into your webpage without ever leaving your Actionscript class. It goes a little something like this:

var js :XML =
<script type="text/javascript">
	function( sText )
		var div = document.createElement("div");
		div.innerHTML = sText != undefined ? sText : "";

		var flashContent = document.getElementById("fromFlash");
		flashContent.appendChild( div );

		console.log( "Logging something: " + sText );

ExternalInterface.call(js, "hello world!");

That’s a little hello world script I wrote that will find a div with the id “fromFlash” and add a new div with the text passed in. The basics for how to do this is to create an XML object with <script> being the tag used. You must use <![CDATA[ stuff goes here ]]> to encapsulate your javascript so that it will be taken exactly as it’s written and not interpreted as code by the Flash compiler.

A great use of this would be to embed a javascript library into the webpage on startup. Say you need to use Google Analytics for your page, you could simply write the javascript code directly into your Flash application so that no matter where it runs, it will make the Analytics calls that you want. Think Facebook APIs, Twitter, etc. Any javascript library that you might need to communicate with, you can embed and call right from Flash.