javascript 如何在html5中维护登录会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26687429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:19:00  来源:igfitidea点击:

How to maintain login session in html5

javascripthtmlsession

提问by Ashish S

I am a beginner to html5.

我是 html5 的初学者。

I am developing a webapp which demands me to maintain user login session.

我正在开发一个 webapp,它要求我维护用户登录会话。

Is that possible to maintain session using HTML5 and javascript only ?

是否可以仅使用 HTML5 和 javascript 来维护会话?

If it is , an example demo or link will be a great help.

如果是,示例演示或链接将有很大帮助。

Thanks in advance.

提前致谢。

采纳答案by its_notHyman

Maybe using cookies could provide the desired result: http://www.w3schools.com/js/js_cookies.asp

也许使用 cookie 可以提供所需的结果:http: //www.w3schools.com/js/js_cookies.asp

An example of saving cookies is

保存 cookie 的一个例子是

document.cookie="session=john doe";

Then, to read the cookie:

然后,读取cookie:

var session = document.cookie;

If you are looking for something that lasts longer, take a look at DOM storage, however, I can't help you with that because it is beyond my scope of knowledge. Either way, the link is https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

如果您正在寻找持续时间更长的东西,请查看 DOM 存储,但是,我无法帮助您,因为它超出了我的知识范围。无论哪种方式,链接都是https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

回答by Avinash Babu

Seems like you are looking for sessionStorage.

似乎您正在寻找sessionStorage.

A simple example from reference.

来自参考的一个简单示例。

// Save data to the current session's store
sessionStorage.setItem("username", "John");
// Access some stored data
alert( "username = " + sessionStorage.getItem("username"));