Tony Bowden's Radio Weblog:
Updated: 03/05/2002; 15:16:57.

 

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

06 April 2002

Drag and Drop redux

It seems that each of the various drag and drop event handlers only know about either the object being dragged, or the area into which it's being dropped. Not both. This means that when dropping an object its receiver can't know what's arriving.

There is a dataTransfer object that can be seen by both ends, which acts as a clipboard onto which you can paste relevant information: but seemingly only either text or a URL (presumably for dragging text or images respectively) - not the object itself.

This means I've had to resort to the terrible hack:

  function makeMovable() {
    event.dataTransfer.setData("Text", event.srcElement.id);
    event.dataTransfer.setData("URL",  "http://ok/");
  }
  
function dropItem() { if (! isMovable()) return; ... }
function isMovable() { return ((event.dataTransfer.getData("URL") == "http://ok/") && event.dataTransfer.getData("Text")); }

[I tried just setting both and making sure both were set, but text dragged from an href has both set already :( ]

This can't be the best way to do this.



Spend a few hours this afternoon playing with drag and drop in IE. There seems to be two main approaches. At JavaScriptKit they use onmousedown and onemouseup to grab elements with class=draggable and then dynamically change their x/y position.

This creates a better visual effect than the official Microsoft approach, which uses a variety of new eventhandlers. But the Microsoft way allows for more powerful scripting, as you get to call functions not just when you grab/drag/drop, but also as enter or leave a 'drop-zone'.

I can't decide yet which I prefer. I should probably look at ways to combine both.



© Copyright 2002 Tony Bowden.



Click here to visit the Radio UserLand website.

 


April 2002
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Mar   May