Persistence enables authors to specify an object to persist on the client during the current and future sessions using DHTML behaviors. Persistence allows Microsoft® Internet Explorer 5 and later to retain Web page information, styles, variables, and state. For example, a collapsible list of links within a table of contents can remain expanded to the user's choice upon leaving and later returning to the page. Or, a search engine query form can retain the last-used search string.
Persistence is implemented as a behavior. The new persistence behaviors include:
Persistence creates new opportunities for the author. Information that persists beyond a single page without support from the server, or within the finite scope of cookies, can increase the speed of navigation and content authoring.
These behaviors can be used to preserve information in the browser's history, in favorites, in an XML store, or directly within a Web page saved to disk. When a user returns to a persisted page, the state of the page can be restored. By allowing information to safely reside on the client, fewer server transactions are required. Custom start pages and Web applications will prosper because information can continue to exist without the paternal support of the server, or repeated server queries. A Web page can remain interactive to a degree, after the connection with the host has been severed, by persisting required information on the client.
Use of persistence in Internet Explorer 5 breaks away from the tradition of using cookies and session files to store information. The Microsoft model for client-server relationships using persistence and Internet Explorer 5 is pragmatic; it maintains the same-domain security policy associated with cookies while increasing storage and management capabilities.
Server solutions | Persistence solutions | |
---|---|---|
Start pages | Custom start pages often rely on cookies and server-side databases to preserve client-specific information. Storing information such as client capabilities, favorite styles, and favorite content requires additional queries to the server to retrieve the information. This results in increased download time for the client and increased processing demands on the server. | Persistence via DHTML behaviors allows a page author to store information on the client. This reduces the need to query a server database for client-specific information and increases the overall performance of the page. Persistence stores the data hierarchically, making it more accessible. |
Web apps | Web-based applications have remained tied to the server for the back-end strength and processing power they require. A Web site that includes a basic form to be e-mailed uses a server to handle the request, or a script to process any e-mail beyond a conventional note. | A persistent Web page can be saved with its form values intact, making it easier to transfer or preserve an entire document. Persistence introduces a new paradigm for Web applications by giving authors the ability to produce complex Web-based applications that take advantage of the client's processing power. |
Forms | Advanced search engines and information entry and retrieval forms often rely on session variables and files to preserve information across Web pages. The server must send and store the same information with each new visit to the Web page, taxing the Web server and the Internet line. | While there is some information that should remain on the server, the rest can be stored on the client. Client-side scripting within each Web page can perform the necessary validation routines, leaving the server to higher-level security concerns. Since persisted information maintains a similar same-domain security structure as cookies, the client can be assured that rogue Web sites will not glean such information from their computers. |
Persistence behaviors require certain conditions before they can be used. A META tag directs the browser that the page is persistent. Persistent elements are identified with a class and an ID property. The onsave and onload event handlers can be defined for nondefault handling.
Behavior | Description |
---|---|
saveFavorite | Persists an object across sessions when the page has been saved as a favorite.
The getAttribute and setAttribute methods are used to store information on a persistent object. The onload and onsave event handlers can be used to call the methods. The saveFavorite behavior is ideal for persisting user-selected styles within a favorite or shortcut. The persisted information is stored within each favorite, allowing multiple favorites with different persisted information to exist. |
saveHistory | Persists an object during the current session.
The getAttribute and setAttribute methods can be used to store information on objects participating in persistence. The onload and onsave event handlers can be used to call the methods. The saveHistory behavior is ideal for storing information only while the browser is open, such as page state for collapsible content, or dynamic styles. |
saveSnapshot | Persists an object across sessions when a page has been saved as Web Page, HTML Only.
Form elements will persist automatically. Script blocks designated to persist can contain only variables. Script objects and comments will be removed. Qualifying variables from external sources on persisting script blocks will be inserted. The saveSnapshot behavior is ideal for persisting Web application information to the client's disk for later retrieval. |
userData | Persists an object across sessions in a UserData store (an arbitrary storage facility) when the object has been explicitly saved.
Form elements, styles, and dynamic values can be persisted using the getAttribute and setAttribute methods, and then explicitly saved using the load and save methods. The userData behavior is ideal for persisting information across sessions and storing information in a hierarchical structure. It provides a good alternative to using cookies. The capacity of the UserData store is 64K per page, with a limit of 640K per domain. For security reasons, a UserData store is available only in the same directory and with the same protocol used to persist the store. |
An ID is not required for the saveFavorite and saveHistory behaviors, but is recommended for performance. The STYLE element also can be set inline on a persistent object.
Following are examples of using behaviors to implement persistence.
Sample Code
This is an example of the saveFavorite behavior.
<!-- Tell the browser the page is persistent--> <META NAME="save" CONTENT="favorite"> <!-- Define the class for the persistent object--> <STYLE> .saveFavorite {behavior:url(#default#savefavorite);} </STYLE> <SCRIPT> // Record information to the persistent object as an attribute. function fnSave(){ oPersistInput.setAttribute("sPersistAttr",oPersistInput.value); } // Retrieve information from a persistent object. function fnLoad(){ oPersistInput.value=oPersistInput.getAttribute("sPersistAttr"); } </SCRIPT> : <!-- The CLASS enables the object to persist, the event handlers fire the scripted functions.--> <INPUT TYPE="text" CLASS="saveFavorite" ID="oPersistInput" onload="fnLoad()" onsave="fnSave()" >
This is an example of the saveHistory behavior.
<!-- Tell the browser the page is persistent--> <META NAME="save" CONTENT="history"> <!-- Define the class for the persistent object--> <STYLE> .saveHistory {behavior:url(#default#savehistory);} </STYLE> : <!-- The default behavior is to persist form elements. When the saveHistory behavior is defined, form elements will not persist unless they have a class. --> This will persist: <INPUT TYPE="text" CLASS="saveHistory" ID="oPersistInput"> This will not persist: <INPUT TYPE="text">
This is an example of the saveSnapshot behavior.
<!-- Tell the browser the page is persistent--> <META NAME="save" CONTENT="snapshot"> <!-- Define the class for the persistent object--> <STYLE> .saveSnapshot {behavior:url(#default#savesnapshot);} </STYLE> : <!-- When the page is saved, the information entered in the persistent form element will be inserted as the value within the HTML file.--> <INPUT TYPE=text CLASS=saveSnapshot ID=oPersistInput> :
This is an example of the userData behavior.
<!-- Define the class for the persistent object--> <STYLE> .userData {behavior:url(#default#userdata);} </STYLE> <SCRIPT> function fnSaveInput(){ // Persistent information is stored as an attribute // on the persistent object, and saved within a // UserData store. var oPersist=oPersistForm.oPersistInput; oPersist.setAttribute("sPersist",oPersist.value); oPersist.save("oXMLBranch"); } function fnLoadInput(){ // Persistent information is loaded from a UserData store // and the value is restored from the persisted attribute. var oPersist=oPersistForm.oPersistInput; oPersist.load("oXMLBranch"); oPersistInput.value=oPersistInput.getAttribute("sPersist"); } </SCRIPT> : <FORM ID="oPersistForm"> <INPUT class=userData type=text id=oPersistInput> <INPUT type=button value="Load" onclick="fnLoadInput()"> <INPUT type=button value="Save" onclick="fnSaveInput()"> </FORM>
The XMLDocument property allows the saveFavorite, saveHistory, and userData behaviors to access the XML Document Object Model (DOM). Using the XML DOM, persistent objects have access to the objects, properties, and methods that allow the author to build hierarchical data structures.
var oXMLDocument=oPersistText.XMLDocument; var oNode=oXMLDocument.createNode(0,"MyElement"); oNode.nodeValue="The value of MyElement"; oXMLDocument.documentNode.insertNode("MyElement");
In XML, this would yield:
<MyElement>The value of MyElement</MyElement>
Persistence introduces a broad expanse of possibilities. The following how-to articles cover some of the more interesting, albeit easy-to-employ, uses of persistence.
The following articles and references contain additional information on authoring and implementing DHTML behaviors.
DHTML Behaviors Persistence