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.isNe开发者_JAVA百科w()
. Is there something equivalent in JavaScript?
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+
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.
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.
精彩评论