XML.ignoreComments = false;
script =
<xml><!--
signup = new Array();
signup.onMouseClick = function(e)
{
alert(e);
}
document.getElementById("delete-elem0").onclick =
signup.onMouseClick;
--></xml>.toString();
page.body.script = script;
page.body.script.@language = "javascript";
page.body.script.@type = "text/javascript;e4x=1";
The result is:
<script language="javascript" type="text/javascript;e4x=1">signup =
new Array();
signup.onMouseClick = function(e)
{
alert(e);
}
document.getElementById("delete-elem0").onclick =
signup.onMouseClick;</script>
I'm trying to figure out why the "<!--" "-->" pair was removed and how
i can insert a "<![CDATA[" "]]>" pair around the JavaScript so that I
can include more XML within the embedded script. When I try, the "<"
in "<![CDATA[" is replaced with "<". I guess that makes some sense,
but I hope there must be some way to inject that string pair into my
XML.
> I use the following JavaScript to create a JavaScript entry in an XHTML
> page:
>
> XML.ignoreComments = false;
> script =
> <xml><!--
> signup = new Array();
> signup.onMouseClick = function(e)
> {
> alert(e);
> }
> document.getElementById("delete-elem0").onclick =
> signup.onMouseClick;
> --></xml>.toString();
If you want to create XML with E4X XML literals and then convert that a
string with XML markup then you should call toXMLString() on the XML
object and not toString(). I am however not sure what you want to
achieve with the above construct.
Which script engine are you using with E4X, Spidermonkey or Rhino?
> page.body.script = script;
> page.body.script.@language = "javascript";
> page.body.script.@type = "text/javascript;e4x=1";
>
> The result is:
>
> <script language="javascript" type="text/javascript;e4x=1">signup =
> new Array();
> signup.onMouseClick = function(e)
> {
> alert(e);
> }
> document.getElementById("delete-elem0").onclick =
> signup.onMouseClick;</script>
You nee to explain what kind of object page is and how exactly you get
the "result" you show.
> I'm trying to figure out why the "<!--" "-->" pair was removed and how
> i can insert a "<![CDATA[" "]]>" pair around the JavaScript so that I
> can include more XML within the embedded script. When I try, the "<"
> in "<![CDATA[" is replaced with "<".
E4X can parse CDATA sections in XML markup presented as a string e.g.
var xmlObject = new XML('<text><![CDATA[Kibo & Xibo]]></text>');
or an XML literal e.g.
var xmlObject2 = <text><![CDATA[Kibo & Xibo]]></text>;
but its data model does not make any difference between CDATA sections
and normal text nodes so in the above examples the XML object of node
kind 'element' with the name 'text' has as its sole child an XML object
of node kind 'text'.
If you serialize an XML object back to a string with toXMLString() then
it does not create CDATA section as far as I know, it simply escapes
characters as needed e.g.
xmlObject.toXMLString()
yields
<text>Kibo & Xibo</text>
So you can use CDATA sections when creating XML objects with E4X but if
you serialize back then you won't get them but the result is well-formed
by escaping characters as needed instead.
--
Martin Honnen
http://JavaScript.FAQTs.com/
> If you want to create XML with E4X XML literals and then convert that a
> string with XML markup then you should call toXMLString() on the XML
> object and not toString(). I am however not sure what you want to
> achieve with the above construct.
Is there anywhere that explains the difference between toString and
toXMLString?
Marcello
The specification is really the best reference, unfortunately. It's hard to give a good description of exactly how the two differ without going there, because the spec definition is the only real way to explain it.
Jeff
--
Rediscover the Web!
http://snurl.com/get_firefox
Reclaim Your Inbox!
http://snurl.com/get_thunderbird
> Marcello Bastéa-Forte wrote:
>
>> Is there anywhere that explains the difference between toString and
>> toXMLString?
>
>
> The specification is really the best reference, unfortunately.
If you don't want to start with the specification then looking at
<http://www.faqts.com/knowledge_base/index.phtml/fid/1784>
where I tried to document all methods in an informal way with some
simple examples might be a start.
I have looked at the specification but a lot of it seemed overly
verbose, but I suppose I can look again.
The main thing that bothers me on E4X is all the method syntax
(children(), text(), etc.) they should be properties, not methods!
(Especially since JavaScript supports getters/setters for properties.)
That's a side note anyhow...
It would be nice to see an 'official' api documentation (for using E4X,
not implementing it) along the lines of Adobe's, but with the little
more detail on each of the methods like you have done. Perhaps on
developer.mozilla.org (although I don't know what's involved on
contributing to that wiki).
Marcello
E4X deliberately defines these as methods so programs can work
with documents that contain valid element names that collide, such
as this case
js> var doc = <doc>
<text>just some text</text>
mixed content
</doc>
js> doc.text
just some text
js> doc.text()
mixed content
I suppose the E4X spec could have defined a set of reserved keywords,
leaving programmers to reference elements contained in tags using
those names with the alternate notation, but it doesn't. As in
js> doc['text']
just some text
- Eric
>
>[...]
Anyway, I've just been using the extra syntax and avoiding the weird
function calls when possible (such as xml.* instead of xml.children()).
Granted, that's probably slower/worse, but I am still just playing around.
Marcello
No, E4X effectively introduced separated name spaces for methods and
properties. That is, "doc.text" means "get the value of the property
text from doc", while "doc.text()" means "get the function named text
from doc and call it using doc as this". For ordinary objects property
and function value are the same, but for XML ones they entirely
different.
If you would like to get the function object behind doc.text(), you
have to use SpiderMonkey extension doc.function::text. In fact, you can
assume that doc.text(), doc['text']() are just syntax sugar for
doc.function::text() or doc.function::['text'].
Regards, Igor