Reputation: 2409
How to check whether session is null or not in javascript?It is right way?
if ('<%=Session["Time"] == null%>')
{
alert('null session');
}
Upvotes: 3
Views: 24638
Reputation: 401
You must try the following code:
var IsNull= '@Session["CallType"]'!= null;
Output: IsNull value will be true or false.
Upvotes: 2
Reputation: 2577
Here is the procedure to check whether session is null or not in Javascript. we have to do something like this.
var sessionValue = '<%=Session["Time"] != null%>';
So here is the point, if Session["Time"] is not null then it will return 'True' which is a string in this case. so then we can do our further processing in this manner.
if (sessionValue == 'True')
{
alert('session is not null');
}
else
{
alert('session is null');
}
Upvotes: 0
Reputation: 634
Something like this should do it:
var isNullSession = <%=(Session["time"]==null).ToString().ToLower()%>;
if (isNullSession) {
alert('A null session variable I be');
}
This wouldn't mean the "Session" was null, just the session variable "time".
Steve
Upvotes: 0
Reputation: 1322
if ('<%=Session["Time"] == null%>') will either evaluate to if ('True') or if ('False') which is equal to true in javascript: You may try as below:
if (<%=((Session["Time"] == null) ? 1 : 0))%>)
{
alert('null session');
}
Upvotes: 0
Reputation: 4983
Here's a solution that will test every 500 milliseconds if the user session has expired.
function CheckSession() {
var session = '<%=Session["username"] != null%>';
if (session == false) {
alert("Your Session has expired");
window.location = "login.aspx";
}
}
setInterval(CheckSession(),500);
Upvotes: 2
Reputation:
I have used this code to check the Session.
public string TransactionID
{
get {
var data = ((Hashtable)(Session["SessionData"]));
if (data != null)
return data["TransactionID "];
else
return null;
}
}
Upvotes: 0