lunes, 1 de agosto de 2016

Recibir datos con el método GET en javascript puro




La url debe tener una estructura similar a http://pagina_de_ejemplo.com?variable1=valor1&variable2=valor2

Script inicial:
function getGET(){
   var loc = document.location.href;
   var getString = loc.split('?')[1];
   var GET = getString.split('&');
   var get = {};//this object will be filled with the key-value pairs and returned.
   for(var i = 0, l = GET.length; i < l; i++){
      var tmp = GET[i].split('=');
      get[tmp[0]] = unescape(decodeURI(tmp[1]));
   }
   return get;
}
var get = getGET();

Cómo obtener los datos:
var variable1=get['variable1']; //se obtiene "valor1"
var variable2=get['variable2']; //se obtiene "valor2"

Néstor Mercedes

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comentarios:

Publicar un comentario