javascript 使用javascript设置会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18076013/
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-27 10:40:02  来源:igfitidea点击:

Setting session variable using javascript

javascriptsession-variables

提问by Sam

I am getting name and email id of a user after he logs in via facebook to my website.. I want to add those variables in session on login form itself using javascript; I tried following:

在用户通过 Facebook 登录到我的网站后,我获得了用户的姓名和电子邮件 ID。我想使用 javascript 在登录表单本身的会话中添加这些变量;我尝试了以下操作:

FB.api('/me', function(me) {
        if (me.name) {
            document.getElementById('auth-displayname').innerHTML = me.name; <% Session["fbName"] = me.name; %>
        }
}

it gives error like me (in this line: <%Session["fbName"] = me.name; %>) does not exist in the current context etc.. my div "auth-displayname" is getting that value but I'm having problem with session variable

它给出了像我这样的错误(在这一行中:<%Session["fbName"] = me.name; %>) 在当前上下文中不存在等。我的 div“auth-displayname”正在获取该值,但我'会话变量有问题

How can I do this

我怎样才能做到这一点

回答by User

You can use

您可以使用

sessionStorage.SessionName = "SessionData",

sessionStorage.SessionName = "SessionData",

sessionStorage.getItem("SessionName")and

sessionStorage.getItem("SessionName")

sessionStorage.setItem("SessionName","SessionData");

sessionStorage.setItem("SessionName","SessionData");

See the supported browsers on http://caniuse.com/namevalue-storage

http://caniuse.com/namevalue-storage查看支持的浏览器

回答by Halcyon

A session is stored server side, you can't modify it with JavaScript. Sessions may contain sensitive data.

会话存储在服务器端,您不能用 JavaScript 修改它。会话可能包含敏感数据。

You can modify cookiesusing document.cookie.

您可以修改的cookie使用document.cookie

You can easily find many examples how to modify cookies.

您可以轻松找到许多如何修改 cookie 的示例。

回答by Thanassis_K

You could better use the localStorageof the web browser.

您最好使用Web 浏览器的localStorage

You can find a reference here

你可以在这里找到参考

回答by Art3mix

It is very important to understand both sessionStorageand localStorageas they both have different uses:

了解两者非常重要sessionStoragelocalStorage因为它们都有不同的用途:

From MDN:

来自MDN

All of your web storage data is contained within two object-like structures inside the browser: sessionStorage and localStorage. The first one persists data for as long as the browser is open (the data is lost when the browser is closed) and the second one persists data even after the browser is closed and then opened again.

您的所有 Web 存储数据都包含在浏览器内的两个类对象结构中:sessionStorage 和 localStorage。只要浏览器打开,第一个数据就会持久化(关闭浏览器时数据会丢失),第二个数据即使在浏览器关闭然后再次打开后也能持久化。

sessionStorage- Saves data until the browser is closed, the data is deleted when the tab/browser is closed.

sessionStorage- 保存数据直到浏览器关闭,当标签页/浏览器关闭时数据被删除。

localStorage- Saves data "forever" even after the browser is closed BUTyou shouldn't count on the data you store to be there later, the data might get deleted by the browser at any time because of pretty much anything, or deleted by the user, best practice would be to validate that the data is there first, and continue the rest if it is there. (or set it up again if its not there)

localStorage- 即使在浏览器关闭后也“永远”保存数据,您不应该指望您存储的数据稍后会在那里,因为几乎任何事情,数据都可能随时被浏览器删除,或者被用户删除,最佳做法是首先验证数据是否存在,如果存在则继续其余的数据。(或者如果不存在则重新设置)

To understand more, read here: localStorage| sessionStorage

要了解更多信息,请阅读此处:localStorage| 会话存储