Entry
How can I use E4X in Mozilla and Mozilla based browsers?
How can I use E4X in Mozilla and Mozilla based browsers?
How can I use E4X in Mozilla and Mozilla based browsers?
Mar 24th, 2005 03:17
Martin Honnen,
The Spidermonkey JavaScript engine used in Mozilla (and Mozilla based
browsers like Netscape) has been extended to implement E4X but currently
you can only use it in nightly trunk builds, the current stable releases
(Mozilla 1.7 suite, Firefox 1.0, Netscape 7.2) do not have E4X support.
To use E4X in Mozilla browsers that support it (e.g. current Mozilla
nightlies) you use a normal (X)HTML <script> element but you need to add
the parameter e4x=1 to the content type attribute value e.g.
<script type="application/x-javascript; e4x=1">
or
<script type="text/javascript; e4x=1">
However as E4X has changed the syntax of the core language (by adding
XML literals for instance) a JavaScript/ECMAScript script making use of
E4X features might yield syntax errors when being exposed to browsers
with script engines that do not support E4X. Therefore mechanisms are
needed to have browsers with E4X support parse the script but have other
browsers ignore it. With pure client-side methods I don't know of a
totally safe way, the best method I have found is the use of the
following type attribute value on the <script> element:
<script type="application/x-javascript; version=1.5; e4x=1">
That way the script is parsed by Mozilla nightlies with E4X support but
ignored by older Mozilla versions. The script is also ignored by all IE
version tested (IE 4, 5, 5.5, 6 on Windows, IE 5 on Mac). Furthermore
Opera 7 and Opera 8.00 beta ignore such a script. Netscape 4 also
ignores it. Only Safari and Konqueror browsers unfortunately do not
ignore such a script thus it is not safe to use that mechanism on
general web pages, you will need to add server-side mechanisms to only
serve the E4X scripts to the appropriate browsers.