Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: Adobe Flex, .NET

Adobe Flex: Article

Flash 8 and JavaScript: "Building an Image Injector"

Using ExternalInterface to update HTML

In the ActionScript layer, place this code:

import flash.external.ExternalInterface;
import flash.filters.DropShadowFilter;

//a filter to give the thumbnails a little depth
var myDrop:DropShadowFilter = new
DropShadowFilter(4,45,0x000000,100,4,4,1,3);

//the directory to get images from
var dir:String = "imgs";
//main path to files
var mPath:String = "http://www.yourServer.com/";

//the function called when a thumbnail is selected
function sendFile(fl){
ExternalInterface.call
("opener.document.getElementById(ŒimageLoader').innerHTML=",
"<img src='"+dir+"/"+fl+"' border='0' />");
}

//the LoadVars object for collecting the image names from fileList.php
var imgs_lv:LoadVars = new LoadVars();
imgs_lv.onLoad = function(success){
if(success){
//the initial setting for columns and rows
var c = r = 0;
var imgs_array = this.files.split("~");
var tLength = imgs_array.length - 1;
var i = -1;
while(++i < tLength){
var img:MovieClip = createEmptyMovieClip("img"+i+"_mc", i+1);
img.createEmptyMovieClip("target_mc",
1).loadMovie(mPath+"makeThumb.php?sentFile="+dir+"/"+imgs_array[i]+"&sentSiz
e=40&sentZoom=1");
img._x = (c*50)+20;
img._y = (r*50)+55;//the plus 55 is to fit under the title at the top

c++;
if(c == 7){
c = 0;
r++;

}
//when a thumbnail is selected, call the function
img.file = imgs_array[i];
img.onRelease = function(){
sendFile(this.file);
}
//set the dropshadow
img.filters = [myDrop];
}
}else{
ExternalInterface.call("alert", "An error occured with the images.");
}
}
//go get the images
imgs_lv.sentDir = dir;
imgs_lv.sendAndLoad(mPath+"fileList.php", imgs_lv, "POST");

Most of this code is for controlling the alignment of the thumbnails. The first part grabs the two classes we need, including the Flash 8 DropShadow class to add some depth to the thumbnails. After that, we create a variable to hold the directory we are going to pass to the PHP script as well as a variable to hold the main path to the PHP scripts. The next part is the function that will be called when a thumbnail is clicked. Notice that in the call method, we are not only passing the property we want to access (the innerHTML property of the ŒimageLoader' element), but we are also using the keyword opener before we access it. What this keyword does is tell JavaScript we want to access the element in the HTML page that opened this HTML page. This means that not only can we access all the properties of the current HTML page we're in, but other HTML pages as well (provided they are on the same domain when setting allowScriptAccess to "sameDomain").

After that, a LoadVars object is created to handle the communication between the PHP and Flash. Within its onLoad event, a while loop is used to cycle through all the images and create thumbnails. This is where the makeThumb.php script comes in handy, because we know each image being returned will consistently be the same size. While we are creating the thumbnails, we set the onRelease event for them so when they are clicked, the sendFile function will be called updating the image in the other HTML page. Finally, we set the sentDir property of the LoadVars object and fetch all the images in that directory.

The only thing left to do is to publish and test it. So after you load the SWF, HTML and PHP pages (or pages that do the same thing as the PHP ones) to your server, you should be able to load the initial page, click on the image, which will open the Image Injector pop up, and swap out images in real time to see which one fits the best.

What's next? As much as I hate to use a cliché', you are bound only by your imagination. From some of the techniques in this article, you can see that not only do you have total access to JavaScript from Flash, but you can call functions, methods and even set properties of different pages from a single pop up window. Imagine being able to control layout and colors of one window from another, it will save a lot time on trial and error designing.

More Stories By David Vogeleer

David Vogeleer is the author of the newly release Flash 8 Professional Programming Unleashed from Sams publishing. He has been certified as both a Flash *Developer and Instructor and enjoys speaking and writing about Flash whenever possible. David currently works as a multimedia specialist at OSEC while still maintaining EMLlabs.com, a site dedicated to real-world Flash usage that he co-founded in 2004 and writing for FlashMagazine.com.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Italy News Desk 02/09/06 01:05:15 PM EST

Flash has been communicating with JavaScript for a long time through getURL and fscommand, but with Flash 8 it's easier than ever. With the ExternalInterface class, you cannot only call JavaScript functions, but also have JavaScript call Flash functions. And now that JavaScript is getting more and more publicity in the form of AJAX (Asynchronous JavaScript and XML), the ability to seamlessly integrate your Flash content within your HTML content is essential.