|
|
Drag and Drop with Microsoft Internet Explorer 5
You are here: irt.org | Articles | Dynamic HTML (DHTML) | Drag and Drop with Microsoft Internet Explorer 5 Published on: Sunday 13th February 2000 By: Ryan Detert
IntroductionAs we have already seen in previous articles, scripting events can add a needed flair to your web pages without the hassle of using a plug-in. In this article, we will introduce you to you some of the brand-new Internet Explorer 5.0 specific drag-and-drop events. Dragging n' Dropping n' GeneralIn the Windows environment you have all used drag-and-drop operations, but maybe without realizing it. For example , if you click an icon on your desktop and hold the mouse button down and then move the cursor around the screen the icon will follow. This is known as dragging. When you release the mouse button this is known as dropping. The New Explorer 5.0 Event HandlersThere are two terms associated with the drag-and-drop operation, source and target. So, if you were dragging a folder into the Recycle Bin, the folder would be the source and the Recycle Bin would be the target. Simple. All the events associated with drag-and-drop operations occur in a sequential manner, all the event handlers are listed below as sequentially as possible. ondragstart (source event)Fires on the source object when the user starts to drag a text selection or selected object. This is the FIRST event to fire when you begin dragging anything. ondrag (source event)Fires on the source object continuously while it is being dragged. It fires directly after the ondragstart event and will continue to fire until the ondragend event handler is triggered. To invoke the ondrag handler:
ondragenter (target event)Fires when the source is dragged over a valid drop target object. The drop target may be in another browser window. ondragover (target event)Fires on the target element continuously while the user drags the object over a valid drop target. ondrop (target event)Fires on the target element when the user releases the mouse button over a valid drop target. ondragleave (target event)Fires if the user drags an object over a valid drop target and then out of the valid drop target. ondragend (source event)The final event to fire, it occurs when the user releases the mouse in a drag operation, regardless of whether the source element is over a valid drop target. Transferring The Dragged DataBy using the window.event.dataTransfer object we can access pre-defined clipboard functionalities to assist us in our drag-and-drop operations. This makes it possible to customize our dragging and dropping. clearData()Removes one or more data formats from the clipboard by means of the dataTransfer object. The syntax for this method is: window.event.dataTransfer.clearData([dataFormat]), where dataFormat is optional and may have any of the following string values.
If no format is specified then all data formats are cleared. (To override the default behavior of the target element, use this method in the ondrop handler.) effectAllowedSpecifies the effects allowed on the source element by the drag-and-drop operation. The syntax is: window.event.dataTransfer.effectAllowed [= effect], where effect is a string specifying the type of effect. Here is a list of the effects:
dropEffectSets or retrieves the type of drag-and-drop operation and the cursor to display for the object. The syntax is: window.event.dataTransfer.dropEffect [= cursorStyle] where cursorStyle is a string that specifies the type of cursor displayed. Here is a list of possible cursor types:
setData()Assigns data in a specified format to the dataTransfer object. The syntax for this method is: window.event.dataTransfer.setData(sDataFormat, sData), where sDataFormat is a required string that specifies the format of the data to be transferred. It may either contain the value "Text" or "URL". The sData argument is another required string that specifies the data supplied by the source element. It may either be descriptive text or a URL of some sort. Do note that the sDataFormat and sData information must correspond. The setData() method will return a boolean true if the operation was successful, otherwise it will return false. getData()Retrieves the data in the specified format from the clipboard through the dataTransfer object. The syntax for this method is: window.event.dataTransfer.getData(sDataFormat), where sDataFormat is required and is a string specifying either the data format value "Text" or "URL". This method also enforces cross-frame security because it will fail if you try to transfer information between different security protocols or between two browsers with different security settings. Working Example - Source CodeBased on the above descriptions of all the methods, and the following annotated code, you should be able to understand how dragging n' dropping in IE5 works:
Working ExampleTry the IE5.0 Drag n' Drop Demo Netscape 4.0 Dragging 'n DroppingNetscape also has a type of drag-and-drop operation, though not nearly as robust as that of Explorer. The operation utilizes Windows' built in drag-and-drop mechanism. The onDragDrop event handler is used in conjunction with the window.captureEvents() method and will be triggered whenever any Windows system object is dragged into a Netscape browser window. However, there is a work-around for Netscape that allows you to create the same effect as in Explorer that utilizes layers and the mouse events that we have discussed in previous articles. We may return at a future date with a full implementation for Netscape 4.0. Feedback on 'Drag and Drop with Microsoft Internet Explorer 5'
View the profile on Ryan Detert and the list of other Articles by Ryan Detert. |
-- div -->
|