Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I set Session Variable from Javascript

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
0
0
US
I want to set a session variable on the same page a button is pushed.

I know I can pass a value to another page and set the variable there, but can I do if from the same page in javascript?

Thanks,

Tom.
 
In a word, no, but you could have the page post back to itself and then do something like set a session variable.

ASP Design Tips - Post Back Page


Best regards,
J. Paul Schmidt, Freelance ASP Web Consultant
ASP Design Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...
 
You could also use javascript to write a cookie then as Bull said reload the page and set the session variable based on the cookie. (if it must be a session variable)

Of course this would make you dependent on cookies being enabled. Not necessarily a good thing.

Wow JT that almost looked like you knew what you were doing!
 
pixl8r: Of course if cookies weren't enabled, session variables wouldn't be working either, so that would make the whole thing moot :)

An extension and agreement with the No's above:
Session variables are stored on the server. Javascript executes on the client. Remember that the HTTP protocol passes requests to the server for documents and then documents back to the user. Cookies are actually kind of a hack (they are passed in the headers of the documents and requests). So there is no other communication between the client and server. Client-side script has no access to the server and only limited access on the client, although you could use a hidden iframe or popup window to send a request for a page with the variable in te querystring, then close it after it has been passed back. Tis would have the same effect as posting back to the server but wouldn't be a visible to the user.

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
There is a question - if session variables are kept on the server, why would cookies need to be enabled?

Also, if you don't use cookies or session variables, how do you propose to build a web app that will need to pass information from page to page?

 
Sessions are assigned unique ids per user, so you need to have the user keep track of their session id somehow. You could do this with a form variable or a cookie. Else you can't tell what data is associated with what user.

You could make good use of form variables to keep from having to have cookie or session variables on your web application or you could just make a comment on your website about enabling first party cookies.

 
I thought that the session variables were there until they close their browser or the timeout happens (20 minutes, by default I believe).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top