views:

369

answers:

4

anybody can help me to change that javascript function to AS3?

thanks :)

     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;
 }
+1  A: 

this returns params as an object, to return a boolean should be a simple edit.

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;
}
madcolor
+1  A: 

I believe you just have to add type definitions to your function and variables.

So:

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;
}

I set the return of false to be nulls, but you can change your function return type to Object so you can return anything out of it, but I assumed you wanted an array to be returned.

CookieOfFortune
+1  A: 

How about

 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;
    }
Scott Evernden
+1  A: 

It's not exactly what you asked, but in AS 3 I think there's an easier way:

import flash.net.URLVariables;

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

And you could use it like this:

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]);
}
Juan Pablo Califano