简体   繁体   中英

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');
}

You must try the following code:

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

Output: IsNull value will be true or false.

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;
   }
}

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');
}

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

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');
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM