[jquery] How to get value in the session in jQuery

I'm a beginner in jQuery.
I want to set the values in an HTML page and I have to get them in another HTML page.

Here is the piece of code I am trying now:

To set the value in session:

$.session.set(userName, $("#uname").val());

To get the value from session:

$.session.get('userName');

This question is related to jquery html jquery-session

The answer is


Assuming you are using this plugin, you are misusing the .set method. .set must be passed the name of the key as a string as well as the value. I suppose you meant to write:

$.session.set("userName", $("#uname").val());

This sets the userName key in session storage to the value of the input, and allows you to retrieve it using:

$.session.get('userName');

Sessions are stored on the server and are set from server side code, not client side code such as JavaScript.

What you want is a cookie, someone's given a brilliant explanation in this Stack Overflow question here: How do I set/unset cookie with jQuery?

You could potentially use sessions and set/retrieve them with jQuery and AJAX, but it's complete overkill if Cookies will do the trick.