944,083 Members | Top Members by Rank

Ad:
Feb 26th, 2005
0

How to save session values in JavaScript

Expand Post »
How do you save a session value in JavaScript?

Please help!
Sally
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
omnis is offline Offline
3 posts
since Feb 2005
Feb 26th, 2005
0

Re: How to save session values in JavaScript

Use cookies. google it with javascript and cookies. There are a ton of functions for it.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Dec 31st, 2006
0

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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function createCookie(name,value,days) {
  2. if (days) {
  3. var date = new Date();
  4. date.setTime(date.getTime()+(days*24*60*60*1000));
  5. var expires = "; expires="+date.toGMTString();
  6. }
  7. else var expires = "";
  8. document.cookie = name+"="+value+expires+"; path=/";
  9. }
  10.  
  11.  
  12. function readCookie(name) {
  13. var nameEQ = name + "=";
  14. var ca = document.cookie.split(';');
  15. for(var i=0;i < ca.length;i++) {
  16. var c = ca[i];
  17. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  18. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  19. }
  20. return null;
  21. }
  22.  
  23. function eraseCookie(name) {
  24. createCookie(name,"",-1);
  25. }



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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nemo5 is offline Offline
6 posts
since Dec 2006
Jan 11th, 2010
0
Re: How to save session values in JavaScript
Please help anybody.

how to use php session value in javascript?
Last edited by selvi_sasi; Jan 11th, 2010 at 7:12 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
selvi_sasi is offline Offline
1 posts
since Jan 2010
Jan 13th, 2010
0
Re: How to save session values in JavaScript
Click to Expand / Collapse  Quote originally posted by nemo5 ...
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function createCookie(name,value,days) {
  2. if (days) {
  3. var date = new Date();
  4. date.setTime(date.getTime()+(days*24*60*60*1000));
  5. var expires = "; expires="+date.toGMTString();
  6. }
  7. else var expires = "";
  8. document.cookie = name+"="+value+expires+"; path=/";
  9. }
  10.  
  11.  
  12. function readCookie(name) {
  13. var nameEQ = name + "=";
  14. var ca = document.cookie.split(';');
  15. for(var i=0;i < ca.length;i++) {
  16. var c = ca[i];
  17. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  18. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  19. }
  20. return null;
  21. }
  22.  
  23. function eraseCookie(name) {
  24. createCookie(name,"",-1);
  25. }



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.
Reputation Points: 10
Solved Threads: 10
Junior Poster in Training
wrivera is offline Offline
53 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Dynamic DIV height with PHP
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: ajax changing url





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC