Session operation of JS

On the PHP server side, there are concepts of Session and Cookie, and on the JS side, there are corresponding concepts of Session and Cookie. Cookie of JS, you can use cookie.js to complete the corresponding operation.

For details, see the chapter Cookie.js realizes the operation of saving user name and password (4) . Use sessionStorage when session. Storage means storage.

1. Setting the value

 sessionStorage.setItem(key,value);

Set the value of an element, setItem. Similar to server-side setAttribute();

2. Get the value

 var data=sessionStorage.getItem(key);

Similar to the server-side getAttribute(); method

3. Delete value

 sessionStorage.removeItem(key);

Similar to the removeAttribute() method on the server side.

4. Clear all the values ​​in the Session

 sessionStorage.clear();

Similar to session.invalidate() on the server side;

5. Use

Sometimes, the value is placed in JS. When the page is refreshed, the JS will be refreshed again, and the set value will return to the original value. If you don’t want this, you can place this value in the JS session. Use sessionStorage to make the corresponding settings.

thanks!!!

The post Session Operations for JS first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/8921
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment