2009-03-31 7 views
-1

quelqu'un peut-il m'aider à changer cette fonction javascript à AS3?comment changer la fonction javascript en ActionScript 3?

merci :)

 function parseURLtoVars(strLocation){ 
     var rArray = new Array(); 
     var key; 
     var urlString = new String(strLocation); 

     if (urlString.search(/\?/)>-1){ 
      var qArray = urlString.split('?')[1].split('&'); 

      if (qArray.length > 0){ 
       for (key in qArray){ 
        var arVal = qArray[key].split('='); 
        if (arVal.length ==2){ 
         rArray[arVal[0]] = arVal[1]; 
        } else { 
         continue; 
        } 
       } 
       return rArray; 
      } else { 
       return false; 
      } 
     } 
     return false; 
    } 
+0

Pas vraiment un problème de codage spécifique ... –

Répondre

1

Que diriez-vous

private function parseURLtoVars(strLocation:String):* 
    { 
      var rArray:Array = new Array(); 
      var key:String; 
      var urlString:String = new String(strLocation); 

      if (urlString.search(/\?/)>-1){ 
        var qArray:Array = urlString.split('?')[1].split('&'); 

        if (qArray.length > 0){ 
          for (key in qArray){ 
           var arVal:Array = qArray[key].split('='); 
          if (arVal.length ==2){ 
            rArray[arVal[0]] = arVal[1]; 
          } else { 
            continue; 
          } 
        } 
        return rArray; 
        } else { 
        return false; 
        } 
      } 
      return false; 
    } 
1

ce params retourne comme un objet, pour retourner un booléen devrait être une édition simple.

function getParams(documentRoot):Object 
{ 
try { 
    var params:Object = LoaderInfo(documentRoot.loaderInfo).parameters; 
    var pairs:Object = {}; 
    var key:String; 
    for(key in params) { 
    pairs.key = String(params.key); 
} 
} catch(e:Error) { 
    return {}; 
} 
    return params; 
} 
1

Je crois qu'il suffit d'ajouter des définitions de type à votre fonction et vos variables.

Alors:

function parseURLtoVars(strLocation):Array 
{ 
    var rArray:Array = new Array(); 
    var urlString:String = new String(strLocation); 
    ... 
      for(var key:String in qArray) 
     ... 
     return rArray; 
     } else { 
     return null; 
     } 
    } 
    return null; 
} 

Je mis le retour de faux être nulls, mais vous pouvez changer votre type de retour de la fonction à l'objet, vous pouvez donc retourner quoi que ce soit hors de lui, mais je suppose que vous vouliez un tableau à être retourné.

1

Ce n'est pas exactement ce que vous avez demandé, mais AS 3 Je pense qu'il ya un moyen plus facile:

import flash.net.URLVariables; 

private function parseURLtoVars(strLocation:String):URLVariables { 
    strLocation = strLocation.indexOf("?") != -1 ? strLocation.split("?")[1] : strLocation; 
    return new URLVariables(strLocation); 
} 

Et vous pouvez l'utiliser comme ceci:

var testUrl:String = "test.php?key=value&key2=another_value"; 
var urlVars:URLVariables = parseURLtoVars(testUrl); 

for(var k:String in urlVars) { 
    trace(k + " = " + urlVars[k]); 
}