spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column24


Persistence Methods

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

In this page we summarize the common changes, additions, and available methods that you can use in programming persistence. First, you have to add the following META tag to your page:

<META NAME="save" CONTENT="<persistenceBehavior>">

where <persistenceBehavior> can be favorite, history, snapshot, or userdata.

Secondly, you need to define the persistence as a built-in one:

<STYLE>  
  .save<persistenceBehavior>{behavior:url(#_IE_);}
</STYLE>

where <persistenceBehavior> is one of the four specified above.

Next, you need to decide which elements we want to persist. You have seen examples where we persisted the BODY element, an IMG element, an INPUT element, and a SCRIPT element. In order to enable a persistent element, you need to specify its Behavior via the CLASS attribute, and assign an ID to the element.

The element's ID is the key to all persistence Behaviors. They all share the same principle, which is first packing the data into the element's object (identified by its ID). Packing of data into the element's object is done via the setAttribute() method, as we explain below. Unpacking the data out of the element's object is done via the getAttribute() method. Persistence Behaviors differ from each other in the way they expect the save and load operations to be triggered. The favorite Behavior expects the save operation to be triggered by the user when he or she selects the Add to Favorites option from the Favorites menu. It expects the load operation to be triggered by the user when he or she selects the favorite name from the list of available favorites. The history Behavior saves the Web page when the user goes Back or Forward, and restores it when the user comes back to the page. The snapshot Behavior saves the data when the user enter a form filed, and it restores the data when the form comes up. The userdata Behavior expects the user to explicitly save and load the element's object via the save() and load() methods, respectively.

For the the favorite and history Behaviors, you need to define an onload() and an onsave() event handlers. The acts of save and load is being done in these event handlers. In the case of the userdata Behavior, these operations can be invoked anywhere in the JavaScript code, and there are no event handlers that do them automatically. You don't need these event handlers for the snapshot Behavior either, because it saves form data automatically.

The generic persistence element definition looks like this:

<<TAG> CLASS="save<Behavior>" ID="id"
 onload="<onloadEventHandler()>" onsave="<onsaveEventHandler()>">

The onsave() event handler defines attributes that store whatever information you need to persist. We use the setAttribute() method to create the attributes:

setAttribute("<attributeName>", <data>);

where <attributeName> is an author-determined name, and <data> can be of any scalar type such as an object property value, an array element, a variable, etc. The onload() event handler decipher the attributes by the getAttribute() method:

<data> = getAttribute("<attributeName>");

Remember to check for the existence of the attributes before you assign them to properties. If you will not check it, properties would be assigned NULL values.

http://www.internet.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle


Created: August 28, 1998
Revised: August 28, 1998

URL: http://www.webreference.com/js/column24/methods