SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,381
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Is this "correct" sessionstorage?

    In my app, I have a splash screen showing on launch according to code on index.html. However, to prevent it from launching every time the Back button takes one to the index.html page, I set a number in sessionstorage so that if the number is there, it won't launch again as long as it's still in memory. This works fine.

    However, I am having it check a value in sessionstorage before a value has been written, so I'm not sure this is "correct" syntax, even though it works. Is there a correct way to do this?

    Code:
    <script>
    function onBodyLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
    }
    
    function onDeviceReady() {
    // Check session storage to see if splash screen already appeared
     countLaunchImage();
    }
    function countLaunchImage() {
    /* If the value is 0 then it's already displayed. If > 0, then t hasn't yet shown and should display. */
     var name = sessionStorage.getItem("count");
     if (name > 0) { showLaunchImage(); }
    }
    function showLaunchImage() {
    // Show splash screen
    navigator.splashscreen.show();
    // Set value to 0 after it appears
    sessionStorage.setItem("count", "0");
    // countLaunchImage() should now see a 0 and not launch next time.
    }
    setTimeout(function() {
     navigator.splashscreen.hide();
    }, 1000);
    </script>
    Steve Husting

  2. #2
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is correct. getItem will return null if the key doesn't exist, and null > 0 evaluates to false, so it does what you want (or does it?).

    Here's the spec for getItem: http://www.whatwg.org/specs/web-apps...torage-getitem
    Simon Pieters

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,381
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you! I appreciate the answer.
    Steve Husting

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,381
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Interesting that this was moved to JS. I thought it was a spec of HTML5: https://code.google.com/p/sessionstorage/
    Steve Husting

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,000
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Session storage has a JavaScript API, hence the JS forum.
    Thank you for taking the time to think about where to post though. It is appreciated.


Tags for this Thread

Bookmarks

Posting Permissions

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