Re: does document.getElementId("MyElementId") work?

At 05:17 PM 10/1/00 -0700, svge@taedium.com wrote:
>Hi,
>
>I think your problem is that, unlike with javascript and html,
>the 'document' object is undefined.  You need to define it yourself
>when the svg is loaded or just use the DOM2 Event interface:
>
>         <svg width="100" height="100">
>             <script><![CDATA[
>             function MouseClickHandler(evt) {
>               var obj = evt.target;
>                 if (obj == null) {
>                   alert("Obj is a null!");
>                 } else {
>                   obj.setAttribute('r', '20');
>                         }
>             }
>           ]]></script>
>           <circle id="MyElement" cx="50" cy="50" r="10"
>                 onclick="MouseClickHandler(evt)"/>
>         </svg>
>
>This will work with the Adobe plugin and IE 5.5.  I don't know
>why, but Netscape 4.7 doesn't seem to like it (indeed, Netscape
>crashes with an 'illegal operation' error.)

The reason the Adobe SVG Viewer doesn't support the "target" DOM attribute 
has to do with difficulties in dealing with Netscape's LiveConnect 
facility, which is the means for plugins to gain access to DOM facilities. 
LiveConnect puts Java between the plugin and JavaScript, which brings about 
severe implementation complications for all DOM attributes. Because of the 
complications, the Adobe SVG Viewer does not support any DOM attributes for 
NN4.x as attributes. Instead, it provides getter and setter functions, as 
in evt.getTarget(). The getters and setters are available in the Adobe SVG 
Viewer for both NN4.x and IE5.x. DOM-compliant direct access to the DOM 
attributes (e.g., evt.target) is only available for IE5.x.

This limitation is most problematic for events. For most everything else in 
SVG, you can access DOM  values via method calls, such as getAttribute(...) 
and setAttribute(...). But there is no W3C-compliant method call for 
getting to DOM attributes on an event structure.

One strategy is to access the 'target' as follows:

var target;
// getTarget() method necessary to overcome limitations with NN4.x
if (evt.getTarget()) target = evt.getTarget();
else target = evt.target;

The above code sees if there is a getter method for the target attribute, 
and if so, then use it, else fall back to accessing the DOM attribute 
directly. I haven't done thorough testing of the above code, but I think it 
allows scripts to be written which will work with the Adobe SVG Viewer on 
all supported platforms and also with other implementations which do not 
implement the getter and setter functions.

Jon


>See the SVG/Interactivity and DOM2/Events pages for more
>information.
>
>HTH,
>ERic
>
>
> >Message-ID: <20001001204420.27562.qmail@web310.mail.yahoo.com>
> >Date: Sun, 1 Oct 2000 13:44:20 -0700 (PDT)
> >From: Wu Ping <wupingzj@yahoo.com>
> >To: www-svg@w3.org
> >Subject: does document.getElementId("MyElementId") work?
> >
> >Dear sir,
> >
> >I try to use the following code to get the reference
> >of an element in a svg file, but always get the null
> >result. Could you give me a tip?
> >
> ><svg width="100" height="100">
> >  <defs><script><![CDATA[
> >    function MouseClickHandler() {
> >      var obj;
> >        obj = document.getElementById('MyElement');
> >        if (obj == null)
> >          alert("Obj is a null!");
> >        else
> >          obj.setAttribute('r', '20');
> >        }
> >  ]]></script>
> >  </defs>
> >  <circle id="MyElement" cx="50" cy="50"
> >onclick="MouseClickHandler()" r="10"/>
> ></svg>
> >
> >I run it with IE5 and adobe svg viewer.
> >
> >Thank you in advance!
> >
> >W P

Received on Monday, 2 October 2000 14:25:36 UTC