James Jeffery
James Jeffery

Reputation: 2101

Javascript and Flash in AS3

Is it possible to use Javascript in flash. For example, as a simple test I am trying to use Javascript's alert method when a button is clicked.

I am using AS3. Is it possible?

Upvotes: 1

Views: 2163

Answers (2)

Marty
Marty

Reputation: 39456

No.. Flash uses ActionScript 3.

You can call a JavaScript function on the same page as an embedded SWF using ActionScript's ExternalInterface class, though.

A quick demo on implementing ExternalInterface:

if(ExternalInterface.available)
    ExternalInterface.call("alert", "Hello!");

Tip: ExternalInterface calls will not work locally unless you add the location of the project in this security settings panel and check "always allow".

Upvotes: 1

inhan
inhan

Reputation: 7470

Like Marty mentioned, you can use the ExternalInterface class to execute a Javascript. Your options are to execute a function embedded in the (html) page code and create one from scratch. Because alert is a default function you can use its name as the first parameter for the call method (of ExternalInterface) and the string as the 2nd one.

If you provide a single parameter, you might wanna write a function instead to execute (or return) something.

btn.addEventListener(MouseEvent.CLICK, btnClicked);
function btnClicked(e:MouseEvent):void {
    ExternalInterface.call("alert","something");
    // or
    ExternalInterface.call("function(){alert('something');}");
}

Upvotes: 2

Related Questions