Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you trigger javascript functions from flash?

How do you trigger a javascript function using actionscript in flash?

The goal is to trigger jQuery functionality from a flash movie

like image 971
Jiaaro Avatar asked Oct 06 '08 12:10

Jiaaro


People also ask

How do you trigger a function in JavaScript?

onchange: It is triggered when an HTML element changes. onclick: It is triggered when an HTML element is clicked. onmouseover: It is triggered when the mouse is moved over a HTML element. onmouseout: It is triggered when the mouse is moved out of a HTML element.


2 Answers

Take a look at the ExternalInterface-Class.
From the AS3-Language Reference:

The ExternalInterface class is the External API, an application programming interface that enables straightforward communication between ActionScript and the Flash Player container– for example, an HTML page with JavaScript. Adobe recommends using ExternalInterface for all JavaScript-ActionScript communication.

And it's work like this:

ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
ExternalInterface.call("sendToJavaScript", input.text);

You can submit parameters and recieve callbacks...pretty cool, right? ;)

As I know it will also work on AS2...

like image 133
jochil Avatar answered Oct 19 '22 16:10

jochil


As Jochen said ExternalInterface is the way to go and I can confirm that it works with AS2.

If you plan to trigger navigation or anything that affects the area where the flash sits don't do it directly from the function you call from flash. Flash expects a return value from the function it calls and if the flash object does not exist when the function is completed the flash plugin will crash.

If you need to do navigation or alter the content you can add a setTimeout call (into your js function). That will create a new thread and give flash the return value it expects.

like image 39
Gene Avatar answered Oct 19 '22 17:10

Gene