Reputation: 403
I'd like to run some javascript code directly "from the flash (swf) banner".
Is it possible? And how can I manage that?
Upvotes: 1
Views: 446
Reputation: 818
In order to be able to inject JS scripts in the DOM using ActionScript and then communicate via the injected functions you could do something like this:
1) import the external class.
import flash.external.ExternalInterface;
2) declare a constant variable with all the JS functions:
private const script_js :XML =
<script>
<![CDATA[
function() {
AJXFNC = {
ajaxFunction:function(_url){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari, Chrome
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("GET", _url, true);
ajaxRequest.send(null);
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
ajaxRequest.open("GET", _url, true);
ajaxRequest.send();
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
ajaxRequest.open("GET", _url, true);
ajaxRequest.send();
} catch (e){
// Something went wrong
return false;
}
}
}
}
}
}
]]>
</script>;
3) inject the JS to DOM:
try {
if( ExternalInterface.available )ExternalInterface.call( script_js );
} catch( error:Error ) {
trace("ExternalInterface is not available");
}
4) call a function:
ExternalInterface.call( "AJXFNC.ajaxFunction", "http://www.google.com" );
I have pasted the technique into this answer because i usually do not trust the up-time of a blog, but all rights go to Adi Feiwel, for writing this: http://todepoint.com/blog/2011/08/01/injecting-and-calling-js-functions-from-within-flash-using-external/
Upvotes: 1