Injecting and calling JS functions from within flash using external

JS injection

I recently encountered a unique requirement in one of my projects, triggering the loading of a page in the background (outside of flash, using Ajax) from within flash.
Often you cannot rely on JS functions to be available, you could load public libraries (Jquery or equivalent) or inject your own functions to the DOM, I chose to inject my own.

This is how you do it:

1) import the external class.

import flash.external.ExternalInterface;

2) declare a constant variable with all the JS functions:

private const script_js :XML =
function() {

var ajaxRequest;
// Opera 8.0+, Firefox, Safari, Chrome
ajaxRequest = new XMLHttpRequest();"GET", _url, true);

} catch (e){
// Internet Explorer Browsers
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");"GET", _url, true);

} catch (e) {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");"GET", _url, true);

} catch (e){
// Something went wrong
return false;


3) inject the JS to DOM:

try {
if( ExternalInterface.available ) script_js );
} catch( error:Error ) {
trace("ExternalInterface is not available");

4) call a function: "AJXFNC.ajaxFunction", "" );

Sweet isn’t it?

2 thoughts on “Injecting and calling JS functions from within flash using external”

Leave a Reply

Your email address will not be published. Required fields are marked *