简体   繁体   中英

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?

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');}");
}

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM