Persistent Data

This section describes how to save the state of a XUL window.

Remembering State

When building a large application, you will typically want to be able to save some of the state of a window across sessions. For example, the window should remember which toolbars are collapsed even after the user exits.

One possibility would be to write a script to collect information about what you would like to save and then save it to a file. However, that would be a pain to do for every application. Conveniently, XUL provides such a mechanism to save the state of a window.

The information is collected and stored in a RDF file (localstore.rdf) in the same directory as other user preferences. It holds state information about each window. This method has the advantage that it works with Mozilla user profiles, so that each user can have different settings.

XUL allows you to save the state of any element. You will typically want to save toolbar states, window positions and whether certain panels are displayed or not, but you can save almost anything.

To allow the saving of state, you simply add a persist attribute to the element which holds a value you want to save. The persist attribute should be set to a space-separated list of attributes of the element that you want to save. The element must also have an id attribute in order to identify it.

For example, to save the position of a window, you would do the following:

  persist="width height"

The two attributes of the window element, the width and the height will be saved. You could add additional attributes by adding a space and another attribute name to the persist attribute. You can add the persist attribute to any element and store any attribute. You might use unusual values if you adjust attributes using a script.

Let's add the persist attribute to some of the elements in the find files dialog. To save the position of the window. To do this, we need to modify the window.

  title="Find Files"
  persist="screenX screenY width height"

This will cause the x and y position of the window and the width and height of the window to be saved. We could extend it further to save the collapsed state of the splitter. It doesn't really make sense to save the current tab state.

(Next) Next, we'll look at using style sheets with XUL files.

Find files example so far: Source View

Add a note User Contributed Notes
October 11, 2005, 8:17 am tony at greyfirst dot ca
Once you add fields to an item's persist attribute, the values saved in localstore.rdf will keep being used even if you later remove those fields from the persist attribute. As an example of where this can cause problems, if you have a resizable window that persists its width and height and you later decide to make it non-resizable with a fixed width and height, the width and height saved in localstore.rdf will override the width and height you specify. An easy work-around is to change the id attribute of the affected element, since persistent data is stored and retrieved by id.
May 23, 2004, 11:16 am 1 at 234 dot cx
On Unix, the position of top level, independent windows is left to the window manager. This means that, for example, 'persist="screenX screenY"' will not work unless the window is marked as dependent when opened. You can do this by adding the "dependent" flag as follows: ("chrome://doodah/content/doodah.xul", "_blank", "chrome,dependent");

For further details see lines 948 to 954 in xpfe/appshell/src/nsXULWindow.cpp, in the Mozilla 1.6 source.
April 16, 2004, 12:24 pm fkoenen at virtualmonet dot com
Can't get you're splitter position to persist? Try adding persist to the
item left or right (above or below) the splitter, the width or height attributes. forcing the items that the splitter splits to persist, may work for you.
February 9, 2004, 5:46 pm rkeane at acm dot org
Trying to save the splitter state I did:
<splitter id="hor-splitter" collapse="before" persist="collapse">

That didn't work but this from the O'Reilly book did:
<splitter id="hor-splitter" collapse="before" persist="state hidden">

Copyright © 1999 - 2005