jquery - variable pass from one page to another

Asked

Viewed 848 times

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 I can get the name and type properties there at home?

  • Do you want to get these values via Javascript? Because if it is, I believe it is not possible, because the page home.html will not be loaded in the browser and thus the JS of this file does not run.

  • I’m actually imbuing this page inside a div in my index

1 answer

1


Create cookies to move the variable to the other page, with this function becomes easier to create:

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

To use this function is enough:

setCookie("nome_cookie", "Valor_cookie");

And this other function serves to catch 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");
  • thanks Wictor... solved!!!!

Browser other questions tagged

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