Scanning your code, there are two obvious errors.  If you haven't already,
you should reference the specification for the ECMAScript binding to the DOM
Level 2 Events API:
http://www.w3.org/TR/DOM-Level-2-Events/ecma-script-binding.html.

The first problem is that you are listening for "MutationEvent" when you
should be listening for "DOMCharacterDataModified".  The DOM Level 2 Events
spec is very confusing about which values to use and when, so I'm not
surprised at this mistake.  Ideally, you should test that the current DOM
implementation supports the Mutation event module before bothering to use my
technique, since the listeners will otherwise never be called.  For
instance:

   if (!document.implementation.hasFeature("MutationEvents", "2.0"))
      alert("You need an SVG implementation that supports MutationEvents!")
   else { ... };

The second problem is that you are using the wrong syntax when registering
you listener.  In ECMAScript, an EventListener is simply a reference to a
function that has parameter of Event type.  So, when calling
addEventListener(), you need to pass a "reference" to such a function.  This
is different than when specifying the value of one of the onxxx event
attributes, in which case you provide the actual code to use as the
attribute value.  For instance, if you have a function called Scroll2 that
has just a single parameter of Event type, then your last line in item 3.
would look like:

  myTextNode.addEventListener("DOMCharacterDataModified", Scroll2, false);

Your actual listener function, Scroll, needs the value of the "elt"
variable, but the definition of EventListener gives you no way to pass in
that value?  One (bad) way to work around this would be to make "elt" a
global variable.  Another (more elegant) way would be to create a new
listener function that calls Scroll, like the following:

  var Scroll2 = function(e){Scroll(e, elt);};

Or, you can change Scroll() itself to just take an Event parameter.

Samuel C. Yang
Echelon Corp. <http://www.echelon.com>

email: <mailto:[EMAIL PROTECTED]>
phone: 408-938-5314
fax: 408-790-3430


-----Original Message-----
From: William Huang [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, February 11, 2003 10:24 AM
To: Batik Users
Subject: Re: Can I call Javascript function from Batik Java code?


Hi Mr. Yang,

Thank you very much for your suggestion. I tried but can't make it work.
Maybe I am wrong somewhere: 1. I created a text node in Batik Java
                  SVGOMTextElement text = (SVGOMTextElement)
doc.createElementNS(svgNs, "text");
                  text.setAttribute("id", "MyTextNode");
                  text.setAttribute("x", "0");
                  text.setAttribute("y", "0");
                  text.setAttribute("visibility", "hidden");
                  svgRoot.appendChild(text);
2. When I need to call the Javascript function, I fired the following event
                Element textElement =
svgDocument.getElementById("MyTextNode");
                DocumentEvent de = (DocumentEvent) svgDocument;
                MutationEvent ev =
(MutationEvent)de.createEvent("MutationEvents");
                ev.initMutationEvent("DOMCharacterDataModified",
                                 true,   // canBubbleArg
                                 false,  // cancelableArg
                                 null,   // relatedNodeArg
                                 null,   // prevValueArg
                                 null,   // newValueArg
                                 null,   // attrNameArg
                                 ev.ADDITION);
                EventTarget t = (EventTarget) textElement;
                t.dispatchEvent(ev);
3. In Javascript, my code is as follows:
var myTextNode = document.getElementById("MyTextNode");
myTextNode.addEventListener("MutationEvent", Scroll(evt, elt), false) Where
Scroll(evt, elt) is my Javascript function.

It seems that the code does not work. Could you tell me what is wrong with
my code?

With best regards,
William


----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, February 10, 2003 3:58 PM
Subject: RE: Can I call Javascript function from Batik Java code?


> Here's a method based on the standard (but optional) DOM Event type, 
> MutationEvents.  It took me a little while to come up with it, but 
> I'll
give
> it to you for free :-).
>
> If your SVG processor supports the MutationEvents feature (you should 
> test for this, especially if you intend to also run on non-Batik SVG 
> engines), and has the right language bindings to the DOM (like Batik 
> does) then you can use a <text> element (probably with 
> visibility="hidden", and width and
> height="0") to which you dispatch mutation events from Java, and to which
> you can attach an ECMAScript listener.  The new text content specified in
> the event data can be used to carry any kind of message you want.  You can
> use the same technique to go from ECMAScript to Java, too.
>
> Note that:
>
> (1) Using dispatchEvent() is more efficient that actually changing the 
> content of the <text> element.
> (2) The <text> element should not have display="none", since then it 
> is
not
> supposed to receive any events.  [However, Batik currently does allow 
> the
> dispatchEvent() method to send events to a <text> element having the
> display="none" attribute.  I believe this is a bug.  Also, I noticed that
in
> Batik when you use the dispatchEvent() method to send a mutation event 
> for
a
> <text> element, the text element does not change its actual content,
meaning
> that the dispatched event is lying about the new (and possibly also 
> the
old)
> content of the element.  This is probably acceptable behavior, since 
> the spec doesn't seem to say this can't happen, and it is more 
> efficient.
Just
> be sure that you don't accidentally treat the event data for such 
> <text> elements as being real text event data.]
>
> Samuel C. Yang
> Echelon Corp. <http://www.echelon.com>
>
> email: <mailto:[EMAIL PROTECTED]>
> phone: 408-938-5314
> fax: 408-790-3430
>
> > -----Original Message-----
> > From: William Huang [mailto:[EMAIL PROTECTED]]
> > Sent: Monday, February 10, 2003 11:44 AM
> > To: Batik Users
> > Subject: Can I call Javascript function from Batik Java code?
> >
> >
> > I changed the SVGImageElementBridge program to make the <a> element 
> > work as my application needs. However, I need to call one large 
> > Javascript function of mine at the end of my
> > createSVGImageNode() method call. I can rewrite the Javascript 
> > function into Java, but that costs too much efforts. Is there a way 
> > to call Javascript function from Batik Java implementation? Thanks a 
> > lot.
> >
> > William
> >
> >
> > --------------------------------------------------------------------
> > -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to