DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   How to save session values in JavaScript (http://www.daniweb.com/forums/thread19283.html)

omnis Feb 26th, 2005 11:34 am
How to save session values in JavaScript
 
How do you save a session value in JavaScript?

Please help!
Sally

Comatose Feb 26th, 2005 11:54 am
Re: How to save session values in JavaScript
 
Use cookies. google it with javascript and cookies. There are a ton of functions for it.

nemo5 Dec 31st, 2006 8:38 am
Re: How to save session values in JavaScript
 
There are more ways to do it than just cookies - it basicaly depends if you want to store the session between visits that happend at the same time, or if you want to save sessions for later use.

If the later is what you want, go for cookies:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}



If you just want to save the session while the user leaves your pages you could use a hidden frame and store the session value inside a hidden input.


All times are GMT -4. The time now is 11:41 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC