Flash as and JavaScript Interactive instance program

Source: Internet
Author: User
Tags closure html page

ActionScript calls JavaScript

You need to introduce flash.external.ExternalInterface, where you use the call method of the Externalinterface class:

Externalinterface.call (functionname:string,... arguments):*, Fuctionname:

The name of the JavaScript function to invoke, arguments: parameter, optional.

Calls a function exposed by the Flash Player container, passing 0 or more parameters. If the required function is not available, the call returns NULL, otherwise it returns the value supplied by the function. Recursion is not allowed, and a recursive call generates a null response.

If the container is an HTML page, this method calls the JavaScript function in the <script> element.

If the container is some other ActiveX container, this method broadcasts the event with the specified name, and the container handles the event.

If the container hosts the Netscape plug-in, you can write custom support or embed HTML controls to the new Npruntime interface and embed Flash Player inside the HTML control. If you embed HTML controls, you can communicate with Flash Player by using a JavaScript interface that communicates with the native container application.

JavaScript calls ActionScript


You need to introduce Flash.external.ExternalInterface, which uses the Addcallback method of the Externalinterface class:

Externalinterface.addcallback (functionname:string, closure:function): Void,functionname:

The name of the function to register, closure: the corresponding execution function.

After the ActionScript method is registered as callable from the container, and the Addcallback () is successfully invoked, JavaScript or ActiveX code in the container can invoke functions registered in Flash Player.

In the HTML page that contains the SWF file, set the parameters in the Object tab of the file:

The code is as follows Copy Code

<param name= "allowscriptaccess" value= "Always"/>

In the SWF file, add the following ActionScript:

Flash.system.Security.allowDomain (Sourcedomain)

These 2 explanations are designed to solve the security sandbox problem, but in practice, we often encounter more headaches.

Concrete examples

Interactive.fla

New FLA file, stage size is 300*80, color is #cccccc, class is specified as interactive;
Position (10,15) to add a 260*22 size label label;
Position (10,50) Add the 170*22 size textInput textInput;
Position (190,50), Add the button button for the 100*22 size.

The code is as follows Copy Code

<script type= "Text/javascript" src= "Interactive.js" ></script>
<body>
<table><tr><td>
<object id= "asfunction" Data= "interactive.swf"
Type= "Application/x-shockwave-flash" width= "height=" >
</object>
</td></tr><tr><td>
<input id= "Jsinput" type= "text"/>
<input type= "button" value= "Invoke as" onclick= "Callas ()"/>
</td></tr></table>
</body>

function Jsmethod (ARG) {
Alert ("As called JS success, parameter is:" + arg);
Return ("As called JS success!") ");
}

function Callas () {
var myflexfun = document.getElementById ("asfunction");
var result = Myflexfun.callas (Jsinput.value);
Alert ("Return value:" + result);
}

Package
{
Import Flash.display.Sprite;
Import flash.events.MouseEvent;
Import Flash.external.ExternalInterface;
public class Interactive extends Sprite
{
Public Function Interactive ()
{
Init ();
}
Private Function init (): void
{
Registers the method Asmethod as callable from the container
Externalinterface.addcallback ("Callas", Asmethod);
Button.addeventlistener (Mouseevent.click,buttonclick);
}
Private Function ButtonClick (event:mouseevent): void
{
Trace (Textinput.text);
Label.text = "As Call js ...";
var result:string = Calljs (Textinput.text);
Label.text = "return value:" + result;
}
ActionScript calls JavaScript;
Private Function Calljs (arg:string): String
{
Invokes the function of the specified JavaScript-side and sequentially specifies the arguments passed to the JavaScript-side function
Return Externalinterface.call ("Jsmethod", Arg);
}
When JavaScript invokes ActionScript, the ActionScript-side function;
Private Function Asmethod (arg:string): String
{
Label.text = arg;
Return "JS called as success! ";
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.