Re: E4H

Le 25/10/2011 11:07, Anne van Kesteren a écrit :
> So I think what is needed to make the DOM drastically easier to use is
> an extension to ECMAScript specific to implementations of the
> Window-object where the literals translate automatically to DOM
> objects. This extension is ideally simpler than E4X and does not have
> to support all of the DOM. Basically elements, attributes, and
> descendant text nodes are what is important here I think.
>
> E.g. to create a hyperlink and append it to a document all you would
> have to do is:
>
>   var link = <a href="http://example.org/">Example Organization</a>
>   document.body.appendChild(link)
It doesn't work to insert a script:

Let's try as inline script:
-----
<html>
    <head>
        <script src="bla.js">
        var s = <script src="bla.js"></script>
        document.body.appendChild(s);
        </script>
    </head>
</html>
-----
Oops?

HTML in JavaScript inlined as text in an HTML element doesn't sound like
a good idea to me.

I think I read something a while ago about adding .innerHTML to
documentFragment. It seems to fit the need of writing something that is
HTML-like (in a JavaScript string!) and conveninent to use:

----
var d = document.createDocumentFragment();
var link = '<a href="http://example.org/">Example Organization</a>';
d.innerHTML = link;
document.body.appendChild(d);
----
It just requires 2 more lines than your example (one to create the
documentFragment, the other for innerHTML).

David

Received on Tuesday, 25 October 2011 09:19:39 UTC