Jui Test
Jui Test

Reputation: 2409

How to check whether session is null or not in javascript?

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

Answers (6)

Pergin Sheni
Pergin Sheni

Reputation: 401

You must try the following code:

 var IsNull= '@Session["CallType"]'!= null;  

Output: IsNull value will be true or false.

Upvotes: 2

Usman
Usman

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

Steve
Steve

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

Dhananjaya Kuppu
Dhananjaya Kuppu

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

HenryDev
HenryDev

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

user6011162
user6011162

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

Related Questions