In many cases one needs to authenticate user. When developing web applications, one would use cookies to do that. However, Nokia Web Runtime (WRT) does not fully support cookies so you can’t use them. Alternatively, Nokia WRT has two widget methods that store data in the phone’s persistent memory which can be used to simulate cookies:
setPreferenceForKey() which stores data on the phones persistent memory, and
preferenceForKey() which retrieves that data from the storage.
The setPreferenceForKey method allows a key to be stored along with its associated preference. The arguments are like name and value pairs. The preference value for the key is stored persistently, so if the widget or device is restarted, the value is retained. However, the values stored by a widget are removed when a widget is uninstalled from the device. This includes the case when a widget is reinstalled; where the old widget is uninstalled, the new widget is installed.
In this example I will demonstrate how to use these functions to create a pseudo cookie.
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pseudo Cookie Code Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript" type="text/javascript" src="basic.js"></script>
<link rel="stylesheet" href="basic.css" type="text/css">
<META NAME="Generator" CONTENT="Nokia WRT plug-in for Aptana Studio 2.0.0" />
</head>
<body onLoad="javascript:init();">
<div id="setCookie" class="bigText"><a href="#" onclick="pseudoCookieSet('this is a test string for the psudoCookie', 'testCookie', 30)">Set Cookie (will expire after 30secs)</a></div>
<div id="getCookie" class="bigText"><a href="#" onclick="printCookie('testCookie')">Get Cookie</a></div>
<div id="txt" class="bigText">PseudoCookie Text will show here.</div>
</body>
</html>
The html code above, basically generates 2 links and a div where I can print the data from the pseudo cookie:

Pseudo Cookie index.html screen shot
basic.js:
function pseudoCookieSet (cookieContent, cookieName, duration) {
var cookieStart = Math.round(new Date().getTime() / 1000);
var cookieEnd = cookieStart + parseInt(duration);
var cookieString = cookieContent + '|+|' + cookieEnd;
widget.setPreferenceForKey(cookieString, cookieName);
}
function pseudoCookieGet (cookieName) {
var returnText = '';
var cookieString = widget.preferenceForKey(cookieName);
var cookieElements = cookieString.split("|+|");
var cookieEnd = parseInt(cookieElements[1]);
var timeNow = Math.round(new Date().getTime() / 1000);
if (timeNow > cookieEnd) {
returnText = 'cookie expired';
} else if (cookieString != null) {
returnText = cookieElements[0];
} else {
returnText = 'cookie is empty or not set';
}
return returnText;
}
function printCookie(cookieName) {
var cookieText = pseudoCookieGet(cookieName);
document.getElementById("txt").innerHTML = cookieText;
}
The function pseudoCookieSet (cookieContent, cookieName, duration) sets the pseudo cookie and receives 3 parameters:
- cookieContent – the data you would like to store in the pseudo cookie;
- cookieName – the name of the pseudo cookie (obviously); and
- duration – how long would you like the pseudo cookie to exist before it is expired (seconds)
The function pseudoCookieGet (cookieName) – returns the following values:
- The data you stored in the pseudo cookie if all is valid;
- ‘expired’ if you tried to call the cookie after the cookie was expired;
- An error ‘cookie is empty or not set’ – if the cookie is empty or not set.
you can use delimiters but don’t use the following sequence ‘|+|’ since the function uses it to separate between the stored data and other administrative data. You can also download a fully working widget that demonstrates the above code: pseudoCookie.wgz