0 Replies Latest reply on May 29, 2007 5:46 PM by JaeWebb

    Using AS2 to Call JavaScript w/ External Interface: What Am I Doing Wrong?

    JaeWebb
      I am trying to trigger a JavaScript fuction from inside a Flash 8/AS2 movie created by my coworker. The JS function is called disappear(), which is meant to close the DIV that contains the flash movie itself.

      I know the JS works in all my target browsers - it's been tested already. Now I need working ActionScript. The ActionScript I've been studying is 3.0 so the fact that this task must be accomplished in AS2 makes it that much harder for me; I know very little about the way Flash Virtual Machine 1 works so I don't know where to begin with the code.

      I would like to implement this one of two ways:

      Preferred Method: I'd like to create an AS file that I can send to my coworker for inclusion in his FLA along with any code necessary for linking the button instance to the class in the AS file (don't think code is necessary; linkage done during right-click on library panel, right?)

      Alternative Method: I'd like to send my coworker text that he can simply drop into the first frame in the movie to make it go.

      I started out trying to call the javascript with on(press) { getURL("avascript:myFunction"); }, which worked in Firefox but not in IE6 or 7. Then I read over the tutorial here: http://www.adobe.com/devnet/flash/articles/external_interface_print.html and switched to using ExternalInterface.call() instead. That's where I am now. My brain is hurting trying to dig up answers for AS2 without reference materials... You are my best hope.

      Please help me make sense of this ActionScript 2.0 code with one of the prefered methods above. I am away from my copy of Flash 8 - I'm using FlashDevelop here at work - so I'd appeciate if you could test any suggestions you make before you make them - unless you are a Flash genius who I should not be questioning :) I've provided the HTML/JS code; all you have to do is make a 500x300 SWF and put a button instance in it.

      ----------------------------------------The attempt at ActionScript 2.0 (don't laugh)----------------------------------------

      import.flash.external.ExternalInterface;

      class JsCallerButton {

      private var _caller_btn:Button;

      //Constructor
      public function jsCallerButton():Void {
      this._caller_btn:Button = new Button();
      this._caller_btn.addEventListener("click", clickHandler);
      }

      //Event handler for event set in class constructor
      function clickHandler(eventObj:Object):Void {
      ExternalInterface.call("disappear");
      }
      }


      ----------------------------------------The HTML/JavaScript----------------------------------------
      <html>
      <head>

      <script>
      var timeout;
      function appear(){
      var the_style = getStyle("floatingflash");
      if (the_style) {
      var current_top = parseInt(the_style.top);
      var new_top = current_top + 5;
      if (document.layers) {
      the_style.top = new_top;
      } else {
      the_style.top = new_top + "px";
      }
      if (new_top < 150) {
      the_timeout = setTimeout('appear();',10);
      }
      }
      } // appear
      function disappear() {
      var the_style = getStyle("floatingflash");
      the_style.display = 'none';
      } // disappear
      function getStyle(ref) {
      if(document.getElementById && document.getElementById(ref)) {
      return document.getElementById(ref).style;
      } else if (document.all && document.all(ref)) {
      return document.all(ref).style;
      } else if (document.layers && document.layers[ref]) {
      return document.layers[ref];
      } else {
      return false;
      }
      } // getStyle
      </script>
      <style>
      #floatingflash {
      position:absolute;
      border:1px solid red;
      background:white;
      }
      </style>
      </head>
      <body onload="appear();">
      <div id="floatingflash" style="top:-400px;left:-150px;margin-left:50%;">
      <!--This 'X' link is for manual calls to the function that should be triggered from inside Flash-->
      <a href="javascript:disappear();">X</a>
      <p>  </p>
      <p>
      <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=" http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="500" height="300" title="Hover Ad">

      <param name="movie" value=" http://------------------------------/500x300.swf" />
      <param name="quality" value="high" />
      <embed src="500x300.swf" quality="high" pluginspage=" http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="500" height="300"></embed>
      </object>
      </p>
      <p><a href="launch.html">Back</a></p>
      </div>
      </body>
      </html>