How to read QueryString parameters in Javascript

2

How do I collect arguments from an HTML page?

Type I've tried to use that neither C # / ASP.NET:

var conteudo = Response.QueryString["usuario"];

Considering a query string as:

/default.html?usuario=pt

Then he would tell me what was after the "=".

How to get the value with JavaScript?

    
asked by anonymous 27.06.2014 / 21:31

1 answer

4
function getUrlParameters(parameter, staticURL, decode){
   /*
    Function: getUrlParameters
    Descrição: Obtem o valor dos parâmetros da URL atual ou URL estática
    Author: Tirumal
    URL: www.code-tricks.com
   */
   var currLocation = (staticURL.length)? staticURL : window.location.search,
       parArr = currLocation.split("?")[1].split("&"),
       returnBool = true;

   for(var i = 0; i < parArr.length; i++){
        parr = parArr[i].split("=");
        if(parr[0] == parameter){
            return (decode) ? decodeURIComponent(parr[1]) : parr[1];
            returnBool = true;
        }else{
            returnBool = false;            
        }
   }

   if(!returnBool) return false;  
}

This code has been taken from Code-tricks , you can use it from following form:

From the current window location:

var parametro1 = getUrlParameters("nomeDoParametro", "", true);

From a static url:

var parametro1 = getUrlParameters("usuario", "http://www.exemplo.com/default.html?usuario=pt", true);

Fiddle

    
27.06.2014 / 21:52