Skip to content Skip to sidebar Skip to footer

Checking Session Timeout In Javascript

Is there any way to check by javaScript whether a session is timeout? If it is possible in javascript, can any one help me with the code snippet?

Solution 1:

if you mean a server session, then no - it can't be done properly.

You could initialise a timer to the same value as the server session - if the timer executes then the user has been on the same page long enough for the server session to expire, but that doesn't actually check if the session is expired, it merely tries to guess.

Alternatively, you could do an asynchronous call to a URL on the server, but on most servers this will automatically extend the session - if the call succeeds, then the session is active...

Solution 2:

If JavaScript pings the server, it will refresh the session. So it can not just call and check. It is normally done with a timer on the client that runs for a length of time. If the timer fires you know the session may be expired.

The timer would fail if the user is using multiple windows. In that case you would need to run something with localstorage/cookies to say when the last time the server was hit.

Solution 3:

Sessions are dependent on the framework you use it at the backend (.net, php etc), javascript is a horizontal stack which is not tightly coupled with any of these server side frameworks.

There is no way with just javascript alone to determine whether a session is active or not.

Post a Comment for "Checking Session Timeout In Javascript"