Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

 

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.

persist attribute

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 size of a window, you would do the following:

<window
  id="someWindow"
  width="200"
  height="300"
  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.

Our Find Files Example

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.

<window
  id="findfile-window"
  title="Find Files"
  persist="screenX screenY width height"
  orient="horizontal"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

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.

var el = env.locale; Find files example so far : Source View

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

Document Tags and Contributors

 Contributors to this page: Sheppy, teoli, fscholz, Chbok, Mgjbot, Morishoji, Jjinux, Pmash, Ptak82, Dria
 Last updated by: Sheppy,