Flex JavaScript Interactive Implementation Code _flex

Source: Internet
Author: User
Tags cdata closure xmlns
Keywords: externalinterface
Class Library used: SWFObject
/**
* Flex calls JavaScript functions
* @params functionname:string JavaScript function name
* @params ... params javascript function parameters
* @return Returns the return content of the JavaScript function
**/
Externalinterface.call (functionname:string, ... params);
Main.mxml
Copy Code code as follows:

<?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.ExternalInterface;
Import Mx.utils.ObjectUtil;
/**
* Invoke Java Script function
* @params evt:mouseevent
* @return void
**/
Private Function Myclick (evt:mouseevent): void {
var item:object = new Object ();
Item.name = "Dante";
item.address = "Beijing";

var results:object = Externalinterface.call ("MyFunc", item);
Results The object returned for the MyFunc function
Trace (objectutil.tostring (results));
}
]]-->
</mx:Script>

<mx:button label= "Click Me" click= "Myclick" (event); />
</mx:Application>

Index.html
Copy Code code as follows:

/**
* MyFunc function called by Flex
* @params Item Object
* @return Results returns an object to Flex
**/
function MyFunc (item) {
Alert (item.name+ "--" +item.address);

Creating objects
var results = {};
Results. Name= "Dante";
Results. Age = 23;
Results. Sex = "man";
return obj;
}

/**
* JavaScript calls Flex functions
* @params functionname:string JavaScript invoke the name of the Flex function
* @params closure:function The function that flex will call
* @return void
**/
Externalinterface.addcallback (functionname:string,closure:function);
Main.mxml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml"
layout= "Absolute" initialize= "Initapp ()" >
<mx:Script>
<!--[cdata[
Import Flash.external.ExternalInterface;
Import Mx.utils.ObjectUtil;
/**
* Initapp
* @return void
**/
Private Function Initapp (): void {
Register List
Externalinterface.addcallback ("SaveD", SaveData);

HTML interacts with Flex
Params.flashvars = "Xmlpath=hello&name=dante";
var item:object = Application.application.parameters;
Trace ("Xmlpath:" +item.xmlpath, "Name:" + item.name);
}
/**
* SaveData
* @params item:object java Script Pass the parameters
* @return void
**/
Private Function SaveData (item:object): void {
Trace ("JavaScript calls the Flex function succeeded!");
Nametxt.text = Item.name;
}
]]-->
</mx:Script>
<mx:textinput id= "Nametxt"/>
</mx:Application>

Index.html

HTML embedded SWF file, I used the swfobject, personally think is a good class library, simplifies the inclusion of code.
Note: JavaScript calls the Flex function, you must obtain the SWF ID, either through document.getElementById () or by using Swfobject.getobjectbyid ().
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script src= "Js/swfobject.js" src= "Js/swfobject.js" type= "Text/javascript" ></script>
<title>SWFObject</title>
<script type= "Text/javascript" language= "JavaScript" ><!--
var flashvars = false;
var params = {};
Params.flashvars = "Xmlpath=hello&name=dante";
var attributes = {};
ID, which is the ID of the SWF, is important to call the Flex method through it
Attributes.id = "swf01";
 
Embedded SWF file, this embedswf method, I'll add in the last
swfobject.embedSWF ("swf/main.swf", "Mypanel", "the" "," "," "9.0.0", "swf/expressinstall.swf", Flashvars,params, attributes);
/**
* Call Flex function
* @return void
**/
function Callflex (item) {
The SWF is obtained by ID, or document.getElementById () can be used.
var swf = document.getElementById ("swf01");
var swf = Swfobject.getobjectbyid ("swf01");
var item = {};
Item.name = document.getElementById ("Nametxt"). Value;
Call the Flex function, pass the parameter item:object
Swf.saved (item);
}
--></script>
<body>
<center>
<div id= "Mypanel" >flex content</div>
<table>
<tr>
<td>Name</td>
<td><input type= "text" id= "Nametxt"/></td>
</tr>
<tr>
&LT;TD colspan= "2" >
<a href= "Javascript:callflex ();" href= "Javascript:callflex ();" >call flex</a>
</td>
</tr>
</table>
</center>
</body>

OK, two kinds of interactive way already told everybody, try it quickly.
SWFObject Use supplement:
SWFOBJECT.EMBEDSWF ()
Swfobject.getobjectbyid ("Swfid")
swfobject.removeswf ("Swfid")
I. SWFOBJECT.EMBEDSWF:
Embedded SWF file, this function parameters are more swfobject.embedswf (Swfurl, id, width, height, version,expressinstallswfurl, Flashvars,params, attributes), the specific functions of each parameter are as follows:
Swfurl (String, required) specifies the URL of the SWF.
ID (String, required) specifies the ID of the HTML element (containing your replacement) that will be replaced by the Flash content.
Width (String, required) specifies the width of the SWF.
Height (String, required) specifies the height of the SWF.
Version (String, required) specifies the Flash Player version (format: major.minor.release) that corresponds to your SWF.
Expressinstallswfurl (String, optional) specifies the URL of the Express install SWF and activates the Adobe Express install.
Flashvars (String, optional) use Name:value to specify your flashvars.
params (String, optional) specifies the params of your nested object element with Name:value.
Attributes (String, optional) The property of the specified object with Name:value.
Note: You can omit the optional arguments without breaking the parameter order. If you don't want to use an optional parameter, but you use an optional argument later, you can simply pass in false as the value of the parameter. For JavaScript objects such as Flashvars, params, and attributes, you can also pass in an empty object {} accordingly.
Two. Swfobject.getobjectbyid ("Swfid")
The SWF object is obtained through Swfid, which allows the flex function to be invoked.
Three. swfobject.removeswf ("Swfid")
Remove SWF by Swfid.
Note: Removing SWF here requires a reminder, because embedswf is a replacement tag, not a fill. For example, the above example <div id= "Mypanel" >flex content</div&gt, and if you want to add the SWF after swfobject.removeswf is executed, you must re-create the <div id= " Mypanel "> or change the second parameter change embedswf replace the new label ID. Because the SWF is replaced here with a tuning <div>
Related Article

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.