Use Session Storage using Jquery

    Nov 20, 2019       by Pankaj Kumar

While working with any language, We commonly need to share the data throughout the application. localStorage and sessionStorage is one of the most common ways to save user-related data at the browser which can be used throughout the application.

In this article, We will see how to use sessionStorage using Jquery. One other similar way to save data is localStorage. The sessionStorage object stores data only for a session. It means that the data stored in the sessionStorage will destroy when browser get closed but with localStorage data will not be deleted when browser gets closed. Click here to read more about the Difference between localStorage and sessionStorage

 

Load the libraries

Load the main script jquery.session.js after loading the jQuery library.

<script src="/path/to/cdn/.../jquery.min.js"></script>

<script src="/path/to/...../jquery.session.js"></script>

 

Save data to sessionStorage

The value will be added using “set”.

$.session.set(‘kae_name’, ‘key _value’);

 

Get saved value from sessionStorage

Using “get” we can fetch specific session variable.

$.session.get(‘key_name’);

 

Remove specific data from sessionStorage

$.session.remove('key_name');

 

Clear all saved values from sessionStorage

$.session.clear();

 

With the help of jQuery and session library above operations can be used. These are very easy and quick to implement at any kind of application. 

 

Thanks for reading the article!


Find other similar Articles here: