Source Code

for file ~/controls/persistcookie.js

// PersistCookie.js
// Javascript Behaviour for the PropPersist Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// To enable the PropPersist just include this file in the page or use the
// PropPersist control.
// ----- 
// 21.10.2006 created by Matthias Hertel
// 20.08.2007 created from the old PropPersist control.
// 05.09.2007 using the OpenAjax hub instead of PageProperties
// 18.12.2007 Simplifications and documentation.
// 19.01.2008 implementing the initstate method to support initial values in a solid way.

jcl.PersistCookieBehavior = {
  /// <summary>Implementation of a JavaScript Behavior that captures the specified events of the eventnamespace
  /// and puts the values into a browser cookie.
  /// When the page is loaded all the vlaues will be used to publish the events again and spread the recorded values.</summary>

  eventnamespace: "jcl", /// <summary>Namespace of the events published by contained elements without a full qualified namespace.</summary>
  eventname: "", /// <summary>The local or complete event name that is used for publishing OpenAjax events.</summary>
  path: null, /// <summary>The path that s used to store the cookie.</summary>
  expire: null, /// <summary>The number of days the cookie should be saved.</summary>

  init: function () {
    /// <summary>Initializing the control.</summary>
    if ((this.eventname != null) && (this.eventname != "")) {
      if (typeof (this.eventname) == "string")
        this.eventname = this.eventname.split(';');
      OpenAjax.hub.subscribe(this.eventnamespace + ".*", this._handleEvent, this);
    } // if
  }, // init


  initstate: function () {
    /// <summary>After loading the page the event values that are persisted to cookies
    /// will be published.</summary>

    // raise all persisted values
    var enl = this.eventname.length;
    if (this.eventname != null) {
      for (var n = 0; n < this.eventname.length; n++) {
        var en = this.eventname[n]; // eventname
        var ev = null; // eventvalue

        if (en.indexOf('=') > 0) {
          // default event value
          ev = en.split('=')[1];
          en = en.split('=')[0];
        } // if

        var ev2 = jcl.getCookie(en);
        if ((ev2 != null) && (ev2 != "")) ev = ev2;

        if (ev != null)
          OpenAjax.hub.publish(this.eventnamespace + "." + en, ev);
      } // for
    } // if
  }, // initstate


  _handleEvent: function (eventName, eventData) {
    /// <summary>Handle a published OpenAjax event and persist the value of the event into a cookie.</summary>

    // remove the eventnamespace from the eventName
    var en = eventName.substr(this.eventnamespace.length + 1);
    for (var n = 0; n < this.eventname.length; n++) {
      if (this.eventname[n].split('=')[0] == en) {
        jcl.setCookie(en, eventData, this.path, this.expire);
        break;
      } // if
    } // for
  } // _handleEvent

}; // jcl.PersistCookieBehavior


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.