materials on this site are legal and intellectual property of Discover Mountain
Reproduction of any portion of any materials on this site without prior consent is absolutely not allowed.
A Tutorial in the use of Internet Explorer's userData - the Super Cookie!
No server side execution means databases are out. What is left? How about using the file system object to read and write files on the client computer. That's possible, but it is generally considered intrusive and there is the remaining problem of auto-deleting stale records.
You see why I decided I needed a cookie type object? Simple to read / write / update / auto-expire. But the 4k limit was not good. I spent hours searching the net for my answer. There was one possibility called userData. userData under the right circumstances can hold up to 512K per web page and 10M per domain! Incredible! It functions like a cookie in that a web page merely invokes a few commands and data can be written to the browser cache as an XML document completely unbeknownst to the user, and the data persists through re-boots and temp folder deletions. Further, userData can be auto-expired just like a cookie. It's a Super Cookie! The limitation is that userData is a behavior only available in Internet Explorer. Well, that is the most common browser in the world whether I use it or not. And in this case, it was easy to say all users would be on IE. Here is what Microsoft has to say about userData. Click here.
Okay. I had my Super Cookie. Now to learn the examples and here we go! Oh rats. The example is so simplistic that it is worthless. Now who would have expected that from MS? Not me. Nope, never. MS's example only demonstrates setting attributes within the root stub. If you haven't studied XML you probably don't know what that means and neither did I. I certainly had a lot jargon to learn to get up to par on this one. Here is a MS type example of what an XML document looks like given a single attribute:
<rootstub name="bob" />
And that's it. That's the sophistication level of MS examples. Here is how you can create similar results.
Enable the userData behavior by adding the following lines to your html page. ASP not necessary as this is an html behavior!
var fdata = form.text1;
The function name is saveUserData. We assign the object form object text1 to variable fdata. We then set a rootstub attribute "name" and give it fdata.value or the value of the text field. You can use ANY name in place of mydata. That is simply the name of the xml file that will be stored on the client's browser. On to Load.
var fdata = form.text1;
fdata.value = fdata.getAttribute("name");
The function is called loadUserData. We set variable fdata to the form object text1. We load the userdata stored in mydata and reset the form field text1 value by using the getAttribute command. And finally here is the form.
<input class="userData" type="text" id="text1">
<input type="button" value="Save" onclick="saveUserData()">
<input type="button" value="Load" onclick="loadUserData()">
Notice I invoke the behavior by calling it in the input tag where the text field is. Actually, I don't do it this way but we're showing a simple example, right? Type something in the box and hit Save. Then blank the text field and hit Load. It'll come right back. In fact, reboot your computer or wipe your cache, it'll always still be there because we didn't set it to expire. Oops! Something MS forgot to put in their examples. By the way, here is the entire working script if you don't want to type it in to see it work. Feel free to grab the entire code and play with it yourself. (If you want to look at the XML on your computer, I can only help with Win2K. It's located on the drive where you operating system is installed, under Documents and Settings, within the folder which represents your own user that logs into the PC, then under Application Data/Microsoft/Internet Explorer/User Data. There you'll find four folders composed of random letters. The browser randomly puts userData in these folders. Look through them and see what is there!)
But what if you wanted something that was actually useful, such as an age and weight associated with multiple names? Well, you can't really do that with MS's example, but you could modify it all a bit and end up with an xml structure like this:
<rootstub name1="bob" age1="17" weight1="110" name2="joe" age2="11" weight2="60" />
and this is perfectly acceptable XML. And if you worked at it you could parse out the information for each name. This still isn't very useful in real-world situation. What if you needed dozens of bits of information about each person and you wanted a dropdown list of each name to select from? Clearly MS's example left a great deal to be desired.
That's our content for the second part of this article. If you're ready to move on, the next article is nearly complete. Check back in a few days.