Error message

  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterMetaData is deprecated in SelectQuery->addMetaData() (line 997 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterMetaData is deprecated in SelectQuery->addMetaData() (line 997 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property SelectQuery::$alterTags is deprecated in SelectQuery->addTag() (line 978 of /var/www/html/includes/database/select.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1887 of /var/www/html/includes/database/query.inc).
10 Julio 2017 clcanela
18631 1

Cómo obtener los parámetros de la URL vía JavaScript o jQuery


18631 1

Algunas veces es necesario obtener los parámetros enviados vía GET en la URL de una página, es decir, todos los pares llave-valor que tienen el formato llave=valor y que vienen separados por ampersand (&) Es posible hacer esto con JavaScript plano (y por ende, compatible con jQuery) usando el objeto location que está disponible en JavaScript, el código es el siguiente:

function parametroURL(_par) {
  var _p = null;
  if (location.search) location.search.substr(1).split("&").forEach(function(pllv) {
    var s = pllv.split("="), //separamos llave/valor
      ll = s[0],
      v = s[1] && decodeURIComponent(s[1]); //valor hacemos encode para prevenir url encode
    if (ll == _par) { //solo nos interesa si es el nombre del parametro a buscar
      if(_p==null){
      _p=v; //si es nula, quiere decir que no tiene valor, solo textual
      }else if(Array.isArray(_p)){
      _p.push(v); //si ya es arreglo, agregamos este valor
      }else{
      _p=[_p,v]; //si no es arreglo, lo convertimos y agregamos este valor
      }
    }
  });
  return _p;
}

Ahora, la explicación

Esta función parametroURL() nos devuelve el parámetro que estamos buscando en la URL, si el parámetro es un arreglo, nos regresa los valores que coincidan con el nombre del parámetro (dentro de un array), de otra forma sólo nos devuelve el valor. Si el parámetro no existe, se devuelve null

Tomamos por hecho que el estándar para separar cada parámetro es con el símbolo & por lo que se itera la cadena de texto separándola con este caracter como delimitador. 

Dentro de cada par, los separamos por el símbolo de igual (=) y hacemos las validaciones para que nos devuelta solmente el parámetro que solicitamos

Por ejemplo, para la URL http://ejemplo.com/pruebas?p1=valor1&par2=v2&aru=va1&aru=va2&aru=va3

Al buscar los parámetros, obtenemos:

parametroURL('aru');
// ["va1", "va2", "va3"]
parametroURL('par2');
// "v2"
parametroURL('p1');
// "valor1"
parametroURL('otro');
// null

De esta forma podemos obtener un parámetro de nuestra URL llamando a nuestra función.

¡Felices Sentencias!

Quizá te interesen
Cómo crear objetos JSON en PHP o en JavaScript
Cómo diferenciar entre un evento por usuario o programático
Tutorial: Cómo leer un JSON con jQuery
Recibirás notificaciones de las respuestas a tu comentario y obtendrémos tu avatar de Gravatar.com Tu correo no será publicado

d13 Julio 2021

d

Recibirás notificaciones de las respuestas a tu comentario y obtendrémos tu avatar de Gravatar.com Tu correo no será publicado