|
XHTML
var XHTML = {
appendChild: function(node, child, attributes) {
if (typeof child == 'string') {
node.appendChild(document.createTextNode(child));
} else if ((typeof child == 'object') && (typeof child.nodeType != 'undefined')) {
node.appendChild(child);
} else if ((typeof child == 'object') && (child instanceof Array)) {
for (var i=0; i<child.length; ++i) {
XHTML.appendChild(node, child[i], attributes);
}
} else {
node.appendChild(document.createTextNode('ERROR!'));
}
if (!!attributes && (typeof attributes == 'object')) {
for (var a in attributes) {
node.setAttribute(((a == 'className' && /*@cc_on!@*/true) ? 'class' : a), attributes[a]);
}
}
return node;
},
createElement: function(element, child, attributes) {
var node = document.createElement(element);
return XHTML.appendChild(node, child, attributes);
},
init: function() {
var elements = ['a', 'abbr', 'acronym', 'address', 'bdo', 'blockquote', 'br', 'button', 'caption', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'fieldset', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link', 'object', 'ol', 'optgroup', 'option', 'p', 'param', 'pre', 'samp', 'script', 'select', 'span', 'strong', 'style', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'ul', 'var'];
for (var i=0; i<elements.length; ++i) {
XHTML[elements[i]] = new Function('child', 'attributes', 'return XHTML.createElement("'+elements[i]+'", child, attributes);');
}
}
};
XHTML.init();
|
<html> <HEAD><TITLE>Untitled Document</TITLE> <META http-equiv=Content-Type content="text/html; charset=utf-8"> <SCRIPT type=text/javascript> var xHTML = function() { return { appendChild: function(node, child, attributes) { if (typeof child == 'string') { node.appendChild(document.createTextNode(child)); } else if ((typeof child == 'object') && (typeof child.nodeType != 'undefined')) { node.appendChild(child); } else if ((typeof child == 'object') && (child instanceof Array)) { for (var i=0; i<child.length; ++i) { xHTML.appendChild(node, child[i], attributes); } } else { node.appendChild(document.createTextNode('ERROR!')); } if (!!attributes && (typeof attributes == 'object')) { for (var a in attributes) { node.setAttribute(((a == 'className' && /*@cc_on!@*/true) ? 'class' : a), attributes[a]); } } return node; }, createElement: function(element, child, attributes) { var node = document.createElement(element); return xHTML.appendChild(node, child, attributes); }, init: function() { var elements = ['a', 'abbr', 'acronym', 'address', 'bdo', 'blockquote', 'br', 'button', 'caption', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'fieldset', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link', 'object', 'ol', 'optgroup', 'option', 'p', 'param', 'pre', 'samp', 'script', 'select', 'span', 'strong', 'style', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'tr', 'ul', 'var']; for (var i=0; i<elements.length; ++i) { xHTML[elements[i]] = new Function('child', 'attributes', 'return xHTML.createElement("'+elements[i]+'", child, attributes);'); } } }; }(); window.onload = function() { var heading1 = xHTML.createElement('h1', 'I Am Heading', {id:'heading1'}); xHTML.appendChild(heading1, xHTML.createElement('sup', 'Super!')); document.getElementsByTagName('body')[0].appendChild(heading1); }; </SCRIPT> </HEAD> <BODY> <H1 id=heading1>I Am Heading<SUP>Super!</SUP></H1></BODY> </html>var createNode = function(args) { var node, length, i; if (typeof(args) == "string") { node = document.createTextNode(args); } else if ( typeof(args) == "object" ) { node = document.createElement(args.tag); if (args.attributes) { for (i in args.attributes) { node.setAttribute(i, args.attributes[i]); } } if (args.style) { for (i in args.style) { node.style[i] = args.style[i]; } } if (args.children) { length = args.children.length, i = 0; for (; i<length; i++) { node.appendChild(createNode(args.children[i])); } } } return node; }; var heading2 = createNode({tag:'h2', attributes:{id:'heading2'}, children:['I Am Also Heading', {tag:'sup', children:[{tag:'em', children:['Yeah!']}]}]}); document.getElementsByTagName('body')[0].appendChild(heading2);Modified from http://spudly.shuoink.com/2008/02/20/using-the-xml-dom-without-writing-150000-lines-of-code/