A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Pull variable FROM AS3 to javascript function?

  1. #1
    Member
    Join Date
    Feb 2008
    Posts
    31

    Pull variable FROM AS3 to javascript function?

    Anyone know how to grab the values from a running AS3 file and put them into a javascript function on the html page?

    I know how to initiate js functions with the ExternalInterface but not sure how to push a variable out.

    What i am using this for is an MP3 player that i developed. I want to pass the variables for the "current track" and "if the player is playing". I just dont know how to begin making the swf pass out its variables

    thanks for any help

    will

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's not clear whether you're calling AS from JS or vice versa. If AS is calling JS, then you can simply pass parameters in the ExternalInterface calls.

    Code:
    ExternalInterface.call("someJSFunction", currentTrack, isPlaying);
    If you are calling AS from JS, you could create and expose getter functions for your properties
    Code:
    private function getCurrentTrack():String{
      return currentTrack;
    }
    private function getIsPlaying():Boolean{
      return isPlaying;
    }
    //...
    ExternalInterface.addCallback("getCurrentTrack", getCurrentTrack);
    ExternalInterface.addCallback("getIsPlaying", getIsPlaying);
    Javascript can then call those methods to get your values.

  3. #3
    Member
    Join Date
    Feb 2008
    Posts
    31
    Thanks for the reply.

    sorry to be confusing. I am grabbing the variables, that are constantly changing, out of the swf(mp3 player) and using them in a javascript function.


    Just so that i understand:

    AS3:
    Code:
    private function getCurrentTrack():String{
      return currentTrack;
    }
    private function getIsPlaying():Boolean{
      return isPlaying;
    }
    Javascript or AS3?:
    Code:
    ExternalInterface.addCallback("getCurrentTrack", getCurrentTrack);
    ExternalInterface.addCallback("getIsPlaying", getIsPlaying);
    Currently i am using this:
    AS3:
    Code:
    public function initFunction() {
       updateStatusToPage();
    }
    private function getCurrentTrack():String{
      return currentTrack;
    }
    private function getIsPlaying():Boolean{
      return isPlaying;
    }
    private function updateStatusToPage() {
    			ExternalInterface.addCallback("getCurrentTrack", getCurrentTrack);
    			ExternalInterface.addCallback("getIsPlaying", getIsPlaying);
    			var result:Boolean = ExternalInterface.call("jsFunction_Update");
    			trace (ExternalInterface.available); // Trace = TRUE
    }
    javascript:
    Code:
    <script language="javascript">
    	function jsFunction_Update() {
    		javascript:updateStatus(currentTrack, isPlaying);
    		return false;
    	}
    </script>
    then the idea is that dynamically form the outside script i can pass:
    Code:
    javascript:updateStatus(4, true);
    when i go to another page so that it will continue where i left off on my playlist

    see any reason this isnt working. I'm just not familiar with talking outside of my swf.

    thanks again

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    All code in my previous reply was Actionscript.

    A couple of things:
    The addCallBack calls are one-time. You do not need to call them each time you update. But that's okay, since you don't need them at all, apparently.

    Second, your javascript is invalid.

    Try this AS:
    Code:
    public function initFunction() {
       updateStatusToPage();
    }
    private function updateStatusToPage() {
    			var result:Boolean = ExternalInterface.call("jsFunction_Update", currentTrack, isPlaying);
    			trace (ExternalInterface.available); // Trace = TRUE
    }
    And this javascript:
    Code:
    <script language="javascript">
    	function jsFunction_Update(currentTrack, isPlaying) {
    		updateStatus(currentTrack, isPlaying);
    		return false;
    	}
    </script>

  5. #5
    Member
    Join Date
    Feb 2008
    Posts
    31
    i actually just went back and after looking at it for a few minutes and getting a headache from its ugliness, I weeded and cleaned it up and came up with almost the same thing. almost

    Your suggestions really helped out. I am going to test this out later on and see if it works.

    Appreciate it a lot. thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center