<?xml version="1.0" encoding="UTF-8"?><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"
	>
<channel>
	<title>Comments on: Is invalid code created through JavaScript ok?</title>
	<atom:link href="http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/</link>
	<description>Web development and Internet trends</description>
	<pubDate>Fri, 05 Dec 2008 11:06:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42606</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Thu, 15 Mar 2007 08:11:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42606</guid>
		<description>Andy,

Definitely, it's all about context.

Goulven,

Most definitely, if manipulating the DOM by adding new elements, make sure it's always properly structured.</description>
		<content:encoded><![CDATA[<p>Andy,</p>
<p>Definitely, it&#8217;s all about context.</p>
<p>Goulven,</p>
<p>Most definitely, if manipulating the <acronym title="Document Object Model">DOM</acronym> by adding new elements, make sure it&#8217;s always properly structured.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Goulven</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42515</link>
		<dc:creator>Goulven</dc:creator>
		<pubDate>Wed, 14 Mar 2007 20:48:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42515</guid>
		<description>Watch out for illegal DOM manipulations though. From personal experience, invalid code can sometimes behave unexpectedly.

I once tried to append TRs to a TABLE unsuccessfully -because a TR only belongs in a THEAD, TFOOT or TBODY.

Just my 2c...</description>
		<content:encoded><![CDATA[<p>Watch out for illegal <acronym title="Document Object Model">DOM</acronym> manipulations though. From personal experience, invalid code can sometimes behave unexpectedly.</p>
<p>I once tried to append TRs to a TABLE unsuccessfully -because a TR only belongs in a THEAD, TFOOT or TBODY.</p>
<p>Just my 2c&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42334</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Tue, 13 Mar 2007 21:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42334</guid>
		<description>Well, to me it's a question of compliance. I tend to follow the standards in first hand but in some cases other methods seem more feasible (I'm not necessarily speaking of HTML here). Depending on the task, i tend to take different actions. Say, inserting content from a remote webpage, .innerHTML is the way to go. When adding elements that's to be manipulated later, I'll use the DOM construction.</description>
		<content:encoded><![CDATA[<p>Well, to me it&#8217;s a question of compliance. I tend to follow the standards in first hand but in some cases other methods seem more feasible (I&#8217;m not necessarily speaking of <acronym title="HyperText Markup Language">HTML</acronym> here). Depending on the task, i tend to take different actions. Say, inserting content from a remote webpage, .innerHTML is the way to go. When adding elements that&#8217;s to be manipulated later, I&#8217;ll use the <acronym title="Document Object Model">DOM</acronym> construction.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42243</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 13 Mar 2007 09:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42243</guid>
		<description>Ok, my old tired eyes got some help, and the wrong semicolons should have been removed. Please let me know if I missed some... :-)</description>
		<content:encoded><![CDATA[<p>Ok, my old tired eyes got some help, and the wrong semicolons should have been removed. Please let me know if I missed some&#8230; <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42228</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 13 Mar 2007 07:56:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42228</guid>
		<description>Wow, great responses!

Sebastian,

That's actually spot on. If we look beyond IE's implementation, it's two completely different actions. Very well put.

And, I'm nor sure if I have a seeing problem, but I can't find the stray semicolons. Would you be nice and give me a hint? :-)

Jens,

Well, to me, it's about structure and performance. Valid code should never hinder us, but help us write good code. If a solution is the best, but invalid, I'd go for it any day.

Morgan,

Oh, definitely. It depends on the context, and object packaging is very often a very nice and good way to go.</description>
		<content:encoded><![CDATA[<p>Wow, great responses!</p>
<p>Sebastian,</p>
<p>That&#8217;s actually spot on. If we look beyond <acronym title="Internet Explorer">IE</acronym>&#8217;s implementation, it&#8217;s two completely different actions. Very well put.</p>
<p>And, I&#8217;m nor sure if I have a seeing problem, but I can&#8217;t find the stray semicolons. Would you be nice and give me a hint? <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Jens,</p>
<p>Well, to me, it&#8217;s about structure and performance. Valid code should never hinder us, but help us write good code. If a solution is the best, but invalid, I&#8217;d go for it any day.</p>
<p>Morgan,</p>
<p>Oh, definitely. It depends on the context, and object packaging is very often a very nice and good way to go.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tanny O'Haley</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42176</link>
		<dc:creator>Tanny O'Haley</dc:creator>
		<pubDate>Tue, 13 Mar 2007 00:27:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42176</guid>
		<description>I always use option 4. You are adding an object to the DOM element, not an attribute, therefore it is not invalid.</description>
		<content:encoded><![CDATA[<p>I always use option 4. You are adding an object to the <acronym title="Document Object Model">DOM</acronym> element, not an attribute, therefore it is not invalid.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan Van Reeth</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42160</link>
		<dc:creator>Stefan Van Reeth</dc:creator>
		<pubDate>Mon, 12 Mar 2007 22:13:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42160</guid>
		<description>Hi Robert,

Sebastian gava a correct reply, so I won't repeat that. On the other hand, I do firmly agree with your viewpoint that option 4 is the best practice. Altough I tend to use option 3 regulary too. But I can't say I have a fixed rule for this: it's more a decision based on what feels good depending on the situation.
About the semicolons: copy/paste can be a real bitch in cases like this, no ;)? Easely missed, and I've done far worse before...</description>
		<content:encoded><![CDATA[<p>Hi Robert,</p>
<p>Sebastian gava a correct reply, so I won&#8217;t repeat that. On the other hand, I do firmly agree with your viewpoint that option 4 is the best practice. Altough I tend to use option 3 regulary too. But I can&#8217;t say I have a fixed rule for this: it&#8217;s more a decision based on what feels good depending on the situation.<br />
About the semicolons: copy/paste can be a real bitch in cases like this, no ;)? Easely missed, and I&#8217;ve done far worse before&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Bassett</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42153</link>
		<dc:creator>Aaron Bassett</dc:creator>
		<pubDate>Mon, 12 Mar 2007 22:00:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42153</guid>
		<description>As other people have pointed out already there is nothing 'invalid' about it. But if you were really concerned you could add the attributes into a custom DTD and reference it in your doctype.

Ok so it won't be valid XHTML 1.0 Strict, it'll be valid something else. But hey valid is valid ;)</description>
		<content:encoded><![CDATA[<p>As other people have pointed out already there is nothing &#8216;invalid&#8217; about it. But if you were really concerned you could add the attributes into a custom <acronym title="Document Type Definition">DTD</acronym> and reference it in your doctype.</p>
<p>Ok so it won&#8217;t be valid <acronym title="eXtensible HyperText Markup Language - HTML reformulated as XML">XHTML</acronym> 1.0 Strict, it&#8217;ll be valid something else. But hey valid is valid <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Morgan Roderick</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42150</link>
		<dc:creator>Morgan Roderick</dc:creator>
		<pubDate>Mon, 12 Mar 2007 21:31:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42150</guid>
		<description>Hmm, for the most part, i try to avoid relying on the DOM to do my bidding, preferring objects to package stuff.
&lt;code&gt;
myobject = { 
    myItems = [];
}
&lt;/code&gt;
This does add more code, but allows me to keep cached references to nodes and collections, and wraps most of my functionality in a way that doesn't usually class with third-party code (or even code I write myself).

Inspired by the work of Christian Heilman (&lt;a href="http://icant.co.uk/sandbox/eventdelegation/" rel="nofollow"&gt;http://icant.co.uk/sandbox/eventdelegation/&lt;/a&gt;) , I've done some work on a recent project, using event delegates instead of event listeners. This works very well, especially for generated / ajaxed content, where you don't really want to clutter it up with inline event listeners, and we all know what a pain IE can be, when trying to manipulate DOM with content inserted with innerHTML (which is another MS innovation, that's off the plantation, but very handy).

Ironically enough, MS created innerHTML and yet IE has the poorest implementation.

So, I guess my approach goes kindof between the options outlined, since I try to keep references to ancestor elements, and have them deal with the events, instead of repeating the same event listeners over and over and over again. This scales really well, and Chris, if you're listening, GREAT WORK!

You really inspired me (see Mommy, I am not harsh all the time ;-) )</description>
		<content:encoded><![CDATA[<p>Hmm, for the most part, i try to avoid relying on the <acronym title="Document Object Model">DOM</acronym> to do my bidding, preferring objects to package stuff.<br />
<code><br />
myobject = {<br />
    myItems = [];<br />
}<br />
</code><br />
This does add more code, but allows me to keep cached references to nodes and collections, and wraps most of my functionality in a way that doesn&#8217;t usually class with third-party code (or even code I write myself).</p>
<p>Inspired by the work of Christian Heilman (<a href="http://icant.co.uk/sandbox/eventdelegation/" rel="nofollow">http://icant.co.uk/sandbox/eventdelegation/</a>) , I&#8217;ve done some work on a recent project, using event delegates instead of event listeners. This works very well, especially for generated / ajaxed content, where you don&#8217;t really want to clutter it up with inline event listeners, and we all know what a pain <acronym title="Internet Explorer">IE</acronym> can be, when trying to manipulate <acronym title="Document Object Model">DOM</acronym> with content inserted with innerHTML (which is another <acronym title="Microsoft">MS</acronym> innovation, that&#8217;s off the plantation, but very handy).</p>
<p>Ironically enough, <acronym title="Microsoft">MS</acronym> created innerHTML and yet <acronym title="Internet Explorer">IE</acronym> has the poorest implementation.</p>
<p>So, I guess my approach goes kindof between the options outlined, since I try to keep references to ancestor elements, and have them deal with the events, instead of repeating the same event listeners over and over and over again. This scales really well, and Chris, if you&#8217;re listening, GREAT WORK!</p>
<p>You really inspired me (see Mommy, I am not harsh all the time <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Johansson</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42135</link>
		<dc:creator>Roger Johansson</dc:creator>
		<pubDate>Mon, 12 Mar 2007 19:40:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42135</guid>
		<description>What others have already said: option 4 does not create invalid HTML. Had you used setAttribute(), however... ;).</description>
		<content:encoded><![CDATA[<p>What others have already said: option 4 does not create invalid <acronym title="HyperText Markup Language">HTML</acronym>. Had you used setAttribute(), however&#8230; ;).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Meiert</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42134</link>
		<dc:creator>Jens Meiert</dc:creator>
		<pubDate>Mon, 12 Mar 2007 19:33:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42134</guid>
		<description>Sorry - &lt;em&gt;no&lt;/em&gt;, why should it? (It's yet one of the reasons why I &lt;a href="http://meiert.com/de/publications/articles/20050513/#sifr" rel="nofollow"&gt;dissuade from using sIFR&lt;/a&gt;.)</description>
		<content:encoded><![CDATA[<p>Sorry - <em>no</em>, why should it? (It&#8217;s yet one of the reasons why I <a href="http://meiert.com/de/publications/articles/20050513/#sifr" rel="nofollow">dissuade from using sIFR</a>.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remy Sharp</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42130</link>
		<dc:creator>Remy Sharp</dc:creator>
		<pubDate>Mon, 12 Mar 2007 18:53:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42130</guid>
		<description>I would, and have been going with option 4.  

My view is that I'm not touching the parts DOM that affect the rendered output of the page, or the HTML that's used to validate the page.  I'm simply extending the DOM.

Similarly, browsers extend the DOM in their own way.  If you take a look at any INPUT element using &lt;a href="http://getfirebug.com" rel="nofollow"&gt;Firebug&lt;/a&gt; and Firefox 2 and you'll see the 'spellcheck' property attached.</description>
		<content:encoded><![CDATA[<p>I would, and have been going with option 4.  </p>
<p>My view is that I&#8217;m not touching the parts <acronym title="Document Object Model">DOM</acronym> that affect the rendered output of the page, or the <acronym title="HyperText Markup Language">HTML</acronym> that&#8217;s used to validate the page.  I&#8217;m simply extending the <acronym title="Document Object Model">DOM</acronym>.</p>
<p>Similarly, browsers extend the <acronym title="Document Object Model">DOM</acronym> in their own way.  If you take a look at any INPUT element using <a href="http://getfirebug.com" rel="nofollow">Firebug</a> and Firefox 2 and you&#8217;ll see the &#8217;spellcheck&#8217; property attached.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cheat Codes &#187; Is invalid code created through JavaScript ok?</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42107</link>
		<dc:creator>Cheat Codes &#187; Is invalid code created through JavaScript ok?</dc:creator>
		<pubDate>Mon, 12 Mar 2007 16:25:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42107</guid>
		<description>[...]  JavaScript. By doing some DOM manipulation, you can create great &#8230; Original post by Robert Nyman and plugin by Elliott Back 			 			 			 [...]</description>
		<content:encoded><![CDATA[<p>[...]  JavaScript. By doing some <acronym title="Document Object Model">DOM</acronym> manipulation, you can create great &#8230; Original post by Robert Nyman and plugin by Elliott Back 			 			 			 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Proctor</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42104</link>
		<dc:creator>Simon Proctor</dc:creator>
		<pubDate>Mon, 12 Mar 2007 16:14:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42104</guid>
		<description>See the way I look at it this is about extending a Javascript Object that just happens to be a DOM Node too. I'd go for your last option every time because it makes sense from a Javascript point of view. It has to be said I hadn't thought about the fact your new attributes aren't valid because if you were do look at the generated code they wouldn't display (being invalid).

I'm not explaining myself very well I guess but basically while I don't like new random attributes being added to HTML pages I see no problem in extending any arbitrary Javascript Object as required.</description>
		<content:encoded><![CDATA[<p>See the way I look at it this is about extending a Javascript Object that just happens to be a <acronym title="Document Object Model">DOM</acronym> Node too. I&#8217;d go for your last option every time because it makes sense from a Javascript point of view. It has to be said I hadn&#8217;t thought about the fact your new attributes aren&#8217;t valid because if you were do look at the generated code they wouldn&#8217;t display (being invalid).</p>
<p>I&#8217;m not explaining myself very well I guess but basically while I don&#8217;t like new random attributes being added to <acronym title="HyperText Markup Language">HTML</acronym> pages I see no problem in extending any arbitrary Javascript Object as required.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sebastian Redl</title>
		<link>http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42103</link>
		<dc:creator>Sebastian Redl</dc:creator>
		<pubDate>Mon, 12 Mar 2007 16:12:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2007/03/12/is-invalid-code-created-through-javascript-ok/#comment-42103</guid>
		<description>There's nothing invalid about option 4. There is a difference between HTML attributes, which are strictly defined, and properties of the DOM node, which are not strictly defined.

Let's look at the three standards concerned: HTML, DOM (Core and HTML) and ECMAScript.

From the viewpoint of HTML, adding an attribute that is not specified is an error.

From the viewpoint of ECMAScript, an object is completely dynamic, and you can add whatever properties to that you want.

From the viewpoint of the DOM, there are some predefined properties that map directly to HTML attributes. Setting these properties also sets the corresponding attributes, and if the attribute changes through any other means, the property changes as well. However, the DOM makes no statement at all about properties that it doesn't define, which means that JavaScript should fall back to simple ECMAScript behaviour.

In other words, setting an arbitrary property of a DOM node does not mean that you set any attribute of the element. In particular, calling linkElm.getAttribute("elementsToExpand") in your code above should return null.

No setting of element attributes -&#62; no possible violation of the HTML standard -&#62; no invalid code. q.e.d.

The only exception is Internet Explorer, which doesn't understand the difference between element attributes and node properties. But worrying about code doing invalid things when processed under invalid rules is stupid.


On a side note, you have a lot of stray semicolons in your code.</description>
		<content:encoded><![CDATA[<p>There&#8217;s nothing invalid about option 4. There is a difference between <acronym title="HyperText Markup Language">HTML</acronym> attributes, which are strictly defined, and properties of the <acronym title="Document Object Model">DOM</acronym> node, which are not strictly defined.</p>
<p>Let&#8217;s look at the three standards concerned: <acronym title="HyperText Markup Language">HTML</acronym>, <acronym title="Document Object Model">DOM</acronym> (Core and <acronym title="HyperText Markup Language">HTML</acronym>) and ECMAScript.</p>
<p>From the viewpoint of <acronym title="HyperText Markup Language">HTML</acronym>, adding an attribute that is not specified is an error.</p>
<p>From the viewpoint of ECMAScript, an object is completely dynamic, and you can add whatever properties to that you want.</p>
<p>From the viewpoint of the <acronym title="Document Object Model">DOM</acronym>, there are some predefined properties that map directly to <acronym title="HyperText Markup Language">HTML</acronym> attributes. Setting these properties also sets the corresponding attributes, and if the attribute changes through any other means, the property changes as well. However, the <acronym title="Document Object Model">DOM</acronym> makes no statement at all about properties that it doesn&#8217;t define, which means that JavaScript should fall back to simple ECMAScript behaviour.</p>
<p>In other words, setting an arbitrary property of a <acronym title="Document Object Model">DOM</acronym> node does not mean that you set any attribute of the element. In particular, calling linkElm.getAttribute(&#8221;elementsToExpand&#8221;) in your code above should return null.</p>
<p>No setting of element attributes -&gt; no possible violation of the <acronym title="HyperText Markup Language">HTML</acronym> standard -&gt; no invalid code. q.e.d.</p>
<p>The only exception is Internet Explorer, which doesn&#8217;t understand the difference between element attributes and node properties. But worrying about code doing invalid things when processed under invalid rules is stupid.</p>
<p>On a side note, you have a lot of stray semicolons in your code.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
