benhsu
benhsu

Reputation: 5546

How do I check in JavaScript if the session is new?

Is there a way in JavaScript to check if the browser session is new? I know in Java you can use Session.isNew(). Is there something equivalent in JavaScript?

Upvotes: 1

Views: 5252

Answers (3)

McKayla
McKayla

Reputation: 6949

In more modern browsers you could use the sessionStorage object.

For example:

if (!sessionStorage.isNewSession)
    // Blah.

sessionStorage.isNewSession = true;

Details on the sessionStorage object:

It retains data as long as the current window is open. If the user refreshes the page, then it will have the same contents as before. If the user closes the browser and then opens your page again, then it will be empty.

But like I said: Modern browsers only.

As far as I know, compatibility is as follows:

Internet Explorer 8+ Firefox 3.5+ Safari 4+ Chrome 1+ Opera 10.6+

Upvotes: 6

AjayR
AjayR

Reputation: 4179

You can create some hidden fields in serverside and can check that value with javascript to check. Alternately you can use Cookies to achieve this with some workaround.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81680

Session is a server-side concept. If the session cookie has been created, there is no way on the client-side to find out if session is new or not.

Upvotes: 3

Related Questions