jquery - pass variable from page to page

0

Well, I have this scenario ... my index.html calls another page (home.html) and there are two variables in the index that I want to take home to make my decisions ...

index code.

    $("a[data-type]").on('click',function(){
    var _nome = $(this).attr('id')
        _tipo = $(this).attr('data-type')
        dados = {"nome": _nome, "tipo": _tipo};

    $.ajax({
        type: 'GET',
        url: 'home.html',
        data : dados,
        success:function(data){
            $('#conteudo').html(data);
        }
    });
})

How do I get the name and type properties in the home?

    
asked by anonymous 20.09.2017 / 00:02

1 answer

1

Create cookies to pass the variable to the other page, with this function it is easier to create them:

function setCookie(name, value, duration) {
        var cookie = name + "=" + escape(value);
        document.cookie = cookie;
}

To use this function, simply:

setCookie("nome_cookie", "Valor_cookie");

And this other function serves to handle the cookie:

function getCookie(name) {
    var cookies = document.cookie;
    var prefix = name + "=";
    var begin = cookies.indexOf("; " + prefix);

    if (begin == -1) {

        begin = cookies.indexOf(prefix);

        if (begin != 0) {
            return null;
        }

    } else {
        begin += 2;
    }

    var end = cookies.indexOf(";", begin);

    if (end == -1) {
        end = cookies.length;                        
    }

    return unescape(cookies.substring(begin + prefix.length, end));
}

To use is simple:

var nome_cookie = getCookie("nome_cookie");
    
20.09.2017 / 00:12