Jump to content

sessionStorage vs Global Variables


Gilbert

Recommended Posts

Hi,  I tried a couple of searches and couldn't find a post that compared these 2 forms of storage.   I was using sessionStorage in my script to remember information from a Db, but as I was reviewing variable properties and scope, I said to myself Why should I involve sessionStorage when I can just assign the info to a global variable which is accessable to the whole website and goes away when you leave the website, just like a session variable.   Is this correct?   Are the global variables not as reliable or something?   What are the differences, similarities; and what can you recommend to me about this.  Thanx a million!

 

Link to comment
Share on other sites

The difference between a regular Javascript variable and sessionStorage is that the global variable disappears as soon as the user leaves the page. sessionStorage remains as long as the user hasn't closed their browser, even if they leave your site and return to it in a few minutes.

Link to comment
Share on other sites

I think I understand your point - The following excerpts are from thew3  JS tutorial under jsScope, but I think I read 'web page' as 'web site' in the first one, which would support your answer.   But down further are two other excerpts which are confusing me.   It says that HTML global variables belong to the window object and remain until the window is closed.  The last line of the 3rd quote is what I was basing my hypothesis on.   Please set me straight if I'm reading this wrong.  Thanks for all your help.   In the end, I have nothing against sessionStorage - I'm just trying to understand the scope thing (actually I use Listerine) (chuckle, chuckle)

Global JavaScript Variables

A variable declared outside a function, becomes GLOBAL.

A global variable has global scope: All scripts and functions on a web page can access it

 

Global Variables in HTML

With JavaScript, the global scope is the complete JavaScript environment.

In HTML, the global scope is the window object. All global variables belong to the window object.

 

The Lifetime of JavaScript Variables

The lifetime of a JavaScript variable starts when it is declared.

Local variables are deleted when the function is completed.

In a web browser, global variables are deleted when you close the browser window (or tab), but remain available to new pages loaded into the same window.

 

 

Edited by Gilbert
Link to comment
Share on other sites

That's not worded very well.  It includes when a page is refreshed, it doesn't matter if the window or tab is still open.  Whenever the browser loads a page it creates a new window object, so every time a page it loaded it starts over with a new window object.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...