IE userData Persistence
IE5 and later enable client-side storage by attaching a proprietary “DHTML behavior” to a document element. You can do that with code like this:
varmemory=document.createElement("div");// Create an elementmemory.id="_memory";// Give it a namememory.style.display="none";// Never display itmemory.style.behavior="url('#default#userData')";// Attach a magic behaviordocument.body.appendChild(memory);// Add it to the document
Once you add the “userData” behavior to an element, that element
gets new load() and save() methods. Call load() to load stored data. You must pass a
string to this method—it is like a file name, identifying a particular
batch of stored data. When data is loaded, the name/value pairs become
available as attributes of the element, and you can query them with
getAttribute(). To save new data,
set attributes with set
Attribute() and then call
the save() method. To delete a
value, use removeAttribute() and
save(). Here is an example, using
the memory element initialized
above:
memory.load("myStoredData");// Load a named batch of saved datavarname=memory.getAttribute("username");// Get one piece of stored dataif(!name){// If it wasn't definedname=prompt("What is your name?); // Get user inputmemory.setAttribute("username", name); // Set it as an attributememory.save("myStoredData");// And save it for next time}
By default, data saved with userData has an indefinite lifetime and lasts until you delete it. But you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access