How to create session in JavaScript
?
如何在JavaScript中创建会话?
I try like this:
我试着像这样:
<script type="text/javascript" >
{
Session["controlID"] ="This is my session";
}
</script>
Why I looking for session?
我为什么要找会议?
I make a request for XML using AJAX. XML response I want to store in session and this session I want to pass to the server page(.asp). I mean to write something like:
我使用AJAX请求XML。我想要在会话中存储XML响应,而这个会话我想传递到服务器页面(.asp)。我的意思是这样写:
<% response.write session("MySession")%>
14 个解决方案
#1
50
You can store and read string information in a cookie.
您可以在cookie中存储和读取字符串信息。
If it is a session id coming from the server, the server can generate this cookie. And when another request is sent to the server the cookie will come too. Without having to do anything in the browser.
如果是来自服务器的会话id,服务器可以生成此cookie。当另一个请求被发送到服务器时,cookie也会出现。不需要在浏览器中做任何事情。
However if it is javascript that creates the session Id. You can create a cookie with javascript, with a function like:
但是,如果是javascript创建了会话Id,您可以使用javascript创建一个cookie,其功能如下:
function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
Then in each page you need this session Id you can read the cookie, with a function like:
然后,在每个页面中都需要这个会话Id,您可以读取cookie,函数如下:
function readCookie(name) {
var i, c, ca, nameEQ = name + "=";
ca = document.cookie.split(';');
for(i=0;i < ca.length;i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return '';
}
The read function work from any page or tab of the same domain that has written it, either if the cookie was created from the page in javascript or from the server.
如果cookie是用javascript从页面创建的,或者是从服务器创建的,那么read函数可以从编写它的域的任何页面或选项卡中工作。
To store the id:
存储id:
var sId = 's234543245';
writeCookie('sessionId', sId, 3);
To read the id:
阅读id:
var sId = readCookie('sessionId')
#2
11
I assume you are using ASP.NET MVC (C#), if not then this answer is not in your issue.
我想你是在使用ASP。NET MVC (c#),如果不是,那么这个答案不在您的问题中。
Is it impossible to assign session values directly through javascript? No, there is a way to do that.
是否不可能直接通过javascript分配会话值?不,有个办法。
Look again your code:
看一遍你的代码:
<script type="text/javascript" >
{
Session["controlID"] ="This is my session";
}
</script>
A little bit change:
一点点的改变:
<script type="text/javascript" >
{
<%Session["controlID"] = "This is my session";%>
}
</script>
The session "controlID" has created, but what about the value of session? is that persistent or can changeable via javascript?
会话“controlID”已经创建,但是会话的值又如何呢?这是持久的还是可以通过javascript改变的?
Let change a little bit more:
让我们再改变一点:
<script type="text/javascript" >
{
var strTest = "This is my session";
<%Session["controlID"] = "'+ strTest +'";%>
}
</script>
The session is created, but the value inside of session will be "'+ strTest +'" but not "This is my session". If you try to write variable directly into server code like:
会话是被创建的,但是会话内部的值将是“‘+ strTest +’”,而不是“这是我的会话”。如果您试图将变量直接写入服务器代码,如:
<%Session["controlID"] = strTest;%>
Then an error will occur in you page "There is no parameter strTest in current context...". Until now it is still seem impossible to assign session values directly through javascript.
然后在您的页面“当前上下文中没有参数strTest…”中将出现错误。到目前为止,似乎仍然不可能通过javascript直接分配会话值。
Now I move to a new way. Using WebMethod at code behind to do that. Look again your code with a little bit change:
现在我换了一种新的方式。在代码后面使用WebMethod来实现这一点。再看看你的代码有一点变化:
<script type="text/javascript" >
{
var strTest = "This is my session";
PageMethods.CreateSessionViaJavascript(strTest);
}
</script>
In code-behind page. I create a WebMethod:
在后台代码页。我创建了一个WebMethod:
[System.Web.Services.WebMethod]
public static string CreateSessionViaJavascript(string strTest)
{
Page objp = new Page();
objp.Session["controlID"] = strTest;
return strTest;
}
After call the web method to create a session from javascript. The session "controlID" will has value "This is my session".
调用web方法后,从javascript创建会话。会话“controlID”将具有“This is my session”的值。
If you use the way I have explained, then please add this block of code inside form tag of your aspx page. The code help to enable page methods.
如果您使用我已经解释过的方式,那么请在您的aspx页面的表单标记中添加此代码块。代码有助于启用页面方法。
<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
Source: JavaScript - How to Set values to Session in Javascript
源码:JavaScript—如何在JavaScript中设置会话值
Happy codding, Tri
快乐的鳕鱼,三
#3
9
I think you misunderstood the concept of session, session is a server side per-user-data-store which allows you to save user data on the server side.
我认为您误解了会话的概念,会话是一个服务器端每个用户数据存储,它允许您在服务器端保存用户数据。
thus, you have 2 options, resort to use cookies, which will give the illusion of session(but not quite the same), you can access cookies very simply by document.cookie .
因此,您有两个选项,使用cookie,这会给您带来会话错觉(但不是完全相同),您可以通过文档非常简单地访问cookie。饼干。
but, if you want your server be aware of the session, you need to use some sort of server request probably the best way is to use AJAX to do this.
但是,如果您想让您的服务器知道会话,您需要使用某种类型的服务器请求,可能最好的方法是使用AJAX实现这一点。
I would recommend you to re-read the definition of sessions.
我建议你重新阅读会议的定义。
#4
8
You can use Local storage.
您可以使用本地存储。
local storage is same as session. the data will be removed when you close the browser.
本地存储与会话相同。当您关闭浏览器时,数据将被删除。
<script>
localStorage.setItem('lastname','Smith');
alert(localStorage.getItem('lastname'));
</script>
#5
1
function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
#6
1
You can try jstorage javascript plugin, it is an elegant way to maintain sessions check this http://www.jstorage.info/
您可以尝试jstorage javascript插件,这是维护会话的一种优雅方式,请查看http://www.jstorage.info/
include the jStorage.js script into your html
包括jStorage。将js脚本放入html中
<script src="jStorage.js"></script>
Then in your javascript place the sessiontoken into the a key like this
然后在javascript中将sessiontoken放入这样的键中。
$.jStorage.set("YOUR_KEY",session_id);
Where "YOUR_KEY" is the key using which you can access you session_id , like this:
其中“YOUR_KEY”是可以访问session_id的键,如下所示:
var id = $.jStorage.get("YOUR_KEY");
#7
1
You can use sessionStorage it is similar to localStorage but sessionStorage gets clear when the page session ends while localStorage has no expiration set.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
您可以使用sessionStorage它与localStorage相似,但是当页面会话结束时,sessionStorage会变得清晰,而localStorage没有过期设置
#8
0
You can use the name
attr:
你可以使用名称attr:
<script type="text/javascript" >
{
window.name ="This is my session";
}
</script>
You still have to develop for yourself the format to use, or use a wrapper from an already existing library (mootools, Dojo etc).
You can also use cookies, but they are more heavy on performance, as they go back and forth from the client to the server, and are specific to one domain.
您仍然需要自己开发要使用的格式,或者使用现有库(mootools、Dojo等)的包装器。您也可以使用cookie,但是它们对性能的影响更大,因为它们从客户端到服务器,并且特定于一个域。
#9
0
You can store and read string information in a cookie.
您可以在cookie中存储和读取字符串信息。
If it is a session id coming from the server, the server can generate this cookie. And when another request is sent to the server the cookie will come too. Without having to do anything in the browser.
如果是来自服务器的会话id,服务器可以生成此cookie。当另一个请求被发送到服务器时,cookie也会出现。不需要在浏览器中做任何事情。
However if it is javascript that creates the session Id. You can create a cookie with javascript, with a function like:
但是,如果是javascript创建了会话Id,您可以使用javascript创建一个cookie,其功能如下:
The read function work from any page or tab of the same domain that has written it, either if the cookie was created from the page in javascript or from the server.
如果cookie是用javascript从页面创建的,或者是从服务器创建的,那么read函数可以从编写它的域的任何页面或选项卡中工作。
#10
0
If you create a cookie and do not specify an expiration date, it will create a session cookie which will expire at the end of the session.
如果您创建了一个cookie并没有指定过期日期,它将创建一个会话cookie,该cookie将在会话结束时过期。
See https://*.com/a/532660/1901857 for more information.
更多信息请参见https://*.com/a/532660/1901857。
#11
0
localStorage and jstorage are your best bets. localStorage is built in and jstorage offers lots of functionalites
localStorage和jstorage是您最好的赌注。localStorage是内置的,jstorage提供了大量的功能
See localStorage browser support on caniuse.
请参阅caniuse上的localStorage浏览器支持。
#12
0
<script type="text/javascript">
function myfunction()
{
var IDSes= "10200";
'<%Session["IDDiv"] = "' + $(this).attr('id') + '"; %>'
'<%Session["IDSes"] = "' + IDSes+ '"; %>';
alert('<%=Session["IDSes"] %>');
}
</script>
链接
#13
0
Here is what I did and it worked, created a session in PHP and used xmlhhtprequest to check if session is set whenever an HTML page loads and it worked for Cordova.
下面是我所做的,它工作了,在PHP中创建了一个会话,并使用xmlhhtprequest来检查会话是否在HTML页面加载时设置,这对Cordova很有效。
#14
-1
Use HTML5 Local Storage. you can store and use the data anytime you please.
使用HTML5本地存储。您可以随时存储和使用这些数据。
<script>
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
var data = localStorage.getItem("lastname");
</script>
#1
50
You can store and read string information in a cookie.
您可以在cookie中存储和读取字符串信息。
If it is a session id coming from the server, the server can generate this cookie. And when another request is sent to the server the cookie will come too. Without having to do anything in the browser.
如果是来自服务器的会话id,服务器可以生成此cookie。当另一个请求被发送到服务器时,cookie也会出现。不需要在浏览器中做任何事情。
However if it is javascript that creates the session Id. You can create a cookie with javascript, with a function like:
但是,如果是javascript创建了会话Id,您可以使用javascript创建一个cookie,其功能如下:
function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
Then in each page you need this session Id you can read the cookie, with a function like:
然后,在每个页面中都需要这个会话Id,您可以读取cookie,函数如下:
function readCookie(name) {
var i, c, ca, nameEQ = name + "=";
ca = document.cookie.split(';');
for(i=0;i < ca.length;i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return '';
}
The read function work from any page or tab of the same domain that has written it, either if the cookie was created from the page in javascript or from the server.
如果cookie是用javascript从页面创建的,或者是从服务器创建的,那么read函数可以从编写它的域的任何页面或选项卡中工作。
To store the id:
存储id:
var sId = 's234543245';
writeCookie('sessionId', sId, 3);
To read the id:
阅读id:
var sId = readCookie('sessionId')
#2
11
I assume you are using ASP.NET MVC (C#), if not then this answer is not in your issue.
我想你是在使用ASP。NET MVC (c#),如果不是,那么这个答案不在您的问题中。
Is it impossible to assign session values directly through javascript? No, there is a way to do that.
是否不可能直接通过javascript分配会话值?不,有个办法。
Look again your code:
看一遍你的代码:
<script type="text/javascript" >
{
Session["controlID"] ="This is my session";
}
</script>
A little bit change:
一点点的改变:
<script type="text/javascript" >
{
<%Session["controlID"] = "This is my session";%>
}
</script>
The session "controlID" has created, but what about the value of session? is that persistent or can changeable via javascript?
会话“controlID”已经创建,但是会话的值又如何呢?这是持久的还是可以通过javascript改变的?
Let change a little bit more:
让我们再改变一点:
<script type="text/javascript" >
{
var strTest = "This is my session";
<%Session["controlID"] = "'+ strTest +'";%>
}
</script>
The session is created, but the value inside of session will be "'+ strTest +'" but not "This is my session". If you try to write variable directly into server code like:
会话是被创建的,但是会话内部的值将是“‘+ strTest +’”,而不是“这是我的会话”。如果您试图将变量直接写入服务器代码,如:
<%Session["controlID"] = strTest;%>
Then an error will occur in you page "There is no parameter strTest in current context...". Until now it is still seem impossible to assign session values directly through javascript.
然后在您的页面“当前上下文中没有参数strTest…”中将出现错误。到目前为止,似乎仍然不可能通过javascript直接分配会话值。
Now I move to a new way. Using WebMethod at code behind to do that. Look again your code with a little bit change:
现在我换了一种新的方式。在代码后面使用WebMethod来实现这一点。再看看你的代码有一点变化:
<script type="text/javascript" >
{
var strTest = "This is my session";
PageMethods.CreateSessionViaJavascript(strTest);
}
</script>
In code-behind page. I create a WebMethod:
在后台代码页。我创建了一个WebMethod:
[System.Web.Services.WebMethod]
public static string CreateSessionViaJavascript(string strTest)
{
Page objp = new Page();
objp.Session["controlID"] = strTest;
return strTest;
}
After call the web method to create a session from javascript. The session "controlID" will has value "This is my session".
调用web方法后,从javascript创建会话。会话“controlID”将具有“This is my session”的值。
If you use the way I have explained, then please add this block of code inside form tag of your aspx page. The code help to enable page methods.
如果您使用我已经解释过的方式,那么请在您的aspx页面的表单标记中添加此代码块。代码有助于启用页面方法。
<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
Source: JavaScript - How to Set values to Session in Javascript
源码:JavaScript—如何在JavaScript中设置会话值
Happy codding, Tri
快乐的鳕鱼,三
#3
9
I think you misunderstood the concept of session, session is a server side per-user-data-store which allows you to save user data on the server side.
我认为您误解了会话的概念,会话是一个服务器端每个用户数据存储,它允许您在服务器端保存用户数据。
thus, you have 2 options, resort to use cookies, which will give the illusion of session(but not quite the same), you can access cookies very simply by document.cookie .
因此,您有两个选项,使用cookie,这会给您带来会话错觉(但不是完全相同),您可以通过文档非常简单地访问cookie。饼干。
but, if you want your server be aware of the session, you need to use some sort of server request probably the best way is to use AJAX to do this.
但是,如果您想让您的服务器知道会话,您需要使用某种类型的服务器请求,可能最好的方法是使用AJAX实现这一点。
I would recommend you to re-read the definition of sessions.
我建议你重新阅读会议的定义。
#4
8
You can use Local storage.
您可以使用本地存储。
local storage is same as session. the data will be removed when you close the browser.
本地存储与会话相同。当您关闭浏览器时,数据将被删除。
<script>
localStorage.setItem('lastname','Smith');
alert(localStorage.getItem('lastname'));
</script>
#5
1
function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
#6
1
You can try jstorage javascript plugin, it is an elegant way to maintain sessions check this http://www.jstorage.info/
您可以尝试jstorage javascript插件,这是维护会话的一种优雅方式,请查看http://www.jstorage.info/
include the jStorage.js script into your html
包括jStorage。将js脚本放入html中
<script src="jStorage.js"></script>
Then in your javascript place the sessiontoken into the a key like this
然后在javascript中将sessiontoken放入这样的键中。
$.jStorage.set("YOUR_KEY",session_id);
Where "YOUR_KEY" is the key using which you can access you session_id , like this:
其中“YOUR_KEY”是可以访问session_id的键,如下所示:
var id = $.jStorage.get("YOUR_KEY");
#7
1
You can use sessionStorage it is similar to localStorage but sessionStorage gets clear when the page session ends while localStorage has no expiration set.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
您可以使用sessionStorage它与localStorage相似,但是当页面会话结束时,sessionStorage会变得清晰,而localStorage没有过期设置
#8
0
You can use the name
attr:
你可以使用名称attr:
<script type="text/javascript" >
{
window.name ="This is my session";
}
</script>
You still have to develop for yourself the format to use, or use a wrapper from an already existing library (mootools, Dojo etc).
You can also use cookies, but they are more heavy on performance, as they go back and forth from the client to the server, and are specific to one domain.
您仍然需要自己开发要使用的格式,或者使用现有库(mootools、Dojo等)的包装器。您也可以使用cookie,但是它们对性能的影响更大,因为它们从客户端到服务器,并且特定于一个域。
#9
0
You can store and read string information in a cookie.
您可以在cookie中存储和读取字符串信息。
If it is a session id coming from the server, the server can generate this cookie. And when another request is sent to the server the cookie will come too. Without having to do anything in the browser.
如果是来自服务器的会话id,服务器可以生成此cookie。当另一个请求被发送到服务器时,cookie也会出现。不需要在浏览器中做任何事情。
However if it is javascript that creates the session Id. You can create a cookie with javascript, with a function like:
但是,如果是javascript创建了会话Id,您可以使用javascript创建一个cookie,其功能如下:
The read function work from any page or tab of the same domain that has written it, either if the cookie was created from the page in javascript or from the server.
如果cookie是用javascript从页面创建的,或者是从服务器创建的,那么read函数可以从编写它的域的任何页面或选项卡中工作。
#10
0
If you create a cookie and do not specify an expiration date, it will create a session cookie which will expire at the end of the session.
如果您创建了一个cookie并没有指定过期日期,它将创建一个会话cookie,该cookie将在会话结束时过期。
See https://*.com/a/532660/1901857 for more information.
更多信息请参见https://*.com/a/532660/1901857。
#11
0
localStorage and jstorage are your best bets. localStorage is built in and jstorage offers lots of functionalites
localStorage和jstorage是您最好的赌注。localStorage是内置的,jstorage提供了大量的功能
See localStorage browser support on caniuse.
请参阅caniuse上的localStorage浏览器支持。
#12
0
<script type="text/javascript">
function myfunction()
{
var IDSes= "10200";
'<%Session["IDDiv"] = "' + $(this).attr('id') + '"; %>'
'<%Session["IDSes"] = "' + IDSes+ '"; %>';
alert('<%=Session["IDSes"] %>');
}
</script>
链接
#13
0
Here is what I did and it worked, created a session in PHP and used xmlhhtprequest to check if session is set whenever an HTML page loads and it worked for Cordova.
下面是我所做的,它工作了,在PHP中创建了一个会话,并使用xmlhhtprequest来检查会话是否在HTML页面加载时设置,这对Cordova很有效。
#14
-1
Use HTML5 Local Storage. you can store and use the data anytime you please.
使用HTML5本地存储。您可以随时存储和使用这些数据。
<script>
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
var data = localStorage.getItem("lastname");
</script>