/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Difference between sessionStorage and a local variable?

I read the description of localStorage and sessionStorage
on [url]http://www.w3schools.com/html/html5_webstorage.asp[/url]

I understand the long term use of localStorage,
but I do not see how a sessionStorage values vary much from a global variable in any program.

It appears to me that both variable values are lost when the browser is exited.
Is there any particular advantage to either variable storage technique (sessionStorage vs. global variable)?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Aug 12.2014 — Apples and oranges maybe?

sessionStorage/localStorage is more analogous to cookies than global variables in my mind. sessionStorage is lost on exit, localStorage is not.
Copy linkTweet thisAlerts:
@TcobbAug 12.2014 — Local storage is like a cookie in many ways, except it is not automatically sent to the browser, and it can store much more data than the standard cookie implementation. And unlike a standard js variable it can persist even if the page is reloaded.
Copy linkTweet thisAlerts:
@Kevin2Aug 12.2014 — Local storage is like a cookie in many ways, except it is not automatically sent to the browser [...][/QUOTE]

True that. It must be set/requested via script. Analogous... almost... ?
Copy linkTweet thisAlerts:
@TcobbAug 12.2014 — Whoops-- I really meant to say that it is not automatically sent to the server . . . although I guess what I originally said was true as well.
Copy linkTweet thisAlerts:
@Kevin2Aug 12.2014 — I was smellin' what you were steppin' in Tcobb.

?
Copy linkTweet thisAlerts:
@JMRKERauthorAug 13.2014 — Thank you Kevin2 and Tcobb, but it looks like both of you missed the actual question I was writing about...
I read the description of localStorage and sessionStorage

on http://www.w3schools.com/html/html5_webstorage.asp

I understand the long term use of localStorage,

[COLOR="#FF0000"]but I do not see how a sessionStorage values vary much from a global variable in any program.



It appears to me that both variable values are lost when the browser is exited.[/COLOR]


Is there any particular advantage to either variable storage technique (sessionStorage vs. global variable)?[/QUOTE]


The question is not about localStorage versus sessionStorage.

It is about the use of a global variable in a script (that is lost when program exited)

and sessionStorage variables (which also appear lost when the browser is exited)

In both cases the value of the variable is lost.


So WHY use a sessionStorage variable when a local variable does the same thing?
Copy linkTweet thisAlerts:
@TcobbAug 13.2014 — Data saved in sessionStorage persists even when you change pages so long as you remain within the same domain. So, if you go to http://zippy.com/index.html and enter in some data that is stored in sessionStorage it will still be there if you click on a link in that page that goes to a different page on that same website. If it was just stored in a regular javascript variable it would otherwise be lost when you went to the new page.
Copy linkTweet thisAlerts:
@deathshadowAug 13.2014 — So WHY use a sessionStorage variable when a local variable does the same thing?[/QUOTE]
They don't do the same thing.

If you created a local variable in your scripting... and navigated to another page AT THE SAME DOMAIN, that local variable ceases to exist. You use sessionStorage and that value will still exist. It only goes away when you close the browser or perform an action that ends the session.

Try this:

test1.html
<a href="test2.html">See if it's stored</a>
<script type="text/javascript">
test = 12;
sessionStorage.setItem('test', 88);
document.write(test + '<br>' + sessionStorage.getItem('test'));
</script>


test2.html
<script type="text/javascript">
document.write(test + '<br>' + sessionStorage.getItem('test'));
</script>


Load test 1, when you go to test 2 the 'test' variable will be undefined, but the sessionStorage 'test' will still be 88. That's the difference. Local vars only exist on the page they are created, you do a page-load, they no longer exist.

... so they're like a cookie -- the difference between sessionStorage and a cookie is that it's not sent in the headers of every single file request OR response. A lot of people don't realize that about cookies; they are sent to the server and back from the server with EVERY file. That's why to speed things up people create a subdomain that can't see the cookies for the serving of static files since .js, .css and image / movie files can't see cookies. ONLY place they really need to be sent is from the markup. [i](that they are sent with any non-HTML containing file is IMHO a really stupid part of the implementation since it serves no purpose)[/i]
Copy linkTweet thisAlerts:
@JMRKERauthorAug 13.2014 — Thank you both.

I expected there to be a difference but with the implementation scripts I was using, both lost the information.

Makes more sense now.

?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...