Home / Articles / Adobe Flash / Extending AJAX with the Flash/JavaScript Integration Kit

Extending AJAX with the Flash/JavaScript Integration Kit

Article Description

Want more control over the user experience? Because Flash makes graphical programming quick, easy, and attractive, and because JavaScript is very effective at manipulating HTML, they can be leveraged to focus on their strengths. Kris Hadlock shows you how to extend AJAX (Asynchronous JavaScript and XML) by cross-communicating between Flash and JavaScript. Discover how this technique enables technologies to focus on specialized tasks, and helps create the ultimate user experience.

Like this article? We recommend

Macromedia Flash MX 2004: Training from the Source

Macromedia Flash MX 2004: Training from the Source


JavaScript to Flash

To retrieve the data in Flash, you first need to create the method that JavaScript will be calling with the flashProxy. Because you already defined this method in JavaScript, you are ready to create it in Flash. In the sample, the addItem() method is used to create bars and add them to the chart and a BarCollection class. The parameters that addItem() takes are the lender name and rate, which are the parameters you are already passing from the flashProxy object in JavaScript. The following is the addItem() method in Flash:

public function addItem(lender:String, rate:Number):Void {...}

To work around the bug I described earlier, you can create a method in Flash that accepts arrays of lenders and their corresponding rates. This allows the addItem() method to be accessible by multiple consecutive method calls.

public function addAllItems(lenders:Array, rates:Array):Void
  for(var i=0; i<lenders.length; i++)
    this.addItem(lenders[i], rates[i]);

The receiving methods in Flash can receive anything that you or your client desires; for instance, the sample could have been a line or pie chart. The best part is that setting it up to receive calls from JavaScript is simple after you build the foundation.

5. Flash to JavaScript | Next Section Previous Section