0

Tengo esta url www.unidosporelmundo.co/home.aspx?c=242&razon=unidos por el mundo&obra=unidos por el mundo&direccion=CRA 45:430 Número de la cotizacion:242

lo que necesito hacer es crear un arreglo con los parametros que trae los cuales son:

razon,obra,direccion,numero de la cotizacion.

Alguna idea de como puedo hacerlo.

function getAllUrlParams(url) {

  // get query string from url (optional) or window
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

  // we'll store the parameters here
  var obj = {};

  // if query string exists
  if (queryString) {

    // stuff after # is not part of query string, so get rid of it
    queryString = queryString.split('#')[0];

    // split our query string into its component parts
    var arr = queryString.split('&');

    for (var i=0; i<arr.length; i++) {
      // separate the keys and the values
      var a = arr[i].split('=');

      // in case params look like: list[]=thing1&list[]=thing2
      var paramNum = undefined;
      var paramName = a[0].replace(/\[\d*\]/, function(v) {
        paramNum = v.slice(1,-1);
        return '';
      });

      // set parameter value (use 'true' if empty)
      var paramValue = typeof(a[1])==='undefined' ? true : a[1];

      // (optional) keep case consistent
      paramName = paramName.toLowerCase();
      paramValue = paramValue.toLowerCase();

      // if parameter name already exists
      if (obj[paramName]) {
        // convert value to array (if still string)
        if (typeof obj[paramName] === 'string') {
          obj[paramName] = [obj[paramName]];
        }
        // if no array index number specified...
        if (typeof paramNum === 'undefined') {
          // put the value on the end of the array
          obj[paramName].push(paramValue);
        }
        // if array index number specified...
        else {
          // put the value at that index number
          obj[paramName][paramNum] = paramValue;
        }
      }
      // if param name doesn't exist yet, set it
      else {
        obj[paramName] = paramValue;
      }
    }
  }

  return obj;
}

getAllUrlParams().c; 
getAllUrlParams().razon; 
getAllUrlParams().unidos;

Me dice que la url es undefined

2
  • Por favor, comparte el código que lleves intentado hasta ahora :)
    – lromeraj
    el 14 may. 2017 a las 21:09
  • Si te dice que la url es undefined, entonces todavía no "Tienes" la url? revisa mi respuesta. el 14 may. 2017 a las 21:34

2 respuestas 2

2

Aqui te dejo un pequeño ejemplo de como hacerlo:

var url="www.unidosporelmundo.co/home.aspx?c=242&razon=unidos por el mundo&obra=unidos por el mundo&direccion=CRA 45:430 Número de la cotizacion:242";

// lo correcto es hasta aqui:
var json=splitURL(url,0);
console.log(json);

// si se quiere como se especifica en la pregunta
// entonces hacer lo siguiente:
var json=splitURL(url,1);
console.log(json);

function splitURL(url, opt) {
 var array = url.split("?")[1].split("&"),
  json = {};

 for (n in array) {
  tmp=array[n].split("=");
   json[tmp[0]]=tmp[1];
 }
 // lo correcto es hasta aqui:
 if (!opt)
  return json;

 // si se quiere como se especifica en la pregunta
 // entonces hacer lo siguiente:
 var tmp=json["direccion"];
 tmp=tmp.split(" Número de la cotizacion:");
 json["direccion"]=tmp[0];
 json["cotizacion"]=tmp[1];
 return json;
}

Espero esto te sirva, Saludos!! ;))...

1

Asumiendo que ya tienes la url, lo que debes hacer es pasar esa url por parámetro a la siguiente función:

function getAllUrlParams(url){
url = url.split("&");
var arr = [];
var aux, result, ultimo;
for(var i=1; i<url.length; i++){
	aux = url[i].split("=");
	result = aux[0];
	arr.push(result);
	if(i==3){
		result=aux[1];
		result = result.split(" ").slice(2,6);
		ultimo = result[3].split(":");
		result[3]=ultimo[0];
		result = result.join(" ");
		arr.push(result);
	}
}
return arr;
}

var params = getAllUrlParams("www.unidosporelmundo.co/home.aspx?c=242&razon=unidos por el mundo&obra=unidos por el mundo&direccion=CRA 45:430 Número de la cotizacion:242");
console.log(params);

Al final estoy imprimiendo por consola el arreglo que necesitas.

Espero haberte ayudado, estoy atento a cualquier duda.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.