HTML5 Local storage vs. Session storage

后端 未结 10 2220
甜味超标
甜味超标 2020-11-22 03:16

Apart from being non persistent and scoped only to the current window, are there any benefits (performance, data access, etc) to Session Storage over Local Storage?

相关标签:
10条回答
  • 2020-11-22 03:59

    localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage.

    That is, the data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.

    For sessionStorage, changes are only available per tab. Changes made are saved and available for the current page in that tab until it is closed. Once it is closed, the stored data is deleted.

    0 讨论(0)
  • 2020-11-22 04:00

    Local storage: It keeps store the user information data without expiration date this data will not be deleted when user closed the browser windows it will be available for day, week, month and year.

    //Set the value in a local storage object
    localStorage.setItem('name', myName);
    
    //Get the value from storage object
    localStorage.getItem('name');
    
    //Delete the value from local storage object
    localStorage.removeItem(name);//Delete specifice obeject from local storege
    localStorage.clear();//Delete all from local storege
    

    Session Storage: It is same like local storage date except it will delete all windows when browser windows closed by a web user.

    //set the value to a object in session storege
    sessionStorage.myNameInSession = "Krishna";
    

    Read More Click

    0 讨论(0)
  • 2020-11-22 04:04

    sessionStorage is the same as localStorage, except that it stores the data for only one session, and it will be removed when the user closes the browser window that created it.

    0 讨论(0)
  • 2020-11-22 04:08

    The only difference is that localStorage has a different expiration time, sessionStorage will only be accessible while and by the window that created it is open.
    localStorage lasts until you delete it or the user deletes it.
    Lets say that you wanted to save a login username and password you would want to use sessionStorageover localStorage for security reasons (ie. another person accessing their account at a later time).
    But if you wanted to save a user's settings on their machine you would probably want localStorage. All in all:

    localStorage - use for long term use.
    sessionStorage - use when you need to store somthing that changes or somthing temporary

    0 讨论(0)
提交回复
热议问题