# Controlling Flash with Javascript



## Bubonic (Jun 19, 2004)

I've been playing around with this and I'm wondering if there is a way to control separate movieclips within a flash object with javascript. I know you can control the entire movie but I want to be able to set off a chain of events involving multiple movieclips in the same flash object.

What I have is a table (made with divs) containing information about residential lots. One lot per row. Able to click on the rows.

When the user clicks on a row, lets say row 21, of the table, the corresponding data is pulled from the xml in the flash and the lightbox in the flash pops up. Then when the user clicks on another row, the lightbox closes, the flash grabs the corresponding data for that row and 're-opens' the lightbox.

The user is already able to do this using the buttons provided in the flash but it would be awesome for the user to be able to control the flash with the informational table below as well.

A rather big post for a somewhat simple question but anyways, can you control multiple movieclips, within a single flash object, with javascript?


----------



## Bubonic (Jun 19, 2004)

Here's my code. I don't know why it's not working. I am using Actionscript 2.0.

HTML: An example of the tale row that a user would click on to grab the appropriate information in flash and display the lightbox. 

```
<div id="d_table">
    <div class="d_tr" id="0" onClick="javascript:doFlash(0);">
       <div class="d_td">Strata Lot 21</div>
       <div class="d_td">$399,900.00</div>
       <div class="d_td_plan">Plan A</div>
       <div class="d_td">August 10, 2008</div>
    </div>
</div>
```
Javascript:

```
var isLightboxOpen = 0;
var tableRows = 15;

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function clearBackgrounds() {
   for (var i = 0; i < tableRows; i++) {
     document.getElementById(i).style.background = "transparent";
   }
}

function setBackground(id) {
    clearBackgrounds();
    document.getElementById(id).style.background = "#C4D0F2";
}

function doFlash(id) {
    setBackground(id);

	// This should close the lightbox
    if (isLightboxOpen) {
		closeLightbox();
	}
    setTimeout("openLightbox("+id+")", 1000);
}

function closeLightbox() {
	//getFlashMovie("plans").closeLightBox();
	isLightboxOpen = 0;
}

function openLightbox(id) {
	getFlashMovie("plans").openLightBox(id);
	//document.plans.openLightBox(id);
	isLightboxOpen = 1;
}
```
Actionscript:

```
import flash.external.ExternalInterface;

var select = 0;  //Variable used to determine which child node to pull data from in the xml.
                       // 0 is the first child, 1 is the second child etc.

function openLightboxFromJavaScript(selectInt):Void {
     	        select = selectInt;
		lightbox.lightbox_bg.gotoAndPlay(2); //initiates lightbox opening
		update();  //fills in all the text boxes etc in the light box with info from the xml.
		disable_btns(); //disables all the flash buttons while lightbox is open.
}

function closeLightboxFromJavaScript():Void {
     	        select = 0;
		lightbox.gotoAndPlay(11); //initiates lightbox closing
		enable_btns(); //enables all buttons
}

ExternalInterface.addCallback("openLightbox", this, openLightboxFromJavaScript);
ExternalInterface.addCallback("closeLightbox", this, closeLightboxFromJavaScript);

//Example of code used for buttons in flash.

lightbox.lightbox_main.close_btn.onRelease = function() {
	lightbox.gotoAndPlay(11);
	select = 0;
	enable_btns();
}
buttons.btn_21.onRelease = function() {
		select = 0;
		lightbox.lightbox_bg.gotoAndPlay(2);
		update();
		disable_btns();
}
```
Sources: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html and http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15683

Hope someone can help me out.


----------



## Bubonic (Jun 19, 2004)

Still need some help on this one. 


*bump*


----------



## Bubonic (Jun 19, 2004)

Still need your help. 

bump.


----------

