Actionscript3 calls Javascript

Source: Internet
Author: User
Here we use the simplest code to demonstrate how to call the JavaScript code in the host HTML in the as3 code.

Mxml code: <? XML version = "1.0" encoding = "UTF-8"?>

<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns = "*"
Creationcomplete = "oncreationcomplete ()">

<Mx: script source = "externalinterfaceasjs. As"/>

<Mx: canvas width = "100%" Height = "100%">
<Mx: panel X = "0" Y = "0" Height = "100" width = "500" Title = "externalinterface: ActionScript to JavaScript">
<Mx: canvas Height = "100%" width = "100%">
<Mx: button x = "346" Y = "11" label = "send message to JavaScript" id = "submitbutton"/>
<Mx: textinput x = "5" Y = "11" width = "334" id = "tinput"/>
</MX: canvas>
</MX: Panel>

</MX: canvas>
</MX: Application>

As3 code:
Import flash. Events. mouseevent;
Import flash. External. externalinterface;
Import flash. system. Security;

Private function oncreationcomplete (): void
...{
Security. allowdomain ("*"); submitbutton. addeventlistener ("click", onsubmitclick );
}

Private function onsubmitclick (Event: mouseevent): void
...{
// Check whether the available attribute of externalinterface is true.
// If the player runs in an independent Flash Player, false is returned for this attribute.
If (externalinterface. available)
...{
VaR S: String = tinput. text;

// Call JavaScript Functions
Externalinterface. Call ("displaystring", S );
}
}

JavaScript code in HTML: <script language = "JavaScript">

// This function will be called by flash
Function displaystring (s)
...{
Alert ("from FLASH:" + S );
}

</SCRIPT>

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.