Hi all, Just as an FYI, I'm sure you can call Javascript functions more directly by tracking down the script engine and looking up the function etc. But I don't have any details on this. Your method is simplier to implement but a bit round about :)
>>>>> "s" == syang <[EMAIL PROTECTED]> writes: s> Scanning your code, there are two obvious errors. If you haven't s> already, you should reference the specification for the ECMAScript s> binding to the DOM Level 2 Events API: s> http://www.w3.org/TR/DOM-Level-2-Events/ecma-script-binding.html. s> The first problem is that you are listening for "MutationEvent" s> when you should be listening for "DOMCharacterDataModified". The s> DOM Level 2 Events spec is very confusing about which values to use s> and when, so I'm not surprised at this mistake. Ideally, you s> should test that the current DOM implementation supports the s> Mutation event module before bothering to use my technique, since s> the listeners will otherwise never be called. For instance: s> if (!document.implementation.hasFeature("MutationEvents", s> "2.0")) alert("You need an SVG implementation that supports s> MutationEvents!") else { ... }; s> The second problem is that you are using the wrong syntax when s> registering you listener. In ECMAScript, an EventListener is s> simply a reference to a function that has parameter of Event type. s> So, when calling addEventListener(), you need to pass a "reference" s> to such a function. This is different than when specifying the s> value of one of the onxxx event attributes, in which case you s> provide the actual code to use as the attribute value. For s> instance, if you have a function called Scroll2 that has just a s> single parameter of Event type, then your last line in item 3. s> would look like: s> myTextNode.addEventListener("DOMCharacterDataModified", Scroll2, s> false); s> Your actual listener function, Scroll, needs the value of the "elt" s> variable, but the definition of EventListener gives you no way to s> pass in that value? One (bad) way to work around this would be to s> make "elt" a global variable. Another (more elegant) way would be s> to create a new listener function that calls Scroll, like the s> following: s> var Scroll2 = function(e){Scroll(e, elt);}; s> Or, you can change Scroll() itself to just take an Event parameter. s> Samuel C. Yang Echelon Corp. <http://www.echelon.com> s> email: <mailto:[EMAIL PROTECTED]> phone: 408-938-5314 fax: s> 408-790-3430 s> -----Original Message----- From: William Huang s> [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 11, 2003 10:24 s> AM To: Batik Users Subject: Re: Can I call Javascript function from s> Batik Java code? s> Hi Mr. Yang, s> Thank you very much for your suggestion. I tried but can't make it s> work. Maybe I am wrong somewhere: 1. I created a text node in s> Batik Java SVGOMTextElement text = (SVGOMTextElement) s> doc.createElementNS(svgNs, "text"); text.setAttribute("id", s> "MyTextNode"); text.setAttribute("x", "0"); text.setAttribute("y", s> "0"); text.setAttribute("visibility", "hidden"); s> svgRoot.appendChild(text); 2. When I need to call the Javascript s> function, I fired the following event Element textElement = s> svgDocument.getElementById("MyTextNode"); DocumentEvent de = s> (DocumentEvent) svgDocument; MutationEvent ev = s> (MutationEvent)de.createEvent("MutationEvents"); s> ev.initMutationEvent("DOMCharacterDataModified", true, // s> canBubbleArg false, // cancelableArg null, // relatedNodeArg null, s> // prevValueArg null, // newValueArg null, // attrNameArg s> ev.ADDITION); EventTarget t = (EventTarget) textElement; s> t.dispatchEvent(ev); 3. In Javascript, my code is as follows: var s> myTextNode = document.getElementById("MyTextNode"); s> myTextNode.addEventListener("MutationEvent", Scroll(evt, elt), s> false) Where Scroll(evt, elt) is my Javascript function. s> It seems that the code does not work. Could you tell me what is s> wrong with my code? s> With best regards, William s> ----- Original Message ----- From: <[EMAIL PROTECTED]> To: s> <[EMAIL PROTECTED]> Sent: Monday, February 10, 2003 3:58 s> PM Subject: RE: Can I call Javascript function from Batik Java s> 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 s> 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 s> 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 s> in >> Batik when you use the dispatchEvent() method to send a mutation >> event for s> a >> <text> element, the text element does not change its actual >> content, s> meaning >> that the dispatched event is lying about the new (and possibly also >> the s> 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. s> 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] >> >> s> --------------------------------------------------------------------- s> To unsubscribe, e-mail: [EMAIL PROTECTED] For s> additional commands, e-mail: [EMAIL PROTECTED] s> --------------------------------------------------------------------- s> To unsubscribe, e-mail: [EMAIL PROTECTED] For s> additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
