How to create a session using JavaScript?
Here I am supposed to get the session as null(since I logged out the application from the other tab), but I am getting session value as logged in user(probably because no page refresh occurs). Assigning the Session Variable using Javascript: Assigning the ASP.NET Session Variable using Javascript: Accessing ASP.NET Session variable using Javascript: Already Answered here Solution 4: i tested this method and it worked for me.
How to create a session using JavaScript?
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:
You can store and read string information in a 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.
However if it is javascript that creates the session Id. You can create a cookie with javascript, with a function like:
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:
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.
var sId = 's234543245'; writeCookie('sessionId', sId, 3);
var sId = readCookie('sessionId')
You can use Local storage.
local storage is same as session. the data will be removed when you close the browser.
Edit: @Naveen comment below is correct. You can use session storage instead. (https://www.w3schools.com/jsref/prop_win_sessionstorage.asp) Thanks.
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 .
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.
I would recommend you to re-read the definition of sessions.
I assume you are using ASP.NET MVC (C#), if not then this answer is not in your issue.
Is it impossible to assign session values directly through javascript? No, there is a way to do that.
The session » controlid » has created, but what about the value of session? is that persistent or can changeable via javascript?
Let change a little bit more:
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:
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.
Now I move to a new way. Using WebMethod at code behind to do that. Look again your code with a little bit change:
In code-behind page. I create a WebMethod:
[System.Web.Services.WebMethod] public static string CreateSessionViaJavascript(string strTest)
After call the web method to create a session from javascript. The session «controlID» will has value «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.
Source: JavaScript — How to Set values to Session in Javascript
Asp.net — Access Session variables in JavaScript, Access Session variables in JavaScript. Ask Question Asked 8 years, 6 months ago. Modified 4 years, 10 months ago. Viewed 77k times 11 2. I …
Accessing values stored in HttpSession using JavaScript
Here is my Spring MVC Controller code:
session.setAttribute("YourProperty", "arg1");
How can I access an attribute stored in my HttpSession using JavaScript?
I have tried using this code:
var property = ; alert(property);
var property=""; alert(property);
Attribute names should match and since you are adding a string, you should add » around , and the code will alert arg1 .
How to access Session variables and set them in, In code-behind I set Session with some data. Session[«usedData»] = «sample data»; And the question is how can I get the Session value(in my … Code sample
Get Current Session Value in JavaScript?
I have a scenario where I open my web application in a browser but in two separate tabs.
In one tab I signed out from the application and as a result the all session values becomes null. While in the other tab I clicked on an anchor tag in the webapplication. In the anchor tag’s jquery -on click event I have checked the session value. Here I am supposed to get the session as null(since I logged out the application from the other tab), but I am getting session value as logged in user(probably because no page refresh occurs).
My idea is to check the session on jQuery and if the session is null make the application logout,otherwise show a popup page..
Here’s my code for getting the session value
How can I get the current session value in jquery ?
The session is a server side thing, you cannot access it using jQuery. You can write an Http handler (that will share the sessionid if any) and return the value from there using $.ajax .
Another approach is in your chtml
var sessionValue= $("#hdnSession").data('value');
or you may access directly by
jQuery(document).ready(function ($) < var value = '@Request.RequestContext.HttpContext.Session["someKey"]'; >);
Accessing & Assigning the Session Variable using Javascript:
Assigning the ASP.NET Session Variable using Javascript:
Accessing ASP.NET Session variable using Javascript:
i tested this method and it worked for me. hope its useful.
assuming that you have a file named index. php
and when the user logs in, you store the username in a php session; ex. $_SESSION[‘username’];
you can do something like this in you index.php file
now you can access the variable userId in another script block placed in the index.php file, or even in a .js file linked to the index.php
it could be very useful, since it enables us to use the username and other user data stoerd as cookies in ajax queries, for updating database tables and such.
if you dont want to use a javascript variable to contain the info, you can use an input with «type=’hidden’;
ex. in your index.php file, write:
however, this way the user can see the hidden input if they ask the browser to show the source code of the page. in the source view, the hidden input is visible with its content. you may find that undesireble.
Javascript — Access $window.sessionStorage from html, In my case I am working with angularjs and I want to access a saved value in user to show or not some html tags. First it save in some controller the …
Can JavaScript read HTTP Session object?
Is it possible to read the value of a dynamic variable like httpRequest.getSession(«attr_name») from within a JavaScript?
(With Javascript, I assume that you mean client script in the browser.)
No, that is not possible. The contents of the Session object never leaves the server, so client script can’t read Session data directly.
If you want to access it in the browser, you have to read the data out of the Session object and send it along in the response (for example in a hidden field), or provide a web service that reads data from the Session object and returns to the browser.
As I said in my comment, the only way would be some kind of Ajax call and request it from the server. I dont know what backend your using, here’s how I would do it in Asp.net MVC and jQuery.
(If there are minor syntax errors, I apologize — not in front of a compiler)
public class HomeController : Controller < //abstract the session code away, don't let the evil javascript touch //it directly. Ideally this would all be in a seperate logic class or something. public string NameAttribute < get < return Session["attr_name"] as string ?? string.empty; >> [HttpGet] public string GetNameAttribute() < return NameAttribute; >public ActionResult Index() < return View(); >>
Alternatively, you could always write down the values you need into hidden fields, and read them with normal javascript.
How to set/get session values from javascript, you can’t access server session in client side. but if you want to do some changes in client side according to server session value. i will give you …