Interaction between flex and JavaScript !!
Flexjavascript. mxml
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "horizontal" paddingtop = "120" width = "650" Height = "420" creationcomplete = "initapp ()">
<Mx: style>
. Style {padding: 5 ;}
</MX: style>
<Mx: SCRIPT>
<! [CDATA [
Import flash. External. externalinterface;
Import MX. Controls. Alert;
// Initialize the callback function
Public Function initapp (): void
{
This. setupcallbacks ();
}
// Call Action Script in Javascript
Public Function callfromjavascript (S: string): void
{
// Alert. Show (S, "flex alert !! ");
Valuefromjavascript. Text = s;
}
// Call Javascript in Action Script
Private function sendtojavascript (): void
{
Externalinterface. Call ("getfromflex", valuefromjavascript. Text );
}
// Callback function
Private function setupcallbacks (): void
{
Externalinterface. addcallback ("callfromjavascript", callfromjavascript );
}
]>
</MX: SCRIPT>
<Mx: panel title = "externalinterface sample" stylename = "style" width = "350" Height = "200">
<Mx: hbox>
<Mx: textinput id = "valuefromjavascript" text = "Halo from Flex"/>
<Mx: button click = "This. sendtojavascript ()" label = "send to JavaScript"/>
</MX: hbox>
<Mx: hbox>
<Mx: linkbutton id = "linkbutton1" label = "Goto Google.com"
Click = "navigatetoURL (New urlrequest('temple.html '),' _ Self ')"/>
<Mx: button id = "button1" label = "testjavascript"/>
</MX: hbox>
</MX: Panel>
</MX: Application>
Flexdeskcript.html
<Body scroll = "no">
<Form ID = "form1" name = "form1">
<Script language = "JavaScript" type = "text/JavaScript">
<! --
// Version check for the flash player that has the ability to start player product install
(6.0r65)
VaR hasproductinstall = detectflashver (6, 0, 65 );
// Version check based upon the values defined in globals
VaR hasrequestedversion = detectflashver (requiredmajorversion, requiredminorversion,
Requiredrevision );
If (hasproductinstall &&! Hasrequestedversion ){
// Do not modify the following four lines
// Location visited after installation is complete if installation is required
VaR mmplayertype = (isie = true )? "ActiveX": "plugin ";
VaR mmredirecturl = Window. location;
Document. Title = Document. Title. Slice (0, 47) + "-Flash Player installation ";
VaR mmdoctitle = Document. title;
Ac_fl_runcontent (
"Src", "playerproductinstall ",
"Flashvars", "mmredirecturl =" + mmredirecturl + '& mmplayertype =' + mmplayertype
+ '& Mmdoctitle =' + mmdoctitle + "",
"Width", "650 ",
"Height", "420 ",
"Align", "Middle ",
"ID", "flexjavascript ",
"Quality", "high ",
"Bgcolor", "#869ca7 ",
"Name", "flexjavascript ",
"AllowScriptAccess", "samedomain ",
"Type", "application/X-Shockwave-flash ",
"Pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} Else if (hasrequestedversion ){
// If we 've detected an acceptable version
// Embed the Flash content SWF when all tests are passed
Ac_fl_runcontent (
"Src", "flexjavascript ",
"Width", "650 ",
"Height", "420 ",
"Align", "Middle ",
"ID", "flexjavascript ",
"Quality", "high ",
"Bgcolor", "#869ca7 ",
"Name", "flexjavascript ",
"AllowScriptAccess", "samedomain ",
"Type", "application/X-Shockwave-flash ",
"Pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} Else {// flash is too old or we can't detect the plugin
VaR alternatecontent = 'alternate HTML content shoshould be placed here .'
+ 'This content requires the Adobe Flash Player .'
+ '<A href = http://www.adobe.com/go/getflash/> get flash </a> ';
Document. Write (alternatecontent); // insert non-Flash Content
}
// -->
</SCRIPT>
<NoScript>
<Object classid = "CLSID: D27CDB6E-AE6D-11cf-96B8-444553540000"
Id = "flexjavascript" width = "650" Height = "420"
Codebase = "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<Param name = "movie" value = "flexdeskcript.swf"/>
<Param name = "quality" value = "high"/>
<Param name = "bgcolor" value = "#869ca7"/>
<Param name = "allowScriptAccess" value = "samedomain"/>
<Embed src = "flexdeskcript.swf" Quality = "high" bgcolor = "#869ca7"
Width = "650" Height = "420" name = "flexjavascript"
Align = "Middle"
Play = "true"
Loop = "false"
Quality = "high"
AllowScriptAccess = "samedomain"
Type = "application/X-Shockwave-flash"
Pluginspage = "http://www.adobe.com/go/getflashplayer">
</Embed>
</Object>
</NoScript>
<Script language = "JavaScript" type = "text/JavaScript">
Function getfromflex (s)
{
Alert (S. tostring ());
}
// Send to flex
Function sendtoflex ()
{
Thismovie ("flexjavascript"). callfromjavascript
(Document. form1.valueforflex. value );
}
Function thismovie (moviename)
{
If (navigator. appname. indexof ("Microsoft ")! =-1)
{
Return window [moviename];
}
Else
{
Return document [moviename];
}
}
</SCRIPT>
<Input type = "text" name = "valueforflex" size = "50" value = "Halo from JavaScript"/>
<Input type = "button" value = "getfromflex" onclick = "sendtoflex ()"/>
</Form>