Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

$(".a".click(function(){    var session=var data = '@Session["UserName"]';    if(session!=null){     //Show popup    }    else{     //Show loginpage    }  })) 

How can I get the current session value in jQuery?

like image 980
SparAby Avatar asked May 08 '14 10:05

SparAby


People also ask

Can I access session variables in JavaScript?

You can't access Session directly in JavaScript.

How do you display session variables in HTML?

String a = Login1. UserName; Session["user"] = a; Response. Redirect("~/Home. aspx");

What is session in JS?

Introduction to JavaScript sessionStorageThe sessionStorage object stores data only for a session. It means that the data stored in the sessionStorage will be deleted when the browser is closed. A page session lasts as long as the web browser is open and survives over the page refresh.


1 Answers

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.

like image 156
Stefano Altieri Avatar answered Sep 17 '22 12:09

Stefano Altieri