Flex gets parameters from page URL Trungeons Interaction 1

In order to obtain the URL parameters passed in from html, the usual order of delivery is: html container—>JavaScript—>ExternalInterface—> ActionScript

Pro-test method 3 is available

AboutflexHow to add a page Passing parameters, I used to have notes in this regard,flexInteract with the background through non-AmfPHPmethodsURLLoader+URLRequest+URLVariablesThe method can also be used to flexThe page passes parameters. BackendphpThe end receiving parameters is very simple, let’s discuss it nowflexHow to handle the pageurlThe passed parameters.
There are many articles on this topic on the Internet, but there are some problems. Let me summarize. Write here.
Method 1: flexReceive values ​​from web pages! ~

Code span>
1
2 3applicationComplete=”initApp()”>
4
5 6< /span>“Will run the app deployed at http://{serverURL}:{port}/MyGreatApp.html” />
7
8 9[Bindable]
10 var serverURL:String;
11
12[Bindable]
13 var port:String;
14
15 function initApp():void{
16 serverURL=Application.application.parameters.serverURL;
17port=Application.application.parameters. port
18
19]]>
span>20
21

The author writes at the end of the article:externallytest.swf?serverURL=String&port=String called in the web page!
This is indeed no problem, but usuallyflexprograms are all based onhtmlWrap the swf files for the container. If you use test.html?serverURL=String&port=String to pass parameters, and then use the article provided The code can’t get the parameters.

Method 2 :flex 3 From the page span>urlFetching parameter value
This method was proposed by the people in the garden.
When passed< span style="c olor:#333333">http://xxxxx/xxx.mxml?name=hermitor http://xxxxx/xxx.swf?name=hermit, you can directly callthis. parameters.nameinflex Take the url parameters inside.
When passed http://xxxxx/xxx.html?name=hermitYou need to modify itjs span>The file can continue to be usedthis.parameters.name
Add to the pagejsMethod

< p style="background:#eeeeee">functiongetparafromurl (){
varurl,pos,parastr,para;
url=window. location.href;
pos = url.indexOf(“?”)
Parastr = url.substring(pos+1);
return< /span>parastr;
}

in< /span>AC_FL_RunContentAdded

“FlashVars”, getparafromurl()

If the page isflex builder automatically generated by a template
Then you need to enter the fileindex.template.html Add the code above.
but if you wantjswhen disabled,flexCan still work
You need to span>……Interested students can check the original text, but don’t recommend this method.

Method 3:flexHow to get the programhtmlContainer-passedURLParameter value
This method is more mature, and the principle is also very clear.

We often use Flex span>The program needs to be used from outsidehtmlto swfFile transfer parameters, (similar to test.html?name=jex&address=chengdu The parameter pair value after the question mark in the address)

The first thing to be clear is that generally we are usingFlex Builderduring Flexduring development, it will automatically be compiled with< span style="color:#333333">htmlcontainer willswfThe files are packed, so generally speaking, we run directlyhtml instead of running directly generated swfFile. AndFlexThe application needs to get externalhtmlThe parameters passed by the container are usually usedJavaScriptto get the corresponding parameters, and then letjavaScript pass to< /span> ActionScript.

inFlex In applications, we usually useExternalInterfaceClass,ExternalInterfacemainly used to make ActionScript directly withFlash PlayerThe container communicates. ExernalInterfaceClasses are usually used as ActionScript Communication withJavaScript bridge.

To get fromhtml span>incomingURLparameters, usually The order of delivery is:htmlcontainer— >JavaScript—>ExternalInterface—>ActionScript

Specific implementation:
inFlex, by callingExternalInterfacethecall method, the parameter is the method to be calledJavaScriptfunction and returnJSThe result of the function call. For example:

ExternalInterface.call< /span>(“JavaScriptfunction”);

InJSInWindowThe object is used to represent oneWebBrowser window, and the window’sLocationobject represents The currently displayed URL, so if you want to get itURL parameters in,

The following statement is usually used:

window.location.href.toString //Get the full text of the URL< span style="color:#333333">

 

window.location.search. substring  // Get the URL behind the question mark text

Note: herewindowAttribute referenceWindow< span style="color:#333333">the object itself, whileWindowthe objectlocationThe attribute refers toLocation span>Object.


Common parameters To give it in the form oftest.html?name=jex&address=chengdu , after getting After the URL text after the question mark, you need toTo decompose it, there are two ways at this time, one is that the decomposition process is in JS, and then pass the final result value to Flex, the other is Put the decomposition process in Flex to complete. Used herethe latter (so just writeAScode instead of writingJScode ^_^)

Code
1
2 3 creationComplete=”init()”>
4
5 < /span>6 importmx.controls.Alert;
7< /span>
8< span style="color:blue">private
var params:Object;
9
10private function init():void{
11btnID.addEventListener(MouseEvent.CLICK,clickHandler);
12 }
13
14privatefunction clickHandler(evt:Event):void{
15 Var args:Object = getParams();
16if(args.name!=null&& args.address!=null){
17dispID.text=”name:”+args.name+” “+”address:”+args .address;
18 }
19}
20< span style="color:black">
21private function getParams():Object {
22< span style="color:black">params={};
23var query:String = ExternalInterface.call(“window.location.search.substring”, 1);
24 if(query){
25 var pairs:Array= query.split(“&”);
26for(var i:uint=0;i i< pairs.length; i++) {
27 Var pos:int=pairs[i].indexOf(“= “);
28 / /Alert.show(String(pos));
29if(pos!= -1) {
30 Var argname:String= pairs[i].substring(0,pos);
31 Var value:String= pairs[i].substring(pos+1);
32
33 Params[argname]=value;33 br> 34 35 36
37returnparams;
38
39 ]]>
40
41< br> 42
43
44

The only disadvantage of this method is that it needs to know the name of the passed parameter. And I made some changes, and used one16-18line< span style="color:#333333">for/inSentence replacement:

< span style="color:teal">1for(var obj:Object in args){
2dispID.text+=obj .toString()+’:’+args[obj.toString()]+’
‘;
3 }

I met hereobjectThe value of the object, please refer to:FlexLearning–Object&&String
CreateObjectThe method is very simple, you can use newoperator to define one Object as follows:

var obj:Object = new Object();

obj.name = “flex”;

obj.version = “2”; < /p>

Here, we generated a new ObjectObjectobj, it has two attributes nameandversion span>. Of course, we can also define directly by assigning initial values.ObjectObject:

var obj:Object = {name: ”flex”, version: “2”}

You may have seen it,ObjectExactlyFLEX in the associative array (corresponding to the < /span>JavainMap span>). In the above example, the obj object defines two sets of key and value pairs, which are respectively Yes (key:name, value:flex) and (key:version, value:2). What I also want to tell you is that Object is used more thanMapEasier. You can either pass. To access, you can also access the key value through []. For example:

var myAssocArray:Object = {fname:”John”, lname:”Public”};

trace(myAssocArray.fname); // Output: John

< p style="background:white">trace(myAssocArray[“lname”]); // Output: Public

myAssocArray.initial = “Q”;

trace(myAssocArray.initial) ; // Output: Q

js get url parameter value

Today I encountered the need to get the parameters passed by the URL of another page on one page. At first, I instinctively thought of using split(“?”) step by step. Break down the required parameters.

After thinking about it, there must be an easier way! So I found two very simple and practical methods on the Internet, under mark

Method 1: Regular analysis method

span>

 



function getQueryString(name) {< /span>var reg = new RegExp(< span style="color:#000000">"(^|&)" + name + "=([ ^&]*)(&|$)", "i"); var r = window.location.search.substr( 1).match(reg); if (r != < span style="color:#0000ff">null) return unescape(r[2]); return null; }


Call this:

 



alert(GetQueryString("Parameter name 1")); alert(GetQueryString("Parameter name 2")); alert(GetQueryString("Parameter name 3"));< /span>


Method 2:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( &|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)< span style="color:#000000">", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Req uest = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null< /span>; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N' ]; </Script>


今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null ; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </< span style="color:#000000">Script>


今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。

后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单实用的方法,mark下

方法一:正则分析法

 

         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


这样调用:

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


方法二:

?
"FONT-SIZE: 16px" >

这样调用:

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


         



function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }


function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }

         



alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));


alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));

?
"FONT-SIZE: 16px" >

?
"FONT-SIZE: 16px" >

?
"FONT-SIZE: 16px" >

?

"FONT-SIZE: 16px" >

"FONT-SIZE: 16px" >

复制代码
         



<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>


复制代码

<Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 参数1,参数2,参数3,参数N; 参数1 = Request['参数1']; 参数2 = Request['参数2']; 参数3 = Request['参数3']; 参数N = Request['参数N']; </Script>

Leave a Comment

Your email address will not be published.