In Flex, The as method to call JS is:
1. Import the package (import flash. External. externalinterface ;)
2. Call with externalinterface. Call ("JS function name", parameter). The returned value is the value returned by the JS function.
The method for js to call as is:
1. Import the package (import flash. External. externalinterface ;)
2. Use externalinterface. addcallback ("function name for JS call", function name in as) in initapp to register
3. You can use document. getelementbyid ("flas ID in HTML") in JS to call the function name (parameter) set during registration.
As and JS communication addcallback failed
Reference: http://www.zhaohongri.cn /? P = 14
Scenario 1: Once flash is cached in the browser, addcallback will become invalid if it is in as at the beginning.
Scenario 2: When a JS function is called as a function, an error is reported on the page, prompting that the flash object cannot be found or the function is not defined. In the flash 8 era, for the externalinterface class, the document only describes how to use it, but does not specifically describe how to organize and structure the page. It has been in the CS3 era, the help section describes the correct function registration and JS call processes. For details, see the Flash CS3 help. The approximate code is as follows:
JS section:
<SCRIPT>
VaR jsready = false;
VaR swfready = false;
Function isready (){
Return jsready;
}
Function setswfisready (){
Swfready = true;
Getswf ("flashobj"). Fun ()
}
Function pageinit (){
Jsready = true;
}
Function getswf (moviename ){
If (navigator. appname. indexof ("Microsoft ")! =-1 ){
Return window [moviename + "_ ob"];
} Else {
Return document [moviename + "_ em"];
}
}
Onload = function (){
Pageinit ();
}
</SCRIPT>
Note that the return window [moviename + "_ ob"] and return document [moviename + "_ em"] are used in the getswf function. In IE, if the object tag and embed use the same ID, ie will not recognize the Flash Object accessed through JS, and FF is no problem.
As section
Private function registerjsfun (): void {
If (externalinterface. Available ){
Try {
VaR containerready: Boolean = iscontainerready ();
// Externalinterface. Call ("Ceshi", "registerjsfun:" + containerready );
If (containerready ){
// Register a function
Setupcallbacks ();
} Else {
// Check whether the system is ready
VaR readytimer: timer = new timer (100 );
Readytimer. addeventlistener (timerevent. Timer, timehandler );
Readytimer. Start ();
}
} Catch (error: Error ){
Trace (error)
}
} Else {
Trace ("external interface is not available for this container .");
}
}
Private function timehandler (Event: timerevent): void {
VaR isready: Boolean = iscontainerready ();
If (isready ){
Timer(event.tar get). Stop ();
Setupcallbacks ();
}
}
Private function iscontainerready (): Boolean {
VaR result: Boolean = Boolean (externalinterface. Call ("isready "));
Return result;
}
Private function setupcallbacks (): void {
Externalinterface. addcallback ("fun", fun );
Externalinterface. Call ("setswfisready ");
}
I will not explain the specifics. If I don't understand it, I can take a closer look at CS3's help. The general meaning is that when the page starts rendering, JS calls the SWF object. It is possible that the SWF object is not fully loaded, therefore, this trigger starts from the flash. When the flash is loaded, it starts to call a function on the page continuously to identify whether the page has been loaded. After pageonload, if this parameter is set to true, Flash has been loaded. At this time, flash starts to register the function and call JS on the page to call the Flash Object.
Example: A. mxml
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: SCRIPT>
<! [CDATA [
Import flash. External .*;
Public Function asfunc (): String {
Return sending_ti.text;
}
Public Function initapp (): void {
// Addcallback method allows JavaScript to call the flash time function
Externalinterface. addcallback ("flexfunctionalias", asfunc );
}
Public Function callwrapper (): void {
VaR F: String = "changedocumenttitle ";
// Externalinterface. Call (functionname: String, parameters) can call JavaScript Functions
// Parameter 1: functionname-the JavaScript function name you want to call must be in the string format.
// Parameter 2: parameters-parameters that need to be passed to the JavaScript function. They are separated by commas (,) and are optional.
VaR getjsvalue: String = externalinterface. Call (F, "New title ");
Received_ti.text = getjsvalue;
}
]>
</MX: SCRIPT>
<Mx: button id = "send_button" x = "368" Y = "100" Click = "initapp (); "label =" send "fontsize =" 12 "width =" 62 "/>
<Mx: textinput id = "received_ti" x = "148" Y = "62" width = "203" fontsize = "12"/>
<Mx: textinput id = "sending_ti" x = "148" Y = "100" width = "203" fontsize = "12"/>
<Mx: Label x = "105" Y = "65" text = "received" fontsize = "12"/>
<Mx: Label x = "105" Y = "103" text = "send" fontsize = "12"/>
<Mx: button x = "368" Y = "64" Click = "callwrapper ();" label = "receive" fontsize = "12" width = "62"/>
</MX: Application>
Index.html
<! Doctype HTML public "-// W3C // DTD nowrap HTML 4.01 transitional // en">
<HTML>
<Head>
<Base target = "_ Self">
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
</Head>
<Script language = "JavaScript">
Function callapp (){
VaR x = myflexapp. flexfunctionalias ();
Document. getelementbyid ('receivedfield '). value = X;
}
Function changedocumenttitle (){
Optional parameter Doc ument. Title =;
Return document. getelementbyid ('sendfield '). value;
}
</SCRIPT>
<Body style = 'overflow-X: hidden; overflow-Y: Den den '>
<Form name = "htmlform">
Send data to:
<Input type = "text" id = "sendfield"/>
<Input type = "button" value = "send" onclick = ""/> <br/>
<Br/>
Receive as data:
<Input type = "text" id = "receivedfield">
<Input type = "button" value = "receive" onclick = "callapp ();"/> <br/>
</Form>
<Object ID = "myflexapp" classid = "CLSID: D27CDB6E-AE6D-11cf-96B8-444553540000" codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width = "100%" Height = "500">
<Param name = movie value = "joinjs.swf">
<Param name = Quality value = high>
<Param name = scale value = noborder>
<Param name = bgcolor value = #000000>
<Embed src = "joinjs.swf" Quality = high width = "100%" Height = "500" scale = noborder bgcolor = #000000 name = "Th1" type = "application/X-Shockwave -Flash "pluginspage =" http://www.macromedia.com/go/getflashplayer "> </embed>
</Object>
</Body>