In the present note we will see a way to store remote data in the browser to be later retrieved from the application, when needed.
Remembering login parameters, saving the last position on a map or a form’s data are typical cases of such a scenario. One possible way to address this need would to keep that information somewhere in the disk from where the application can recover it promptly. This would seem to be the most natural way of achieving that goal. But what would happen when we run the application remotely from the browser, greatly opening the range of users? In these cases, saving the information in the application does not seem to be the best way to do it; the most reasonable way, however, seems to be storing that information “near the user”; i.e, in the browser.
One of the most effective and classical method of sharing information between the browser and any remote application is by using cookies. Cookies are small pieces of information that the application can store in each browser and later recover them in a clear way. In our particular case, we can create and enquire the value of a cookie directly from the application through the SetCookie and GetCookie methods of Thinfinity.BrowserInfo.
The SetCookie method has the following format:
VirtualUI.BrowserInfo.SetCookie(name, value, expirationDate);
|name||string||Name of the cookie. With this name it is stored, properly identified, in the browser.|
|value||string||Value of the cookie|
|expirationDate||string||Stored in UTC/GMT format, indicating the expiration date of the cookie. If its value is “”, the cookie does not expire.|
This example written in Delphi shows how store a data value (in this case the value of the username field) into a cookie:
procedure TForm1.Button1Click(Sender: TObject); begin if Trim(edtUsername.Text) = '' then begin ShowMessage('The username field must have a value'); end else begin // saving the cookie when running with Thinfinity VirtualUI only. if VirtualUI.Active then begin VirtualUI.BrowserInfo.SetCookie('CookiesDemo_Username', edtUsername.Text, ''); ShowMessage('Username was saved as a cookie in your browser.'); end; end; end;
The GetCookie method has a single argument:
|name||string||Name of the cookie. With this name the cookie will be retrieved from the browser. If the cookie doesn’t exist, an empty string will be received.|
And the example written in Delphi is:
procedure TForm1.FormShow(Sender: TObject); begin if VirtualUI.Active then edtUserName.Text := VirtualUI.BrowserInfo.GetCookie('CookiesDemo_Username'); end;
Just like in the case of SetCookie, in the present case we also making sure that VirtualUI is active (i.e., that this instance of the application runs remotely, using VirtualUI).
Thus, with very few code lines, we can integrate a little further the browser with our application, and add minor enhancements that help improve user experience.