Pass data from an ULR to an array

0

I have this url www.unidosporelmundo.co/home.aspx?c=242&razon=united by the world & work = united by the world & address = CRA 45: 430 Number of the quote: 242

What I need to do is create an arrangement with the parameters it brings which are:

  

reason, work, address, number of the quote.

Some idea of how I can do it.

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;

He tells me that the url is undefined

    
asked by Eduard 14.05.2017 в 22:35
source

2 answers

2

Here is a small example of how to do it:

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

I hope this serves you, Greetings !! ;)) ...

    
answered by 15.05.2017 / 00:04
source
1

Assuming that you already have the url, what you should do is pass that url by parameter to the following function:

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

In the end, I'm printing the console you need by console.

I hope I have helped you, I am attentive to any questions.

    
answered by 14.05.2017 в 23:19