2

JS的Session操作

 1 year ago
source link: https://blog.p2hp.com/archives/8921
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在PHP服务器端,有Session和Cookie的概念,在JS端,也有相对应的Session和Cookie的概念。 JS的Cookie,可以利用cookie.js来完成相应的操作。
具体可以看 Cookie.js实现保存用户名和密码操作(四) 一章。 session时使用sessionStorage . Storage表示存储的意思。

一. 设置值

sessionStorage.setItem(key,value);

设置元素的值, setItem. 类似于服务器端的setAttribute();

二.得到值

var data=sessionStorage.getItem(key);

类似于服务器端的getAttribute();方法

三. 删除值

sessionStorage.removeItem(key);

类似于服务器端的removeAttribute() 方法.

四.清除Session中所有的值

sessionStorage.clear();

类似于服务器端的 session.invalidate();

有的时候,将值放置在JS中,当刷新页面的时候,会重新刷新一下JS,那么设置的值就又回到了原始的值。 如果不想这样,可以将这个值放置在JS的session中。 用sessionStorage来进行相应的设置。

谢谢!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK