I want to redirect the user to the "Login page" from anywhere in the application once the session is expired.
How can I accomplish that using JavaScript?
A typical implementation of a session requires two things:
- A cookie in the browser
- A server side record of the session associated with the number stored in the cookie
These cookies are usually set up as session cookies. They expire when the browser closes. Thus you don't need to worry about testing them to achieve this.
To find out if the session is still valid on the server, you need to (periodically) make an HTTP request to the server (you can do this with Ajax) and have the server respond with information about the life of the session.
A simple solution would be to return true or false, and then redirect (by setting location.href) if it is false.
A more efficient solution would be to return the remaining time to live for the session, and use that information to determine when next to check (and redirect if it is 0). A more
It is not necessary to do a periodic check of session is expired, the best thing you can do is you can check when event is fired on the front end, Say like before submit / before click etc. Server time is a key resources we need to manage to give better app performance.
来源:https://stackoverflow.com/questions/6042384/how-to-check-if-the-session-expires-using-javascript