Author: James Kong Emails: jameskong@brandeis.edu, jameskong098@gmail.com GitHub: jameskong098 LinkedIn: James Deming Kong
localStorage is a web storage object that enables developers to store key-value pairs in a web browser. It ensures that this data survives all page refreshes, even when a user closes or restarts a browser. It does not have an expiration date.
localStorage can be used for settings pages, such as the one used in my team's project Command & Control. While storing setting configurations within an online database like MongoDB is an option, it is not always necessary, especially when a user account system is not required. Saving settings in localStorage allows the website to function properly regardless of where it is accessed.
It is recommended to create a default configuration file that lists a set of default values in case no setting configurations have been stored in localStorage. Below is an example of a config file used in our project:
In the example below, we use a default value that is defined beforehand from the ros_config file. The logic states that if no value exists within local storage, then use the value from the ros_config value. You can just name the local storage value any name just make sure it stays consistent when you try to update it later on.
After you define the logic for determining what value to use upon boot, you will want to also know how to change and/or clear the values from local storage for maybe implementing a save button or a clear button.
To update the local storage value, you will want to use the following function (with the first parameter being the name of the local storage value and the second being the value you want to be used for replacing the local storage value):
To clear the values within local storage, you will want to use the following function: