How do I redirect and send url get value to another page using Javascript?

0

I'm getting an url item with this code:

function GetQueryString(a)
{
    a = a || window.location.search.substr(1).split('&').concat(window.location.hash.substr(1).split("&"));

    if (typeof a === "string")
        a = a.split("#").join("&").split("&");


    if (!a) return {};

    var b = {};
    for (var i = 0; i < a.length; ++i)
    {

        var p = a[i].split('=');

        if (p.length != 2) continue;

        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }

    return b;
}


var qs = GetQueryString();

  qs["item"]; // Item que eu estou pegando da url ex: 
     https://meusite.com/pagina.php?item=(valor que eu quero pegar)

And after doing this process, I would like that with the value obtained from

variable "qs" concatenate with the new page link that would be redirected after 3 seconds, I am using this code:

setTimeout("document.location = 'https://www.meusite.com/pagina.php?item='" + 'qs['item']',3000);

How can I fix this code?

    
asked by anonymous 04.02.2017 / 23:02

1 answer

1

The function does not return an array. Alias, you are referencing a key, as if it were an associative array. This does not exist in JavaScript.

Return is a JSON object and should be accessed like this: qs.item . The correct code (I put an alert to show the URL) to redirect is:

function GetQueryString(a)
{
    a = a || window.location.search.substr(1).split('&').concat(window.location.hash.substr(1).split("&"));

    if (typeof a === "string")
        a = a.split("#").join("&").split("&");


    if (!a) return {};

    var b = {};
    for (var i = 0; i < a.length; ++i)
    {

        var p = a[i].split('=');

        if (p.length != 2) continue;

        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }

    return b;
}


var qs = GetQueryString();

URL = "https://meusite.com/pagina.php?item=" + qs.item;

alert(URL);

window.location = URL;
    
04.02.2017 / 23:18