问题
How to read and write session values in javascript
回答1:
If you speak about server-side session values, there's no other way than to somehow communicate with the server (e.g. with an asynchronous callback where you would read/write them with some method).
For simulation of client-side session variables, you might want to have a look here ;)
回答2:
you can't write directly to a server side session var with javascript. Assuming you are not using Ajax here.
回答3:
Session variables reside on the server and as I'm sure you are aware, Javascript is client-side.
回答4:
A bit more context could be useful in understanding what exactly you're trying to achieve. As the session is held on the server, the only way to "instantly" communicate with it would be using AJAX calls.
This being not such a common way of operating maybe explaining your situation may help in giving you alternative, more common ways of doing what you need.
回答5:
HTTP session vars are controlled by the server, JavaScript runs client side, and as such cannot modify the vars directly. Meaning this isn't possible using only Javascript
回答6:
Java is client side scripting language. The client's web-browser does not know anything about session!
All you can do is to use cookies to store values which can be accessed across web-pages.
回答7:
You can't because session variables are actually saved to a file on the server only. The only thing your browser knows about the session is the session id, which is saved into a cookie.
This is the whole point with sessions. The variables are safe because no one on the outside can read them. If you want variables readable by the browser, consider using cookies instead.
来源:https://stackoverflow.com/questions/3626494/session-value-in-javascript