JavaScript: Debugging messages

Intro

Maybe its redundant, but I created my own way of adding a 'poor man's debugging' method for JavaScript.

I was annoyed, that I have to use either alert, which pops up a nasty new window, or document.write(ln) which creates a new page. Therefore this debug class, accesses a given section, typically <ul id="unique"></ul>, <ol id="unique"></ol>, <div id="unique"></div> with an specified id, or creates a new <div id="unique"></div> section on top of the page, where any new added debug entry is listed.

Example

Defined area, immediately

    Some messages:

Code

%begin html%
    Some messages:
%end%

Undefined area, on click

Click here to get debug output at the head of the document

Code

%begin html% Click here to get debug output at the head of the document %end%

Syntax

Code

debug.js: JavaScript script for debugging Click to retract Click to retract ...
%begin javascript% /* * debug.js * Debug class for debugging informations * without alert or document.write(ln) methods * but within a given or newly created block element * * Author: P.Zumbruch, P.Zumbruch@gsi.de * Date: 04 Sep 2007 * */

function debugOutput (id, element) { /* Constructor: data: id: unique attribute id to look for or to create for the output data element: block element to use for the entries [ul, ol, span, ... ] methods: init(id): initializing the object
add(text)
adding a new text element

*/ this.index = 0; this.id = id; this.element = element;

this.add = add; this.init = init;

init(this.id); }

function add(text) { /* add(text): adds a new element of type element to the output enclosing the textNode filled with "text"

argument(s):
text
any text
*/ document.getElementById(this.id).appendChild(document.createElement(this.element)); document.getElementById(this.id).getElementsByTagName(this.element)[this.index++].appendChild(document.createTextNode(text)); }

function init(id) { /* init(id): initializes the object: i.e.: - it looks for a given element having the attribute id - if not existing, a new "div" element is created and positioned as the first child right of the "body" argument(s):
id
any valid HTML/XHTML/XML universal attributes id

*/ if ( document.getElementById(id) == null ) { var obj = document.createElement("ul"); var area = document.createElement("div"); area.style.backgroundColor = "#999"; area.style.color = "#000000"; area.style.border = "2px solid #f00"; area.style.padding = "1ex"; obj.id = id; var pos = document.getElementsByTagName("body")[0]; pos.insertBefore(area,pos.firstChild); var header = document.createElement("h4"); var text = "Debug information (" + id +"):"; header.appendChild(document.createTextNode( text )); area.appendChild(header); area.appendChild(obj); area.appendChild(document.createElement("hr")); } } %end%

-- PeterZumbruch - 14 Sep 2007
  • debug.js: JavaScript script for debugging
Topic attachments
I Attachment Action Size Date Who Comment
debug.jsjs debug.js manage 2.0 K 2007-09-04 - 16:38 PeterZumbruch JavaScript script for debugging
Topic revision: r3 - 2007-09-14, PeterZumbruch
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback
Imprint (in German)
Privacy Policy (in German)