Javascript – Issues with most basic e4x test

e4xfirefoxjavascript

When I load a page containing e4x in FF 3.5, I get no inkling that e4x even exists in the browser's JS implementation. Notes below, but here's my HTML :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>e4x test</title>
    <script type="text/javascript" src="lib/dojo/dojo/dojo.js">
    </script>
    <script type="text/javascript;e4x=1">
        function hello() {
            var x = new XML();
            x = <foo></foo>
            dojo.byId("container").innerHTML = "Print me!" + x.toXMLString();
        }
    </script>
    <script type="text/javascript">
        dojo.addOnLoad(hello);
    </script>
</head>
<body>
<div id="container">
</div>
</body>
</html>

When I inspect in firebug, it says x doesn't have a toString() method, and my IDE (aptana) thinks that XML is not an object type. Does anyone have any idea what I'm doing wrong?

Best Answer

I'm guessing that it was working all along, but your browser doesn't recognize a "foo" tag and because it does not know how to render it, it ignores it. By putting something inside of your foo tag you would get content out.

BTW: The new XML() statement is entirely unnecessary. You can just do this:

var x = <foo>bar</foo>;

That will create a new XML object for you. Saying new XML() is like saying new String(). You can do it, but it is just a waste of space.