Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the better option to use: localStorage(), window.localStorage(), or document.localStorage()?

Tags:

The question says it all. I realize that all options do work in up-to-date browsers, but what is the semantically best choice, and why?

like image 938
木川 炎星 Avatar asked Mar 16 '11 01:03

木川 炎星


People also ask

What is the difference between window localStorage and localStorage?

Supposedly, window. localStorage makes the localStorage faster to be found than just writing localStorage. Storing a reference to it on a variable makes it even faster. Anyway, these improvements are negligible on modern browsers.

What should you use localStorage for?

The localStorage read-only property of the window interface allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions.

Which method will you use to store items in local storage?

The major methods in local storage are setItem , getItem , removeItem and clear . A key is required when storing, retrieving, and removing items from the local storage.

What is the correct way to store the users data in localStorage & fetch the data from localStorage *?

Any content/data saved to the localStorage object will be available after the browser has been restarted (closed and opened again). In order to save an item to localStorage , you can use the method setItem() . This method must be handed a key and a value.


1 Answers

According to W3C standards, the correct is window.localStorage because the localStorage attribute is part of window object. And it is not a function, it is an attribute.

Source: http://dev.w3.org/html5/webstorage/#dom-localstorage

Example: http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm

like image 177
Felipe Avatar answered Oct 12 '22 03:10

Felipe