Send html page variable to another page using javascript

Asked

Viewed 2,835 times

2

My page receives information from my js, this so my code:

for (var i = 0; i < len; i++) {
    tblText += '<a href="teste.html"><table id="t01" class="table-bordered">';
    tblText += '<tr><th>Protocolo</th><td>' + results.rows.item(i).protocolo + '</td></tr>';
    tblText += '<tr><th>Status</th><td>' + results.rows.item(i).status + '</td></tr>';
    tblText += '</table></a>'
    var meuDepartamento = results.rows.item(i).nomeDepartamento;
    document.getElementById("tabelaSolicitacao").innerHTML = tblText;           
}

I would like that when I click on this table (which is displayed in my html), it sends the results.rows.item(i).protocolo to the new window teste.html, i don’t know how to do this using Javascript.

  • You use JQuery in your project? Or only AngularJS ?

  • 1

    Yes I do a query in my BD, before starting this for

  • Not Query and yes JQuery http://jquery.com/

  • You understood what I said?

  • Got it, I use it

  • Blz, I have to pass a script for session creation via jQuery pear there.

Show 1 more comment

2 answers

1

One solution is to use the local storage (local storage) as described here.

For example:

//Escrever um valor
localStorage.fruta = 'banana';

//Exibir o valor armazenado
console.log(localStorage.fruta);

0

I’m going to spend something that will change your life with JavaScript and a Script in jQuery to create Session with this you can pass your variables and much more via JavaScript by the website.

Script for Creating Sessions:

(function($) {

    $.session = {

        _id: null,

        _cookieCache: undefined,

        _init: function() {
            if (!window.name) {
                window.name = Math.random();
            }
            this._id = window.name;
            this._initCache();

            // See if we've changed protcols

            var matches = (new RegExp(this._generatePrefix() + "=([^;]+);")).exec(document.cookie);
            if (matches && document.location.protocol !== matches[1]) {
                this._clearSession();
                for (var key in this._cookieCache) {
                    try {
                        window.sessionStorage.setItem(key, this._cookieCache[key]);
                    } catch (e) {};
                }
            }

            document.cookie = this._generatePrefix() + "=" + document.location.protocol + ';path=/;expires=' + (new Date((new Date).getTime() + 120000)).toUTCString();

        },

        _generatePrefix: function() {
            return '__session:' + this._id + ':';
        },

        _initCache: function() {
            var cookies = document.cookie.split(';');
            this._cookieCache = {};
            for (var i in cookies) {
                var kv = cookies[i].split('=');
                if ((new RegExp(this._generatePrefix() + '.+')).test(kv[0]) && kv[1]) {
                    this._cookieCache[kv[0].split(':', 3)[2]] = kv[1];
                }
            }
        },

        _setFallback: function(key, value, onceOnly) {
            var cookie = this._generatePrefix() + key + "=" + value + "; path=/";
            if (onceOnly) {
                cookie += "; expires=" + (new Date(Date.now() + 120000)).toUTCString();
            }
            document.cookie = cookie;
            this._cookieCache[key] = value;
            return this;
        },

        _getFallback: function(key) {
            if (!this._cookieCache) {
                this._initCache();
            }
            return this._cookieCache[key];
        },

        _clearFallback: function() {
            for (var i in this._cookieCache) {
                document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
            }
            this._cookieCache = {};
        },

        _deleteFallback: function(key) {
            document.cookie = this._generatePrefix() + key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
            delete this._cookieCache[key];
        },

        get: function(key) {
            return window.sessionStorage.getItem(key) || this._getFallback(key);
        },

        set: function(key, value, onceOnly) {
            try {
                window.sessionStorage.setItem(key, value);
            } catch (e) {}
            this._setFallback(key, value, onceOnly || false);
            return this;
        },

        'delete': function(key) {
            return this.remove(key);
        },

        remove: function(key) {
            try {
                window.sessionStorage.removeItem(key);
            } catch (e) {};
            this._deleteFallback(key);
            return this;
        },

        _clearSession: function() {
            try {
                window.sessionStorage.clear();
            } catch (e) {
                for (var i in window.sessionStorage) {
                    window.sessionStorage.removeItem(i);
                }
            }
        },

        clear: function() {
            this._clearSession();
            this._clearFallback();
            return this;
        }

    };

    $.session._init();

})(jQuery);

How to use:

// DEFINIR SESSÃO
$.session.set('nome_da_session', 'valor_da_session');

// RECUPERAR SESSÃO
$.session.get('nome_da_session');
// OUTPUT "valor_da_session"

// LIMPAR TODAS AS SESSÕES
$.session.clear();

// CRIAR E RECUPERAR SESSÃO AO MESMO TEMPO
$.session.set('nome_da_session', 'valor_da_session').get('nome_da_session');
// OUTPUT > "valor_da_session"

// REMOVER SESSÃO ESPECÍFICA
$.session.remove('nome_da_session');

I hope it helps.

Browser other questions tagged

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