Decrypting LocalStorage: What is this mysterious file?
With the development of the Internet, web development has become more and more common, and people's personal information and data are also widely stored in browsers. And one of the mysterious files is LocalStorage. So what exactly is LocalStorage? In this article, we will decipher the principles and usage of LocalStorage and provide specific code examples.
LocalStorage is a web storage mechanism provided by the browser, which can store and obtain key-value pair data on the browser. Compared with traditional cookies, LocalStorage has a larger storage capacity (usually 5MB) and a longer storage period (permanent storage). Without expiration time, LocalStorage data will always exist in the user's browser and will not be cleared even if the browser is closed.
The use of LocalStorage is very simple. We can use JavaScript to operate LocalStorage, set key-value pair data through the setItem() method, obtain data through the getItem() method, and delete data through the removeItem() method. The following is some basic sample code:
// Set LocalStorage data
localStorage.setItem('name', 'David');
localStorage.setItem('age', '28');
// Get LocalStorage data
console.log(localStorage.getItem('name')); // Output: David
console.log(localStorage.getItem('age')); // Output: 28
// Delete LocalStorage data
localStorage.removeItem('name');
console.log(localStorage.getItem('name')); // Output: null
LocalStorage can not only store string type data, but also other basic data types, such as numbers, Boolean values, etc. LocalStorage can also store object type data, just convert the object into JSON format. The sample code is as follows:
// Set LocalStorage object data
var user = {
name: 'David',
age: 28,
email: 'david@example.com '
};
localStorage.setItem('user', JSON.stringify(user));
// Get LocalStorage object data
var storedUser = JSON.parse(localStorage.getItem ('user'));
console.log(storedUser.name); // Output: David
console.log(storedUser.age); // Output: 28
console.log(storedUser. email); // Output: david@example.com
LocalStorage data is stored in domain name units, and LocalStorage data under different domain names are independent. This means that in the same browser, web pages under different domain names cannot share LocalStorage data. This is to protect the privacy and security of users.
In addition to simply setting and obtaining data, LocalStorage can also monitor data changes. Through the addEventListener() method, we can add a change event listener to LocalStorage. When the data in LocalStorage changes, the event will be triggered. The sample code is as follows:
// Monitor LocalStorage data changes
window.addEventListener('storage', function(e) {
console.log('LocalStorage data changes:', e.key, e.newValue);
});
// Modify LocalStorage data
localStorage.setItem('name', 'Emily');
// Console output: LocalStorage data changes :name Emily
Summary: LocalStorage is a mysterious and powerful web storage mechanism that can store and retrieve data on the browser. The use of LocalStorage is very simple, and data can be easily manipulated through the setItem(), getItem() and removeItem() methods. LocalStorage data is stored in domain name units, and LocalStorage data under different domain names are independent. Changes in LocalStorage data can be monitored through the addEventListener() method. The use of LocalStorage can help developers store and manage data more conveniently and provide a better user experience. I hope this article will help you decrypt LocalStorage!
The above is the detailed content of Understanding localstorage: Uncovering the mysteries of this mysterious file. For more information, please follow other related articles on the PHP Chinese website!