Obtener parámetros de la url con Javascript

Para obtener un parámetro de la url con el lenguaje de programación Javascript hay que implementar el siguiente código:

Url del ejemplo:

http://tecnicoweb.es/?idUsuario=3&pelo=rubio&ojos=verdes

Código para obtener el valor del parámetro «idUsuario».

var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
var anuncioParam = urlParams.get('idUsuario');

Paso a paso.

window.location.search;

Obtenemos «?idUsuario=3&pelo=rubio&ojos=verdes»

var urlParams = new URLSearchParams(queryString);

URLSearchParams es un objeto y te proporciona diferentes funcionalidades, podemos ver todas su opciones aquí

var anuncioParam = urlParams.get('idUsuario');

Se extra el parametro que le pasamos al .get.

console.log(anuncioParam);

30.847 thoughts to “Obtener parámetros de la url con Javascript”

  1. Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks

  2. Thanks for another fantastic post. The place else may
    just anyone get that type of info in such an ideal manner of writing?

    I have a presentation subsequent week, and I am at the search for such info.

  3. Generic Viagra is typically called sildenafil, and it has the same active ingredient and dosage as Viagra, but it is not sold under the brand name cialis online generic Taking Viagra or Levitra on a full stomach will not only reduce their peak concentration in the body by 30 to 50 but in addition will delay their peak concentration by one hour