Expand | Collapse

◀ 23. CSS formatting25. To-do list ▶

24. JavaScript support

24.1. Namespace policy

All global JavaScript functions and variables shall be prefixed with “xhtml2to1_”.

24.2. Document load and unload

    function xhtml2to1_onload()
    {
      var nav_div = document.getElementById('_navigation');
      xhtml2to1_tree_load_state(nav_div);
      return true;
    }

    function xhtml2to1_onunload()
    {
      var nav_div = document.getElementById('_navigation');
      xhtml2to1_tree_save_state(nav_div);
      return true;
    }
    

24.3. Collapse-expand toggle

Certain block-level elements displayed in the document body can be optionally collapsed and hidden by the user. In particular, this is used for sub-sections in the document. The xhtml2to1_collapse_toggle JavaScript function implements this functionality.

This function can be called via the onclick attribute, passing it as parameters:

  1. the ID of the content element block to collapse or expand if the toggle is clicked;
  2. the standard event object

      function xhtml2to1_collapse_toggle(id, event)
      {
        if(!event) event = window.event;
        var switch_elem = event.target ? event.target : event.srcElement;
        
        var target = document.getElementById(id);
      
        if(target.style.display == 'none') {
          target.style.display = 'block';
          switch_elem.childNodes.item(0).data = '\u25bc';
        } else {
          target.style.display = 'none';
          switch_elem.childNodes.item(0).data = '\u25ba';
        }

        return false;
      }
    

24.5. Functions to access the DOM

      function xhtml2to1_get_child_element(e, gi) {
        var cl = e.childNodes;
        for(var i = 0; i < cl.length; ++i) {
          if(cl[i].nodeType == document.ELEMENT_NODE 
              && cl[i].tagName == gi)
            return cl[i];
        }
      
        return null;
      }
    

24.6. Functions to access cookies


      function xhtml2to1_get_cookie(name) {
        var a = document.cookie.split(';');
        var re = /^ *([^ =]+)=(.*)$/;

        for( var i = 0; i < a.length; ++i ) {
          var m = re.exec( a[i] );
          if ( m != null && m[1] == name )
              return m[2];
        }

        return null;
      }

      function xhtml2to1_set_cookie(name, value, days) {
        var info = "";
        if( days ) {
          var date = new Date();
          date.setTime( date.getTime() + (days*24*60*60*1000) );
          info = "; expires=" + date.toGMTString();
        }
        info += "; path=/";

        document.cookie = name + "=" + value + info;
      }

      function xhtml2to1_erase_cookie(name) {
        xhtml2to1_set_cookie(name, "", -1);
      }

    

Formatted using xhtml2to1 by Steve Cheng.