简体   繁体   中英

Is it possible to run html <script> in the flash banner?

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

Is it possible? And how can I manage that?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM