www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] Difference between sessionStorage and a local variable?

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    resolved [RESOLVED] Difference between sessionStorage and a local variable?

    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,
    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)?

  2. #2
    Join Date
    Oct 2013
    Posts
    486
    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    495
    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.

  4. #4
    Join Date
    Oct 2013
    Posts
    486
    Quote Originally Posted by Tcobb View Post
    Local storage is like a cookie in many ways, except it is not automatically sent to the browser [...]
    True that. It must be set/requested via script. Analogous... almost...
    Last edited by Kevin2; 08-12-2014 at 03:35 PM.

  5. #5
    Join Date
    Mar 2009
    Posts
    495
    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.

  6. #6
    Join Date
    Oct 2013
    Posts
    486
    I was smellin' what you were steppin' in Tcobb.


  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question

    Thank you Kevin2 and Tcobb, but it looks like both of you missed the actual question I was writing about...
    Quote Originally Posted by JMRKER View Post
    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,
    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)?
    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?

  8. #8
    Join Date
    Mar 2009
    Posts
    495
    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.

  9. #9
    Join Date
    May 2014
    Posts
    909
    Quote Originally Posted by JMRKER View Post
    So WHY use a sessionStorage variable when a local variable does the same thing?
    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
    Code:
    <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
    Code:
    <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. (that they are sent with any non-HTML containing file is IMHO a really stupid part of the implementation since it serves no purpose)
    Last edited by deathshadow; 08-13-2014 at 06:03 AM.
    Java is to JavaScript as Ham is to Hamburger.

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles