<?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: True Dynamic Element Object Creation</title>
	<atom:link href="http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/feed/" rel="self" type="application/rss+xml" />
	<link>http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/</link>
	<description>El blog de rolandog</description>
	<lastBuildDate>Thu, 29 Mar 2012 22:20:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Pervasive Smothering &#187; Blog Archive &#187; On Dynamic Elements Creation, Again</title>
		<link>http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/comment-page-1/#comment-362</link>
		<dc:creator>Pervasive Smothering &#187; Blog Archive &#187; On Dynamic Elements Creation, Again</dc:creator>
		<pubDate>Tue, 18 Jul 2006 23:27:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.rolandog.com/?p=401#comment-362</guid>
		<description>[...] But, thanks to the great and insightful comments of Matthias Miller, I came up with this interesting piece of code: [...]</description>
		<content:encoded><![CDATA[<p>[...] But, thanks to the great and insightful comments of Matthias Miller, I came up with this interesting piece of code: [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Miller</title>
		<link>http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/comment-page-1/#comment-361</link>
		<dc:creator>Matthias Miller</dc:creator>
		<pubDate>Thu, 23 Mar 2006 05:27:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.rolandog.com/?p=401#comment-361</guid>
		<description>First of all, a correction on my first comment. The last line should be:

&lt;blockquote&gt;mp3builder.attachTo(Delicious.Mp3.player);&lt;/blockquote&gt;

Just a tip on your onclick handler. Where you have:

&lt;blockquote&gt;
var link = crEl(”a”,”href”,”#”,”onclick”,alert(’Hello dudes’),”tn”,”Hello world.”);&lt;/blockquote&gt;

you are calling the alert function and passing its result into the function. Instead, you should pass in a reference to a function. You might do this:

&lt;blockquote&gt;
var link = crEl(”a”,”href”,”#”,”onclick”,function() { alert(’Hello dudes’) },”tn”,”Hello world.”);&lt;/blockquote&gt;

Or this (note that there are no parentheses after showAlert):

&lt;blockquote&gt;
function showAlert() {
   alert(’Hello dudes’);
}
var link = crEl(”a”,”href”,”#”,”onclick”,showAlert,”tn”,”Hello world.”);&lt;/blockquote&gt;

There is yet another approach that I considered presenting this morning. Essentially, instead of setting attributes, it sets properties on the JavaScript object just as you do for the onclick handler. For example:

&lt;blockquote&gt;
function crEl(tagName, properties) {
	function crE(tagName) { /*...*/ }

	function setObjectProperties(object, properties) {
		for (var propertyName in properties) {
			if (typeof properties[propertyName] === &quot;object&quot;) {
				// do a deep copy of objects; recurse here
				setObjectProperties(object[propertyName], properties[object]);
			}
			else if (tagName == &quot;param&quot;) {
				element[&quot;name&quot;] = propertyName;
				element[&quot;value&quot;] = properties[propertyName];
			}
			else {
				element[propertyName] = properties[propertyName];
			}
		}
	}

	tagName = tagName.toLowerCase();
	var element = crE(tagName); //temporary element
	setObjectProperties(object, properties);
	return element;
}
&lt;/blockquote&gt;

You might call it like this:

&lt;blockquote&gt;
function showAlert() {
   alert(’Hello dudes’);
}
var my_div = crElement(&quot;div&quot;, { onclick: showAlert, style: { backgroundColor: &quot;#FF0000&quot; } });
&lt;/blockquote&gt;

Let me know if more explanation would be helpful. I&#039;m interested in seeing what you come up with.</description>
		<content:encoded><![CDATA[<p>First of all, a correction on my first comment. The last line should be:</p>
<blockquote><p>mp3builder.attachTo(Delicious.Mp3.player);</p></blockquote>
<p>Just a tip on your onclick handler. Where you have:</p>
<blockquote><p>
var link = crEl(”a”,”href”,”#”,”onclick”,alert(’Hello dudes’),”tn”,”Hello world.”);</p></blockquote>
<p>you are calling the alert function and passing its result into the function. Instead, you should pass in a reference to a function. You might do this:</p>
<blockquote><p>
var link = crEl(”a”,”href”,”#”,”onclick”,function() { alert(’Hello dudes’) },”tn”,”Hello world.”);</p></blockquote>
<p>Or this (note that there are no parentheses after showAlert):</p>
<blockquote><p>
function showAlert() {<br />
   alert(’Hello dudes’);<br />
}<br />
var link = crEl(”a”,”href”,”#”,”onclick”,showAlert,”tn”,”Hello world.”);</p></blockquote>
<p>There is yet another approach that I considered presenting this morning. Essentially, instead of setting attributes, it sets properties on the JavaScript object just as you do for the onclick handler. For example:</p>
<blockquote><p>
function crEl(tagName, properties) {<br />
	function crE(tagName) { /*&#8230;*/ }</p>
<p>	function setObjectProperties(object, properties) {<br />
		for (var propertyName in properties) {<br />
			if (typeof properties[propertyName] === &#8220;object&#8221;) {<br />
				// do a deep copy of objects; recurse here<br />
				setObjectProperties(object[propertyName], properties[object]);<br />
			}<br />
			else if (tagName == &#8220;param&#8221;) {<br />
				element["name"] = propertyName;<br />
				element["value"] = properties[propertyName];<br />
			}<br />
			else {<br />
				element[propertyName] = properties[propertyName];<br />
			}<br />
		}<br />
	}</p>
<p>	tagName = tagName.toLowerCase();<br />
	var element = crE(tagName); //temporary element<br />
	setObjectProperties(object, properties);<br />
	return element;<br />
}
</p></blockquote>
<p>You might call it like this:</p>
<blockquote><p>
function showAlert() {<br />
   alert(’Hello dudes’);<br />
}<br />
var my_div = crElement(&#8220;div&#8221;, { onclick: showAlert, style: { backgroundColor: &#8220;#FF0000&#8243; } });
</p></blockquote>
<p>Let me know if more explanation would be helpful. I&#8217;m interested in seeing what you come up with.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rolandog</title>
		<link>http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/comment-page-1/#comment-360</link>
		<dc:creator>rolandog</dc:creator>
		<pubDate>Thu, 23 Mar 2006 01:02:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.rolandog.com/?p=401#comment-360</guid>
		<description>Hello Matthias, and thank you for your insightful comment. Your idea of the assembler sounds really intriguing, I&#039;ll give it a try and see what I can come up with...

I did spend some time before reading your comment expanding the function, so that it could accept objects and append an onclick event (this last one was unsuccesful).

Here is the formula I came up with: (dropped some conditions, and a for, and went with a while... but your object oriented arguments seem much better

&lt;blockquote&gt;
function crEl() {
	function cE(el) { //http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
		if (typeof(document.createElementNS) != &quot;undefined&quot;) {
			return document.createElementNS(&quot;http://www.w3.org/1999/xhtml&quot;, el);
		}
		if (typeof(document.createElement) != &quot;undefined&quot;) {
			return document.createElement(el);
		}
		return false;
	}
	var a = arguments; //arguments
	var nN = a[0].toLowerCase(); //nodeName
	if(nN == &quot;tn&quot;) { //creates textNode with first argument after &quot;tn&quot;
		var tN = crTN(a[1]);
		return tN;
	} else if(a.length &gt;= 1 &amp;&amp; typeof(a[0]) == &quot;string&quot;) {
		var tE = cE(nN); //temporary Element
		var strs = new Array(); //array to store strings and other stuff
		for(var b = 0; b &lt; a.length; ++b) {
			var tst = a[b]; //temporary storage
			if(typeof(tst) == &quot;object&quot;) {
				tE.appendChild(tst);
			} else {
				strs.push(tst); //these can actually be strings, numbers, functions... anything but objects
			}
		}
		a = strs; //new arguments... (after having appended objects, and removed them from the list)
	} else {
		return a[0]; //in case an object gets passed as the first argument
	}
	var al = a.length; //length of attribute and value pairs
	if(nN == &quot;param&quot;) { //this applies for the param element
		tE.setAttribute(&quot;name&quot;, a[1]);
		tE.setAttribute(&quot;value&quot;, a[2]);
	} else { //if the main element is not a param element...
		var s = 0; //how much to skip after parsing a method
		var l = 1;
		while(l&lt;al) {
			var m = a[l].toLowerCase();
			if(m == &quot;tn&quot;){ //this checks for a delcaration of a textNode
				var txN = crTN(a[l+1]); // creates a textNode
				tE.appendChild(txN); //appends the textNode
				s=2;
			} else if(m == &quot;param&quot; &amp;&amp; (a[l+2])) { //if there is a false attribute called &#039;param&#039;
				var tpm = cE(m); //creates the param
				tpm.setAttribute(&quot;name&quot;, a[l+1]); //param name and name value
				tpm.setAttribute(&quot;value&quot;,a[l+2]); //param value and value value :P
				tE.appendChild(tpm);
				s=3;
			} else if(m == &quot;onclick&quot;) {
				tE.onclick = a[l+1];
				s=2;
			} else {
				tE.setAttribute(a[l], a[l+1]);
				s=2;
			}
			l+=s;
		}
	}
	return tE; //returns the element
}

function crTN() { //creates a textNode from the first argument.
	var tT = arguments[0]; //temporary text
	var tTN = document.createTextNode(tT); //temporary text node
	return tTN;
}
&lt;/blockquote&gt;

I made some test cases for it (using another modified delicious playtagger)... I&#039;m having trouble with the onclick attribute, I think it might be a mistake of mine... Anyhow, I&#039;m looking for an argument called &#039;onclick&#039;, and I&#039;m inferring the next argument to be a function... so that I can then use the onclick method and pass it a function. Here is the content creation function.

&lt;blockquote&gt;
function cc() { //create content
var id0 = document.getElementById(&quot;image&quot;); //where I&#039;m going to place some of the nodes
var id1 = document.getElementById(&quot;text&quot;);
var id2 = document.getElementById(&quot;links&quot;);
//Here is an image
var img = crEl(&quot;img&quot;,&quot;src&quot;,&quot;http://rolandog.com/wp-content/r_g.png&quot;,&quot;alt&quot;,&quot;rolandog.com&quot;,&quot;width&quot;,&quot;80&quot;,&quot;height&quot;,&quot;15&quot;);
//The following test appends through three different methods an image... 
id0.appendChild(img); //works
document.getElementById(&quot;image&quot;).appendChild(img); //works
document.getElementById(&quot;image&quot;).appendChild(crEl(&quot;img&quot;,&quot;src&quot;,&quot;http://rolandog.com/wp-content/r_g.png&quot;,&quot;alt&quot;,&quot;rolandog.com&quot;,&quot;width&quot;,&quot;80&quot;,&quot;height&quot;,&quot;15&quot;)); //fails
//Now we&#039;re gonna try some methods of placing text.
var bld = crEl(&quot;strong&quot;,&quot;tn&quot;,&quot;I like toast... &quot;); //creating bold text
var par = crEl(&quot;p&quot;,&quot;tn&quot;,&quot;Mmm, toast.&quot;,bld) //a paragraph that should have a text node... if it was detected and that appends the bold text
//Now I&#039;ll append it
id1.appendChild(par);
//Here is the source of a document and it will be displayed as text/plain
var src = crEl(&quot;object&quot;,&quot;type&quot;,&quot;text/plain&quot;,&quot;data&quot;,&quot;http://rolandog.com/sounds/playtagger.html&quot;); //source for the playtagger.html
//now I&#039;ll place it as a child in the id1.
id1.appendChild(src); //maybe I can create fractal documents??
//Now for the links...
var link = crEl(&quot;a&quot;,&quot;href&quot;,&quot;#&quot;,&quot;onclick&quot;,alert(&#039;Hello dudes&#039;),&quot;tn&quot;,&quot;Hello world.&quot;);
id2.appendChild(link); //fails in IE... but it does work... the onclick attribute requires its value to be a function
}
&lt;/blockquote&gt;

I did however upload the tests with my latests modifications for object creation in &lt;a href=&quot;http://rolandog.com/sounds/tests.xhtml&quot; rel=&quot;nofollow&quot;&gt;xhtml&lt;/a&gt; and &lt;a href=&quot;http://rolandog.com/sounds/tests.html&quot; rel=&quot;nofollow&quot;&gt;html&lt;/a&gt;...

Though I think I&#039;ll head in the direction you&#039;ve pointed. I&#039;m currently in my polymer chemistry lab, but when I get home I&#039;ll give your functions a try... they seem much better. I dunno if I have to leave a statement to declare my code as open source or GPL, but yeah... it &lt;em&gt;is&lt;/em&gt; free, and anyone that stops by can pitch in and &#039;steal&#039; it or improve it.</description>
		<content:encoded><![CDATA[<p>Hello Matthias, and thank you for your insightful comment. Your idea of the assembler sounds really intriguing, I&#8217;ll give it a try and see what I can come up with&#8230;</p>
<p>I did spend some time before reading your comment expanding the function, so that it could accept objects and append an onclick event (this last one was unsuccesful).</p>
<p>Here is the formula I came up with: (dropped some conditions, and a for, and went with a while&#8230; but your object oriented arguments seem much better</p>
<blockquote><p>
function crEl() {<br />
	function cE(el) { //http://simon.incutio.com/archive/2003/06/15/javascriptWithXML<br />
		if (typeof(document.createElementNS) != &#8220;undefined&#8221;) {<br />
			return document.createElementNS(&#8220;http://www.w3.org/1999/xhtml&#8221;, el);<br />
		}<br />
		if (typeof(document.createElement) != &#8220;undefined&#8221;) {<br />
			return document.createElement(el);<br />
		}<br />
		return false;<br />
	}<br />
	var a = arguments; //arguments<br />
	var nN = a[0].toLowerCase(); //nodeName<br />
	if(nN == &#8220;tn&#8221;) { //creates textNode with first argument after &#8220;tn&#8221;<br />
		var tN = crTN(a[1]);<br />
		return tN;<br />
	} else if(a.length &gt;= 1 &#038;&#038; typeof(a[0]) == &#8220;string&#8221;) {<br />
		var tE = cE(nN); //temporary Element<br />
		var strs = new Array(); //array to store strings and other stuff<br />
		for(var b = 0; b &lt; a.length; ++b) {<br />
			var tst = a[b]; //temporary storage<br />
			if(typeof(tst) == &#8220;object&#8221;) {<br />
				tE.appendChild(tst);<br />
			} else {<br />
				strs.push(tst); //these can actually be strings, numbers, functions&#8230; anything but objects<br />
			}<br />
		}<br />
		a = strs; //new arguments&#8230; (after having appended objects, and removed them from the list)<br />
	} else {<br />
		return a[0]; //in case an object gets passed as the first argument<br />
	}<br />
	var al = a.length; //length of attribute and value pairs<br />
	if(nN == &#8220;param&#8221;) { //this applies for the param element<br />
		tE.setAttribute(&#8220;name&#8221;, a[1]);<br />
		tE.setAttribute(&#8220;value&#8221;, a[2]);<br />
	} else { //if the main element is not a param element&#8230;<br />
		var s = 0; //how much to skip after parsing a method<br />
		var l = 1;<br />
		while(l&lt;al) {<br />
			var m = a[l].toLowerCase();<br />
			if(m == &#8220;tn&#8221;){ //this checks for a delcaration of a textNode<br />
				var txN = crTN(a[l+1]); // creates a textNode<br />
				tE.appendChild(txN); //appends the textNode<br />
				s=2;<br />
			} else if(m == &#8220;param&#8221; &#038;&#038; (a[l+2])) { //if there is a false attribute called &#8216;param&#8217;<br />
				var tpm = cE(m); //creates the param<br />
				tpm.setAttribute(&#8220;name&#8221;, a[l+1]); //param name and name value<br />
				tpm.setAttribute(&#8220;value&#8221;,a[l+2]); //param value and value value <img src='http://rolandog.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br />
				tE.appendChild(tpm);<br />
				s=3;<br />
			} else if(m == &#8220;onclick&#8221;) {<br />
				tE.onclick = a[l+1];<br />
				s=2;<br />
			} else {<br />
				tE.setAttribute(a[l], a[l+1]);<br />
				s=2;<br />
			}<br />
			l+=s;<br />
		}<br />
	}<br />
	return tE; //returns the element<br />
}</p>
<p>function crTN() { //creates a textNode from the first argument.<br />
	var tT = arguments[0]; //temporary text<br />
	var tTN = document.createTextNode(tT); //temporary text node<br />
	return tTN;<br />
}
</p></blockquote>
<p>I made some test cases for it (using another modified delicious playtagger)&#8230; I&#8217;m having trouble with the onclick attribute, I think it might be a mistake of mine&#8230; Anyhow, I&#8217;m looking for an argument called &#8216;onclick&#8217;, and I&#8217;m inferring the next argument to be a function&#8230; so that I can then use the onclick method and pass it a function. Here is the content creation function.</p>
<blockquote><p>
function cc() { //create content<br />
var id0 = document.getElementById(&#8220;image&#8221;); //where I&#8217;m going to place some of the nodes<br />
var id1 = document.getElementById(&#8220;text&#8221;);<br />
var id2 = document.getElementById(&#8220;links&#8221;);<br />
//Here is an image<br />
var img = crEl(&#8220;img&#8221;,&#8221;src&#8221;,&#8221;http://rolandog.com/wp-content/r_g.png&#8221;,&#8221;alt&#8221;,&#8221;rolandog.com&#8221;,&#8221;width&#8221;,&#8221;80&#8243;,&#8221;height&#8221;,&#8221;15&#8243;);<br />
//The following test appends through three different methods an image&#8230;<br />
id0.appendChild(img); //works<br />
document.getElementById(&#8220;image&#8221;).appendChild(img); //works<br />
document.getElementById(&#8220;image&#8221;).appendChild(crEl(&#8220;img&#8221;,&#8221;src&#8221;,&#8221;http://rolandog.com/wp-content/r_g.png&#8221;,&#8221;alt&#8221;,&#8221;rolandog.com&#8221;,&#8221;width&#8221;,&#8221;80&#8243;,&#8221;height&#8221;,&#8221;15&#8243;)); //fails<br />
//Now we&#8217;re gonna try some methods of placing text.<br />
var bld = crEl(&#8220;strong&#8221;,&#8221;tn&#8221;,&#8221;I like toast&#8230; &#8220;); //creating bold text<br />
var par = crEl(&#8220;p&#8221;,&#8221;tn&#8221;,&#8221;Mmm, toast.&#8221;,bld) //a paragraph that should have a text node&#8230; if it was detected and that appends the bold text<br />
//Now I&#8217;ll append it<br />
id1.appendChild(par);<br />
//Here is the source of a document and it will be displayed as text/plain<br />
var src = crEl(&#8220;object&#8221;,&#8221;type&#8221;,&#8221;text/plain&#8221;,&#8221;data&#8221;,&#8221;http://rolandog.com/sounds/playtagger.html&#8221;); //source for the playtagger.html<br />
//now I&#8217;ll place it as a child in the id1.<br />
id1.appendChild(src); //maybe I can create fractal documents??<br />
//Now for the links&#8230;<br />
var link = crEl(&#8220;a&#8221;,&#8221;href&#8221;,&#8221;#&#8221;,&#8221;onclick&#8221;,alert(&#8216;Hello dudes&#8217;),&#8221;tn&#8221;,&#8221;Hello world.&#8221;);<br />
id2.appendChild(link); //fails in IE&#8230; but it does work&#8230; the onclick attribute requires its value to be a function<br />
}
</p></blockquote>
<p>I did however upload the tests with my latests modifications for object creation in <a href="http://rolandog.com/sounds/tests.xhtml" rel="nofollow">xhtml</a> and <a href="http://rolandog.com/sounds/tests.html" rel="nofollow">html</a>&#8230;</p>
<p>Though I think I&#8217;ll head in the direction you&#8217;ve pointed. I&#8217;m currently in my polymer chemistry lab, but when I get home I&#8217;ll give your functions a try&#8230; they seem much better. I dunno if I have to leave a statement to declare my code as open source or GPL, but yeah&#8230; it <em>is</em> free, and anyone that stops by can pitch in and &#8216;steal&#8217; it or improve it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Miller</title>
		<link>http://rolandog.com/archives/2006/03/21/true-dynamic-element-object-creation/comment-page-1/#comment-359</link>
		<dc:creator>Matthias Miller</dc:creator>
		<pubDate>Wed, 22 Mar 2006 14:57:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.rolandog.com/?p=401#comment-359</guid>
		<description>I was reading your post and had a few ideas how you might improve the function. 

First of all, you could pass in attribute values as an object, which lets you have natural name/value pairing, such as:

&lt;blockquote&gt;function crEl(tagName, attributes) {
   tagName = tagName.toLowerCase();
   var element = document.createElement(tagName); //temporary element
   for (var property in attributes) {
      if (tagName == &quot;param&quot;) {
         element.setAttribute(&quot;name&quot;, property);
         element.setAttribute(&quot;value&quot;, attributes[property]);
      }
      else {
         element.setAttribute(property, attributes[property]);
      }
   }
   return element;
}

var div = crEl(&quot;div&quot;, { id: &quot;mydiv&quot;, style: &quot;background-color: #ffffff;&quot; });
&lt;/blockquote&gt;

If you&#039;re interested in a sophisticated element assembler, you could create an ElementBuilder object. Since I&#039;m on the topic anyway, let me try:

&lt;blockquote&gt;
function ElementBuilder() {
	// maintain a stack of elements to push/pop
	this._elementStack = [document.createDocumentFragment()];
}

ElementBuilder.prototype.pushEl = function(tagName, attributes) {
	var element = crEl(tagName, attributes);
	this._elementStack[this._elementStack.length-1].appendChild(element);
	this._elementStack.push(element);
	return element;
};

ElementBuilder.prototype.addText = function(text) {
	var textNode = document.createTextNode(text);
	this._elementStack[this._elementStack.length-1].appendChild(textNode);
};

ElementBuilder.prototype.pushAndPopEl = function(tagName, attributes) {
	this.pushEl(tagName, attributes);
	this.popEl();
};

ElementBuilder.prototype.popEl = function() {
	if (this._elementStack.length &gt; 1) {
		this._elementStack.pop();
	}
};

ElementBuilder.attach = function(parent) {
	parent.appendChild(this._elementStack[0]);
};
&lt;/blockquote&gt;

You would use it like this:

&lt;blockquote&gt;
var mp3builder = new ElementBuilder();
mp3builder.pushEl(&quot;object&quot;, {type:&quot;application/x-shockwave-flash&quot;,data:&quot;http://del.icio.us/static/swf/playtagger.swf&quot;,style:&quot;vertical-align:bottom;&quot;,width:&quot;100&quot;,height:&quot;14&quot;,id:&quot;player&quot;);
mp3builder.pushAndPopEl(&quot;param&quot;,{allowScriptAccess:&quot;sameDomain&quot;});
mp3builder.pushAndPopEl(&quot;param&quot;,{flashVars:&quot;theLink=&quot;+url});
mp3builder.pushAndPopEl(&quot;param&quot;,{movie:&quot;http://del.icio.us/static/swf/playtagger.swf&quot;});
mp3builder.pushAndPopEl(&quot;param&quot;,{quality:&quot;high&quot;});
mp3builder.pushAndPopEl(&quot;param&quot;,{bgcolor:&quot;#ffffff&quot;});
mp3builder.pushAndPopEl(&quot;param&quot;,{wmode:&quot;transparent&quot;});
mp3builder.popEl();

Delicious.Mp3.player = crEl(&quot;span&quot;, {id: &quot;delicious&quot;});
ElementBuilder.attachTo(Delicious.Mp3.player);
&lt;/blockquote&gt;

I apologize for any errors. Feel free to edit this comment for formatting and coding errors.</description>
		<content:encoded><![CDATA[<p>I was reading your post and had a few ideas how you might improve the function. </p>
<p>First of all, you could pass in attribute values as an object, which lets you have natural name/value pairing, such as:</p>
<blockquote><p>function crEl(tagName, attributes) {<br />
   tagName = tagName.toLowerCase();<br />
   var element = document.createElement(tagName); //temporary element<br />
   for (var property in attributes) {<br />
      if (tagName == &#8220;param&#8221;) {<br />
         element.setAttribute(&#8220;name&#8221;, property);<br />
         element.setAttribute(&#8220;value&#8221;, attributes[property]);<br />
      }<br />
      else {<br />
         element.setAttribute(property, attributes[property]);<br />
      }<br />
   }<br />
   return element;<br />
}</p>
<p>var div = crEl(&#8220;div&#8221;, { id: &#8220;mydiv&#8221;, style: &#8220;background-color: #ffffff;&#8221; });
</p></blockquote>
<p>If you&#8217;re interested in a sophisticated element assembler, you could create an ElementBuilder object. Since I&#8217;m on the topic anyway, let me try:</p>
<blockquote><p>
function ElementBuilder() {<br />
	// maintain a stack of elements to push/pop<br />
	this._elementStack = [document.createDocumentFragment()];<br />
}</p>
<p>ElementBuilder.prototype.pushEl = function(tagName, attributes) {<br />
	var element = crEl(tagName, attributes);<br />
	this._elementStack[this._elementStack.length-1].appendChild(element);<br />
	this._elementStack.push(element);<br />
	return element;<br />
};</p>
<p>ElementBuilder.prototype.addText = function(text) {<br />
	var textNode = document.createTextNode(text);<br />
	this._elementStack[this._elementStack.length-1].appendChild(textNode);<br />
};</p>
<p>ElementBuilder.prototype.pushAndPopEl = function(tagName, attributes) {<br />
	this.pushEl(tagName, attributes);<br />
	this.popEl();<br />
};</p>
<p>ElementBuilder.prototype.popEl = function() {<br />
	if (this._elementStack.length &gt; 1) {<br />
		this._elementStack.pop();<br />
	}<br />
};</p>
<p>ElementBuilder.attach = function(parent) {<br />
	parent.appendChild(this._elementStack[0]);<br />
};
</p></blockquote>
<p>You would use it like this:</p>
<blockquote><p>
var mp3builder = new ElementBuilder();<br />
mp3builder.pushEl(&#8220;object&#8221;, {type:&#8221;application/x-shockwave-flash&#8221;,data:&#8221;http://del.icio.us/static/swf/playtagger.swf&#8221;,style:&#8221;vertical-align:bottom;&#8221;,width:&#8221;100&#8243;,height:&#8221;14&#8243;,id:&#8221;player&#8221;);<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{allowScriptAccess:&#8221;sameDomain&#8221;});<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{flashVars:&#8221;theLink=&#8221;+url});<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{movie:&#8221;http://del.icio.us/static/swf/playtagger.swf&#8221;});<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{quality:&#8221;high&#8221;});<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{bgcolor:&#8221;#ffffff&#8221;});<br />
mp3builder.pushAndPopEl(&#8220;param&#8221;,{wmode:&#8221;transparent&#8221;});<br />
mp3builder.popEl();</p>
<p>Delicious.Mp3.player = crEl(&#8220;span&#8221;, {id: &#8220;delicious&#8221;});<br />
ElementBuilder.attachTo(Delicious.Mp3.player);
</p></blockquote>
<p>I apologize for any errors. Feel free to edit this comment for formatting and coding errors.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

