Re: Simplifying element creation

On 7/10/11 7:51 AM, Sean Hogan wrote:
> On 7/10/11 1:20 AM, Julien Richard-Foy wrote:
>>> 3. How is generating a JS structure then calling Element.create(),
>>> node.append(), etc a win compared to generating a string of HTML then
>>> calling innerHTML, insertAdjacentHTML()?
>> It may be better for performance concerns, but it’s surely less error
>> prone. When appending strings you have to be careful to close tags at
>> the right place, to escape quotes, etc.
>> Using strings is far less composable than using a proper tree API.
>
> Either way, you write a function, test it, then you're done.
> Or, more likely, you use a function that someone else has already 
> written and is already in production.
>

On second thoughts, I'll concede that this can be a minor win for the 
JSON representation of the DOM:
1. If the script containing the JSON compiles this gives you some 
confidence that valid HTML will be generated.
2. If node.append(json), etc run without throwing this increases the 
confidence.

The same cannot be said when generating a string of HTML and calling 
innerHTML, etc.

Looking at the JSON DOM snippets I've seen and reflecting on the HTML 
snippets I've generated, I would doubt that the HTML approach takes 
significantly more development time (including validating the generated 
DOM).

NOTE: Where I write JSON above I actually mean a JS structure that can 
represent the DOM, not a JSON string.

Sean

Received on Friday, 7 October 2011 11:47:01 UTC