Summarizing flex interacting with browsers

Source: Internet
Author: User

In order to extend the application functionality when using Flex as a Web application, there is bound to be more interaction with the browser, which will use the knowledge described below
1. Implement URL jump in Flex (using Flash.net.navigateToURL () method such as:)
* * param1: URL to jump param2: Jump Way _blank/_self/navigatetourl (New URLRequest ("Http://blog.csdn.net/xingjunli"), "_bla NK ");
2. Use Flashvars to replace parameters (use Application.application.parameters property)
The page embeds the SWF file code (we can set Flashvars information through JS Dynamic to achieve the purpose of data interaction)
<object classid= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width= "100%" height= "100%" id= "MYSWF" > <param Name= "movie" value= "main.swf"/> <param name= "quality" value= "High"/> <param name= "bgcolor" value= "#869ca7 "/> <param name=" allowscriptaccess "value=" Samedomain "/> <param name=" flashvars "value=" UserName=Andy_ Liu&age=18&address=hongkong "/> </object>
Get parameter code in Flex:
Private Function Onflashvars (e:mouseevent): void//Get page parameter {var parameters:object = Application.application.parameters; Laburl.text = Application.application.url.toString (); http://localhost:8889/main.swf var param1:string = parameters. UserName; Andy_liu var param2:int = parseint (parameters. Age); var param3:string = parameters. address; Hongkong/* Txtparam1.text = param1; Txtparam2.text = String (param2); Txtparam3.text = param3; */ }
3. Call JavaScript function in Flex
First in the page to complete the JS function (note in Flex can only invoke the current page to introduce JS functions)
function Showuserinfo (name,age,address) {alert (' Name: ' + name+ '/n ' + ' Age: ' + age + '/n ' + "address ' +address); return" Cal L successful! "}
In Flex, call the method and get the JS function return value (using the Externalinterface.call () method):
Private Function Oncalljs (e:mouseevent): void {/* * param1: Current page JS function method name * Param ... The parameters passed 0-n A/var resultvalue:string = Externalinterface.call ("Showuserinfo", Txtparam1.text,txtparam2.text, Txtparam3.text); Resultvalue = Call successful! }
4. Call ActionScript function in JavaScript
The JS function called in the page
function Callflexmethod () {var swf= "myswf"; var container;//alert (navigator.appname); if (Navigator.appName.indexOf (" Microsoft ") >=0 {container = document;} else {container = window;} container[swf].flexfunction1 (); Non-parametric method Container[swf].flexfunction2 ("myhtmlrequest!"); /Parameter Transfer Method alert (Container[swf].flexfunction3 ()); Include return value method}

How to register and invoke execution in flex:
Important You must perform the following registration in flex to invoke Private function registercallbacks () in js: void {externalinterface.addcallback ("FlexFunction1 ", JsCallBack1); Externalinterface.addcallback ("FlexFunction2", JsCallBack2); Externalinterface.addcallback ("FlexFunction3", JSCALLBACK3); Private Function JsCallBack1 (): void {alert.show ("call the method!");} Private Function JsCallBack2 (params:*): VO ID {alert.show ("Call Method2:" + String (params));} Private Function JsCallBack3 (): String {return math.random (). toString ();
5, Browsermanager Management component application
Browsermanager is a Singleton manager that acts as a proxy between browsers and applications. Use it to access the URL in the browser's address bar, which is similar to accessing the Document.location property in JavaScript. Schedule These events when the URL property changes. The listener can then respond, change the URL, or prevent other objects from obtaining this event.
Get BM Example:
private var Bm:ibrowsermanager = Browsermanager.getinstance ();
A, get the parameters passed by the URL:
Private Function Getbrowservalue (): void {//url = "http://localhost/browser/main.html#name=peter;age=25;address= Shenzhen "var o:object = Urlutil.stringtoobject (bm.fragment); Txturlname.text = O.name; Peter Txturlage.text = O.age; Txturladdress.text = o.address; Shenzhen
b, get browser information (Request URL: "Http://localhost:8889/main.html#name=peter;age=25;address=shenzhen%20china")
       private function Onbrowsermanager (e:mouseevent): void {txtbmoutput.text = "full URL:/n "+ Bm.url +/n/n"; Txtbmoutput.text + = "Base URL:/n" + bm.base + "/n/n"; Txtbmoutput.text + = "url Fragment url:/n" + bm.fragment + "/n-----------/n"; var o:object = Urlutil.stringtoobject (bm.fragment); Txtbmoutput.text + + "Object: N" + objectutil.tostring (o) + "/n/n"; Txtbmoutput.text + = "Name:/n" + o.name + "/n/n"; Txtbmoutput.text + = "Age:/n" + o.age + "/n/n"; Txtbmoutput.text + = "Address:/n" + o.address + "n-----------/n"; Txtbmoutput.text + + "URL Port:/n" + urlutil.getport (bm.url) + "/n/n"; Txtbmoutput.text + = "URL Protocol:/n" + urlutil.getprotocol (bm.url) + "/n/n"; Txtbmoutput.text + = "URL Server:/n" + urlutil.getservername (bm.url) + "/n/n"; Txtbmoutput.text + = "URL Server with Port:/n" + urlutil.getservernamewithport (Bm.url) +/n-----------/N; /* Execution Result: full Url:http://localhost:8889/main.html#name=peter;age=25;address=shenzhen%20china Base url:http://localhost:8889/main.html URL Fragment Url:name=peter;age=25;address=shenzhen%20china-----------Object: (Object) #0 address = "Shenzhen" age = name = "Peter" Name:peter age:25 address:shenzhen-----------URL port:8889 url protocol:http url server:localhost url Server with port:localhost:8889-----------*/}
C, Browsermanager also includes some other browser extension methods such as: Bm.settitle ("new title: Training!") Modify page title, etc... Reference http://livedocs.adobe.com/flex/3/html/help.html?content=deep_linking_3.html
6, in flex through the implementation of Mx.managers.IHistoryManagerClinet interface to implement custom history manager
Custom TextInput
<?xml version= "1.0" encoding= "Utf-8"?> <mx:textinput xmlns:mx= "Http://www.adobe.com/2006/mxml" Change me! "implements=" mx.managers.IHistoryManagerClient "creationcomplete=" Mx.managers.HistoryManager.register ( this); "Change=" TextChanged (event) > <mx:Script> <!--[cdata[import Mx.managers.HistoryManager; private function TextChanged (e:event): void {historymanager.save ();} public Function SaveState (): Object {return {text:text};} p ublic function LoadState (state:object): void {var newstate:string = State?state.text: ""; if (newstate!= text) {text = unescape (newstate);}} ]]--> </mx:Script> </mx:TextInput>
When we use the TextInput defined above, we will find that in the URL of the browser, we will dynamically create such information as "#app =a4e4&8211-text=asdfasdfasdfsd%20" So flex uses browser URL to record component changes in SWF files implemented save history data has been implemented for history management.

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.