# Implementing the Drag Functionality ArkWeb provides the functionality of dragging elements on web pages. Users can place an element by holding down the element and dragging it to another element. This functionality meets the HTML5 standard. ## Dragging Web Page Content to Other Applications The following table lists the data formats supported by ArkWeb. You set data in these formats based on the HTML5 standard to drag content to other applications. | Data Format | Description | | ------------- | -------- | | text/plain | Text | | text/uri-list | Link | | text/html | HTML| | Files | File | ## Listening for Drag Events The drag functionality of ArkWeb is different from that of ArkUI. ArkWeb is mainly used to drag web page content. Therefore, only some drag events can be listened for. | Method | Description | | ----------- | ----------------------------------------------------- | | [onDragStart](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#ondragstart) | This method is not supported and should not be implemented. Otherwise, the drag behavior of the **Web** component will be affected.| | [onDragEnter](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#ondragenter) | Called when an element is dragged to the web area.| | [onDragMove](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#ondragmove) | Called when an element is moved in the web area. | | [onDragLeave](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#ondragleave) | Called when the dragged element leaves the web area. | | [onDragEnd](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#ondragend10) | Called when the dragging of an element ends. | ## Implementing the Drag Logic on ArkTS In most cases, the drag functionality implemented in HTML5 can meet the requirements of an application. If necessary, refer to the following examples to read drag data on ArkTS. 1. [Establishing a data channel between the application and the frontend Page](web-app-page-data-channel.md). 2. In the **onDrop** method, implement simple logic, for example, temporarily storing some key data. 3. To implement time-consuming tasks, add the application processing logic to the message receiving method on ArkTS. The **onDrop** method on ArkTS is executed earlier than the event processing method ( **droppable.addEventListener('drop')** in the HTML example) in HTML. If page redirection is performed in the **onDrop** method, the **drop** method in HTML5 cannot be executed correctly, and the unexpected result is generated. Therefore, a bidirectional communication mechanism must be established to notify ArkTS to execute the corresponding service logic after the **drop** method in HTML5 is executed. ```ts import { webview } from '@kit.ArkWeb' import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData'; @Entry @Component struct DragDrop { private controller: webview.WebviewController = new webview.WebviewController() @State ports: Array = [] @State dragData: Array = [] build() { Column() { Web({ src: $rawfile("drag.html"), controller: this.controller, }).onPageEnd((event) => { //Register the message port. this.ports = this.controller.createWebMessagePorts(); this.ports[1].onMessageEvent((result: webview.WebMessage) => { //Process the data received from HTML. You can record logs to confirm the message. The message format can be customized as long as it can be uniquely identified. console.log("ETS receive Message: typeof (result) = " + typeof (result) + ";" + result); // Process the message after the message is received in result. You can perform time-consuming tasks. }); console.log("ETS postMessage set h5port "); //After the message port is registered, the front end sends a registration completion message to complete bidirectional port binding. this.controller.postMessage('__init_port__', [this.ports[0]], '*'); })// Implement simple logic in onDrop, for example, temporarily storing some key data. .onDrop((event) => { console.log("ETS onDrop!") let data: UnifiedData = (event as DragEvent).getData() as UnifiedData; let uriArr: Array = data.getRecords(); if (!uriArr || uriArr.length <= 0) { return false; } // Traverse records to obtain data for temporary storage or use other methods to temporarily store data. for (let i = 0; i < uriArr.length; ++i) { if (uriArr[i].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { let plainText = uriArr[i] as unifiedDataChannel.PlainText; if (plainText.textContent) { console.info("plainText.textContent: ", plainText.textContent); } } } return true }) } } } ``` HTML example: ```html HTML5 Dragging Demo

HTML5 Dragging Demo

Draggable element
Please drag the element here
``` ![web-drag-drop](figures/web-dragdrop.gif) Log output: ![web-drag-log](figures/web-drag-log.png) ## FAQs ### Why is the drag event set in HTML5 not triggered? Check whether the CSS resources are properly set. Some web pages set the CSS style only for devices with specific UAs. You can set a custom UA in the **Web** component to solve this problem. For example: ```ts import { webview } from '@kit.ArkWeb' @Entry @Component struct Index { private webController: webview.WebviewController = new webview.WebviewController() build(){ Column() { Web({ src: "example.com", controller: this.webController, }).onControllerAttached(() => { // Set a custom UA. let customUA = 'android' this.webController.setCustomUserAgent(this.webController.getUserAgent() + customUA) }) } } } ```