1

I'd like to run some javascript code directly "from the flash (swf) banner".

Is it possible? And how can I manage that?

5
  • So, you want a communication layer between ActionScript and JavaScript so that you can call a certain JavaScript function from the ActionScript part of a flash banner ?
    – helly0d
    Jan 21, 2015 at 19:20
  • I have already found some information about calling js functions from the ActionScript, but I need exactly to run a script. Not a function. Jan 21, 2015 at 19:22
  • 1
    Are you trying to inject a script into a page or call a function that is already present within the loaded JavaScript of the page?
    – zero298
    Jan 21, 2015 at 19:23
  • I'm exactly trying to inject a script into a page. I do not want to call function, which is already loaded. Jan 21, 2015 at 19:26
  • I'm not sure that you can add an entirely new script tag. Why are you trying to add a new tag? What functionality is available through JS that is not already present through ActionScript? Also, are you planning on running this banner from your own domain? You'll probably run into some domain access problems if you aren't. Consider: Run JS from Flash and Calling JS from SWF
    – zero298
    Jan 21, 2015 at 19:38

1 Answer 1

1

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/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.