18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Drag and Drop<br />

In this example, all the dragged item events fire, but no drop target event fires when you drag the text<br />

over the red . In order to turn the into a valid drop target, you must override the default<br />

behavior of dragenter and dragover. Because this is IE-specific, you can just set the<br />

oEvent.returnValue attribute to false:<br />

<br />

<br />

System Drag And Drop Example<br />

<br />

function handleDragDropEvent(oEvent) {<br />

var oTextbox = document.getElementById(“txt1”);<br />

oTextbox.value += oEvent.type + “\n”;<br />

switch(oEvent.type) {<br />

case “dragover”:<br />

case “dragenter”:<br />

oEvent.returnValue = false;<br />

}<br />

}<br />

<br />

<br />

<br />

Try dragging the text from the textbox to the red square.<br />

Drop target events fire now.<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

In this example, when you drag the text over the red , the cursor changes to a pointer with a<br />

plus sign next to it, indicating that this is a valid drop target. By default, the dragenter and dragover<br />

events for the don’t allow dropping, so if you prevent the default behavior you allow the<br />

to become a drop target. After dragenter and dragover are fired, dragleave and drop are<br />

also enabled.<br />

The dataTransfer object<br />

Simply dragging and dropping isn’t of any use unless data is actually being affected. To aid in the transmission<br />

of data via drag and drop, Internet Explorer 5.0 introduced the dataTransfer object, which<br />

exists as a property of event and is used to transfer string data from the dragged item to the drop target<br />

(the dataTransfer object is still used in IE 6.0).<br />

393

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!