Results 1 to 2 of 2

Thread: accessing localstorage using javascript

Hybrid View

  1. #1
    Join Date
    May 2013

    accessing localstorage using javascript

    I have a requirement where when the user is working on a screen, before the session logs off it saves a message in local storage:

    var userid = self.document.Form1.gsAftlnr.value;
    /* saves the userid in the above variable */
    localStorage.setItem(userid, 'Your previous work was saved in Drafts in the Post Centre.');

    Suppose if the userid = 749461, with this above storage, I am expecting the localstorage the store the string data under key name 749461. Correct me if I am wrong.

    When the user logs in again, I am executing the following code:
    if(localStorage.getItem(userid) !== null)
    { alert(localStorage.getItem(userid)); }
    I want this alert to show up only when the same user logs in again, which is 749461 in this case. But this alert is popping up for other users also.

    Please let me know what I am doing wrong here.

  2. #2
    Join Date
    Mar 2007
    if(localStorage.getItem(userid) !== null)
    A simple test reveals that locaStorage.getItem does not return type null for which you are testing.

    if( localStorage.getItem( userid ) )
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

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