954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to save session values in JavaScript

How do you save a session value in JavaScript?

Please help!
Sally

omnis
Newbie Poster
3 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Use cookies. google it with javascript and cookies. There are a ton of functions for it.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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.

nemo5
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Please help anybody.

how to use php session value in javascript?

selvi_sasi
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.


javascript only support cookies. You can set them up to keep track of a user session but they do not support the use of sessions. PHP uses both sessions ans cookies. If you trying to pass in session variables from PHP to Javascript you will have to come up with a good work around since there is no direct way to do this. you can use hidden fields that generate the variable which you can then get in javascript.

wrivera
Junior Poster in Training
53 posts since Jan 2010
Reputation Points: 10
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You