Script to script
I have developed a widget that can easily be applied to a website.
Warning: this post is for client-side engineering geeks. This is not for designers. Run away now.
Using Flash, I have developed a widget that can easily be applied to a website. The widget loads data from an XML file and creates an interactive interface for viewing the data. For my latest client, I decided to modify the widget to read data from the HTML page containing the widget, rather than from an secondary file. I want the HTML page to display the data for visitors who do not have the Flash browser plug-in and serve as the source of data when the widget is displayed for visitors who do have the Flash plug-in.
To make this happen, I tried to send the HTML-structured data to Flash using SWFObject and flashvars. jQuery code identified the HTML element containing the data, assigned it to a variable, and passed that variable to Flash when the widget was loaded into the page. Within Flash, the function normally used to process XML was modified to parse the data in its HTML format. This was a failure. I was unable to get Flash to recognize the contents of the variable as and XML object.
For my second attempt, I tried something more sophisticated. I used jQuery to parse the HTML structured data and convert it into an array of objects. Each object represented a data point. I thought this would be easier. Instead of parsing the data using ActionScript, I could send Flash “pre-packaged” information. Unfortunately, I failed to realize the limits of the SWFObject and flashvars. Flashvars is an object but the object nodes must be strings. When I tried to pass my array to Flash using flashvars, the array was converted into one very long string.
Banging my head against the proverbial wall did finally yield a solution with many potential uses. Instead of trying to pass data to the SWF all at once, I used jQuery to process the data and pass each data point to the SWF as a function call. This is possible using Flash’s ExternalInterface object.
ExternalInterface allows Flash to interact with JavaScript in the containing page. In addition, you can access ActionScript functions in the SWF by creating a reference to the <object> in the HTML that contains the SWF. Using jQuery, doing so is simple.
var pointer-to-swf = $("body").find("object").get(0);
(assuming the only <object> tag in the page is for the swf)
Once the pointer is created, you can access ActionScript functions like so:
pointer-to-swf.ASFunction(params);
To solve my particular problem, I used jQuery to gather data from the HTML in the page. The data was then passed to Flash as a function call. The ActionScript function accepted the data and added it to the display.
ExternalInterface is not a new thing. Communicating between ActionScript and JavaScript has been possible for a while now. However, jQuery has only recently made it possible to easily manipulate HTML and perform AJAX methods. Combining jQuery with the ability to communicate with and control Flash opens up some big doors.
The biggest discovery, for me, is an alternative to flashvars or data loading methods within ActionScript. I can, instead, pass information to Flash as needed using jQuery.