SVG:Script

From MozillaWiki
Revision as of 10:02, 27 May 2005 by Holger (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

preventing default behaviour in event code

when writing drag and drop code, you will realize that text on the page gets accidently selected while dragging. or if you want to use the backspace key in your code, you want to prevent the browser from going back to the last page. in these cases, one can use the evt.preventDefault() method.

using eventListeners with objects

the methods addEventListener()and removeEventListener() are very usefull when writing interactive SVG. you can pass an object as the second parameter to that methods. this object has to implement the handleEvent interface.

function myRect(x,y,w,h,message){
 this.message=message
 this.rect=document.createElementNS("http://www.w3.org/2000/svg","rect")
 this.rect.setAttribute("x",x)
 this.rect.setAttribute("y",y)
 this.rect.setAttribute("width",w)
 this.rect.setAttribute("height",h)
 document.documentElement.appendChild(this.rect)
 this.rect.addEventListener("click",this,false)
 this.handleEvent= function(evt){
   switch (evt.type){
    case "click":
     alert(this.message)
     break;
    }
   }
  }