Error in function to return Querystring in Javascript

Asked

Viewed 62 times

0

When using this function by calling the filter parameter of an error, instead of returning the value of the Querystring parameter it returns a Function.


//Retorna os valores da querystring
function QueryString(pName)
{
    //Cria as variaveis
    var lQueryValue;
    var lQueryString = location.search.replace(/\x3F/, "").replace(/\x2B/g, " ").split("&");
    //Verifica se tem alguma query string
    if (lQueryString != "")
    {
        //Cria o array
        var lArray = [];
        //Roda em todos os caracteres da querystring
        for (var i = 0; i < lQueryString.length; i++)
        {
            //Quebra a query string
            lQueryValue = lQueryString[i].split("=");
            //Seta o valor do array
            lArray[lQueryValue[0]] = unescape(lQueryValue[1]);
        }
        //Armazena o retorno
        var lReturn = lArray[pName];
        //Retorna
        return lReturn;
    }
    return null
}

I found that this seems to be an array problem.


var lArray = [];
lArray['filter'];

Should return the Undefined value or the correct value, but returns the prototype string of the array filter function.

Does anyone have any tips on how to solve this problem ?

1 answer

0

Friend what is happening is that in your array lArray you are added a key and then are trying to fetch it under another name. Change the following line:

lArray[lQueryValue[0]] = unescape(lQueryValue[1]);

For the following:

lArray[lQueryValue[0].substr(lQueryValue[0].indexOf('?') + 1)] = unescape(lQueryValue[1]);

I hope I helped friend

  • So, but it works with all the parameters requested in the url, only it doesn’t work with when I ask for the parameter 'filter'. I will try to apply your suggestion and warning if I have had any success. I appreciate your analysis.

  • I changed the line as you suggest and the error remained

  • It seems that there is a native filter function in the array object and for this reason it does not understand how to search for a value but rather as a reference to the filter function, so it returns the code of the function, see the link https://www.schools.com/jsref/jsref_filter.asp

  • The solution to the problem would be to try to make it search inside the array by the string filter value and not by the function reference.

  • Try using the following code: var lArray = []; lArray['filter']; .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.