This article explains how to automatically save user input, and how to reload the saved input values within a Web Runtime widget.
Contents |
User input on mobile devices is slower and more difficult than on desktop devices, due to different factors, as:
So, mobile applications should always provide a functionality to allow the user not to lose the entered data, when some kind of interruptions happen, as:
In these and other cases it's a good practice to allow the user to continue editing the previous entered data, when he goes back to the previous input screen. This greatly improves the final user experience, since it avoids unnecessary actions and inputs from the user, and allows a more flexible usage of the mobile application.
This example shows how to automatically save and load user input from 2 text fields, without requiring any extra actions to the user. The same approach can be used with more complex forms, and with different kinds of fields.

For this example, a basic SMS form is built, with the classic user input fields:
<html>
[...]
<body onLoad="javascript:init();">
<div id="sms_form">
<label for="receiver">Phone Number</label>
<input type="text" name="receiver" id="receiver" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<input type="submit" value="Send">
</div>
</body>
</html>
In order to save the user input, different approaches are possible:
So, the last proposed approach will be used, and entered data will be saved only when actually changed. In order to be notified of any changes of an input field, the onchange event handler is used. The following startAutosave() method takes a field and a key as arguments, and starts listening to the change events of that field. When this event is fired, the saveValue() function is called, that performs the actual saving of the input field value:
function startAutosave(field, key)
{
var changeHandler = function()
{
saveValue(field, key);
};
field.onchange = changeHandler;
}
function saveValue(field, key)
{
widget.setPreferenceForKey(field.value, key);
}
So, the the widget's init() method, the startAutosave() method is called for both the receiver and the message inputs:
function init()
{
startAutosave(document.getElementById('receiver'), 'sms_receiver');
startAutosave(document.getElementById('message'), 'sms_message');
}
Once the user exits and reopen the widget, or simply goes back to the previous form view, the previously saved input values have to be restored, and this can be easily done by reading the preferences stored by the previous saveValue() function. The following loadSavedValue() method takes a field and a key as arguments and, if available, restores the saved value in the input field:
function loadSavedValue(field, key)
{
var value = widget.preferenceForKey(key);
if(value)
{
field.value = value;
}
}
In order to restore the values as soon as the user enters the widget, the loadSavedValue() can be called in the widget's init() method:
function init()
{
loadSavedValue(document.getElementById('receiver'), 'sms_receiver');
loadSavedValue(document.getElementById('message'), 'sms_message');
}
A sample widget, that shows the auto-save and auto-load functionalities, is available for download here: Media:TextFieldAutoSaveWidget.zip
No related wiki articles found