다음을 통해 공유


Introduction to Persistence

Persistence enables authors to specify an object to persist on the client during the current and later sessions using Dynamic HTML (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:

  • saveFavorite—persists page state and information when the web page is saved as a favorite.
  • saveHistory—persists page state and information within the current session's memory.
  • saveSnapshot—persists page state and information directly in the page when users save the Web page to their hard disk.
  • userData—persists page state and information within an XML store, a hierarchical data structure.

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 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.

  • Benefits 
  • Using Persistence 
  • Example Uses of Persistence 
  • Persisting Content in XML 
  • Persistence How-Tos 
  • Related Topics

Benefits

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 sent in e-mail 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.

Using Persistence

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 persist automatically. Script blocks designated to persist can contain only variables. Script objects and comments are removed. Qualifying variables from external sources on persisting script blocks are 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 64 KB per page, with a limit of 640 KB per domain, for restricted sites. For security reasons, a userData store is available only in the same directory and with the same protocol used to persist the store.

security note Security Alert  Using these behavior incorrectly can compromise the security of your application. The saveFavorite, saveSnapshot, and userData behaviors persist data as plain text in a saved Web page. Text is not encrypted and therefore not secure. Any application that has access to the drive where the page is saved also has access to the data and can tamper with it. Therefore, it is recommended that you not persist sensitive data like credit card numbers. For more information, see Security Considerations: DHTML and Default Behaviors.

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.

Example Uses of Persistence

The following examples show how to use behaviors to implement persistence.

The following examples shows 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()"
>

Code example: https://samples.msdn.microsoft.com/workshop/samples/author/persistence/saveFavorite_1.htm

The following example shows 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 persists:
<INPUT TYPE="text" CLASS="saveHistory" ID="oPersistInput">
This does not persist:
<INPUT TYPE="text">

Code example: https://samples.msdn.microsoft.com/workshop/samples/author/persistence/saveHistory_1.htm

The following example shows 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>
:

Code example: https://samples.msdn.microsoft.com/workshop/samples/author/persistence/saveSnapshot_1.htm

The following example shows 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>

Code example: https://samples.msdn.microsoft.com/workshop/samples/author/persistence/userData_1.htm

Persisting Content in XML

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("element","MyElement", "");
oNode.nodeValue="The value of MyElement";
oXMLDocument.documentNode.insertNode("MyElement");

In XML, this would yield:

<MyElement>The value of MyElement</MyElement>

Persistence How-Tos

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.