ts-basic-components-web.md 161.3 KB
Newer Older
E
ester.zhou 已提交
1 2
# Web

E
ester.zhou 已提交
3
The **<Web\>** component can be used to display web pages. It can be used with the [@ohos.web.webview](../apis/js-apis-webview.md) module, which provides APIs for web control.
4

5 6 7 8
> **NOTE**
>
> - This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
> - You can preview how this component looks on a real device. The preview is not yet available in the DevEco Studio Previewer.
E
ester.zhou 已提交
9

E
ester.zhou 已提交
10
## Required Permissions
E
ester.zhou 已提交
11
To use online resources, the application must have the **ohos.permission.INTERNET** permission. For details about how to apply for a permission, see [Declaring Permissions](../../security/accesstoken-guidelines.md).
E
ester.zhou 已提交
12

E
ester.zhou 已提交
13 14
## Child Components

E
ester.zhou 已提交
15
Not supported
E
ester.zhou 已提交
16 17 18

## APIs

E
ester.zhou 已提交
19
Web(options: { src: ResourceStr, controller: WebviewController | WebController})
E
ester.zhou 已提交
20 21 22 23

> **NOTE**
>
> Transition animation is not supported.
E
ester.zhou 已提交
24
> **\<Web>** components on a page must be bound to different **WebController**s.
25 26

**Parameters**
E
ester.zhou 已提交
27

28 29
| Name       | Type                                    | Mandatory  | Description   |
| ---------- | ---------------------------------------- | ---- | ------- |
E
ester.zhou 已提交
30
| src        | [ResourceStr](ts-types.md)               | Yes   | Address of a web page resource. To access local resource files, use the **$rawfile** or **resource** protocol. To load a local resource file in the sandbox outside of the application package, use **file://** to specify the path of the sandbox.|
E
ester.zhou 已提交
31
| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | Yes   | Controller. **WebController** is deprecated since API version 9. You are advised to use **WebviewController** instead.|
32 33

**Example**
E
ester.zhou 已提交
34

E
ester.zhou 已提交
35
  Example of loading online web pages:
36 37
  ```ts
  // xxx.ets
E
ester.zhou 已提交
38
  import web_webview from '@ohos.web.webview'
E
ester.zhou 已提交
39

E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

  Example of loading local web pages:
E
ester.zhou 已提交
53 54 55 56 57
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
58
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
59 60 61 62 63 64 65 66
    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  Example of loading local resource files in the sandbox:
  1. Use[globalthis](../../application-models/uiability-data-sync-with-ui.md#using-globalthis-between-uiability-and-page) to obtain the path of the sandbox.
     ```ts
     // xxx.ets
     import web_webview from '@ohos.web.webview'
     let url = 'file://' + globalThis.filesDir + '/xxx.html'

     @Entry
     @Component
     struct WebComponent {
       controller: web_webview.WebviewController = new web_webview.WebviewController()
       build() {
         Column() {
           // Load the files in the sandbox.
           Web({ src: url, controller: this.controller })
         }
       }
     }
     ```

  2. Modify the **MainAbility.ts** file.
  
     The following uses **filesDir** as an example to describe how to obtain the path of the sandbox. For details about how to obtain other paths, see [Obtaining the Application Development Path](../../application-models/application-context-stage.md#obtaining-the-application-development-path).
     ```ts
     // xxx.ts
     import UIAbility from '@ohos.app.ability.UIAbility';
     import web_webview from '@ohos.web.webview';

     export default class EntryAbility extends UIAbility {
         onCreate(want, launchParam) {
             // Bind filesDir to the globalThis object to implement data synchronization between the UIAbility component and the UI.
             globalThis.filesDir = this.context.filesDir
             console.log("Sandbox path is " + globalThis.filesDir)
         }
     }
     ```

     ```html
     <!-- index.html -->
     <!DOCTYPE html>
     <html>
         <body>
             <p>Hello World</p>
         </body>
     </html>
     ```
E
ester.zhou 已提交
113 114

## Attributes
115

116
Only the following universal attributes are supported: [width](ts-universal-attributes-size.md#Attributes), [height](ts-universal-attributes-size.md#attributes), [padding](ts-universal-attributes-size.md#Attributes), [margin](ts-universal-attributes-size.md#attributes), and [border](ts-universal-attributes-border.md#attributes).
117 118 119 120 121 122 123 124

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

Sets whether to enable the DOM Storage API. By default, this feature is disabled.

**Parameters**
E
ester.zhou 已提交
125

126 127
| Name             | Type   | Mandatory  | Default Value  | Description                                |
| ---------------- | ------- | ---- | ----- | ------------------------------------ |
128 129 130
| domStorageAccess | boolean | Yes   | false | Whether to enable the DOM Storage API.|

**Example**
E
ester.zhou 已提交
131

132 133 134 135 136
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
137
    controller: WebController = new WebController()
138 139
    build() {
      Column() {
E
ester.zhou 已提交
140 141
        Web({ src: 'www.example.com', controller: this.controller })
          .domStorageAccess(true)
142 143 144 145 146 147 148 149 150
      }
    }
  }
  ```

### fileAccess

fileAccess(fileAccess: boolean)

151
Sets whether to enable access to the file system in the application. This setting does not affect the access to the files specified through [$rawfile(filepath/filename)](../../quick-start/resource-categories-and-access.md).
152 153

**Parameters**
E
ester.zhou 已提交
154

155 156
| Name       | Type   | Mandatory  | Default Value | Description                  |
| ---------- | ------- | ---- | ---- | ---------------------- |
E
ester.zhou 已提交
157
| fileAccess | boolean | Yes   | true | Whether to enable access to the file system in the application. By default, this feature is enabled.|
158 159

**Example**
E
ester.zhou 已提交
160

161 162 163 164 165
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
166
    controller: WebController = new WebController()
167 168
    build() {
      Column() {
E
ester.zhou 已提交
169 170
        Web({ src: 'www.example.com', controller: this.controller })
          .fileAccess(true)
171 172 173 174 175 176 177 178 179 180 181 182
      }
    }
  }
  ```

### imageAccess

imageAccess(imageAccess: boolean)

Sets whether to enable automatic image loading. By default, this feature is enabled.

**Parameters**
E
ester.zhou 已提交
183

184 185
| Name        | Type   | Mandatory  | Default Value | Description           |
| ----------- | ------- | ---- | ---- | --------------- |
186
| imageAccess | boolean | Yes   | true | Whether to enable automatic image loading.|
187 188 189 190 191 192 193

**Example**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
194
    controller: WebController = new WebController()
195 196
    build() {
      Column() {
E
ester.zhou 已提交
197 198
        Web({ src: 'www.example.com', controller: this.controller })
          .imageAccess(true)
199 200 201 202 203 204 205 206
      }
    }
  }
  ```

### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
E
ester.zhou 已提交
207
    controller: WebController | WebviewController})
208

209
Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. The parameters cannot be updated.
210 211

**Parameters**
E
ester.zhou 已提交
212

213 214 215 216 217 218
| Name       | Type                                    | Mandatory  | Default Value | Description                     |
| ---------- | ---------------------------------------- | ---- | ---- | ------------------------- |
| object     | object                                   | Yes   | -    | Object to be registered. Methods can be declared, but attributes cannot.   |
| name       | string                                   | Yes   | -    | Name of the object to be registered, which is the same as that invoked in the window.|
| methodList | Array\<string\>                          | Yes   | -    | Methods of the JavaScript object to be registered at the application side. |
| controller | [WebController](#webcontroller) or [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | Yes   | -    | Controller.                     |
219 220

**Example**
E
ester.zhou 已提交
221

222 223 224 225 226
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    controller: WebController = new WebController()
    testObj = {
      test: (data1, data2, data3) => {
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
      },
      toString: () => {
        console.log('toString' + "interface instead.")
      }
    }
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
        })
      }
    }
  }
  ```
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
261 262
    testObj = {
      test: (data1, data2, data3) => {
E
ester.zhou 已提交
263 264 265 266
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
267 268
      },
      toString: () => {
E
ester.zhou 已提交
269
        console.log('toString' + "interface instead.")
270 271 272 273
      }
    }
    build() {
      Column() {
E
ester.zhou 已提交
274 275 276 277 278 279 280
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
281 282 283 284 285 286 287 288 289 290 291 292 293
        })
      }
    }
  }
  ```

### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

Sets whether JavaScript scripts can be executed. By default, JavaScript scripts can be executed.

**Parameters**
E
ester.zhou 已提交
294

295 296 297 298 299
| Name             | Type   | Mandatory  | Default Value | Description               |
| ---------------- | ------- | ---- | ---- | ------------------- |
| javaScriptAccess | boolean | Yes   | true | Whether JavaScript scripts can be executed.|

**Example**
E
ester.zhou 已提交
300

301 302 303 304 305
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
306
    controller: WebController = new WebController()
307 308
    build() {
      Column() {
E
ester.zhou 已提交
309 310
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
311 312 313 314 315 316 317 318 319 320 321 322
      }
    }
  }
  ```

### mixedMode

mixedMode(mixedMode: MixedMode)

Sets whether to enable loading of HTTP and HTTPS hybrid content can be loaded. By default, this feature is disabled.

**Parameters**
E
ester.zhou 已提交
323

324 325
| Name      | Type                       | Mandatory  | Default Value           | Description     |
| --------- | --------------------------- | ---- | -------------- | --------- |
E
ester.zhou 已提交
326
| mixedMode | [MixedMode](#mixedmode)| Yes   | MixedMode.None | Mixed content to load.|
327 328

**Example**
E
ester.zhou 已提交
329

330 331 332 333 334
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
335 336
    controller: WebController = new WebController()
    @State mode: MixedMode = MixedMode.All
337 338
    build() {
      Column() {
E
ester.zhou 已提交
339 340
        Web({ src: 'www.example.com', controller: this.controller })
          .mixedMode(this.mode)
341 342 343 344 345 346 347 348 349 350 351 352
      }
    }
  }
  ```

### onlineImageAccess

onlineImageAccess(onlineImageAccess: boolean)

Sets whether to enable access to online images through HTTP and HTTPS. By default, this feature is enabled.

**Parameters**
E
ester.zhou 已提交
353

354 355 356 357 358
| Name              | Type   | Mandatory  | Default Value | Description            |
| ----------------- | ------- | ---- | ---- | ---------------- |
| onlineImageAccess | boolean | Yes   | true | Whether to enable access to online images through HTTP and HTTPS.|

**Example**
E
ester.zhou 已提交
359

360 361 362 363 364
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
365
    controller: WebController = new WebController()
366 367
    build() {
      Column() {
E
ester.zhou 已提交
368 369
        Web({ src: 'www.example.com', controller: this.controller })
          .onlineImageAccess(true)
370 371 372 373 374 375 376 377 378 379 380 381
      }
    }
  }
  ```

### zoomAccess

zoomAccess(zoomAccess: boolean)

Sets whether to enable zoom gestures. By default, this feature is enabled.

**Parameters**
E
ester.zhou 已提交
382

383 384 385 386 387
| Name       | Type   | Mandatory  | Default Value | Description         |
| ---------- | ------- | ---- | ---- | ------------- |
| zoomAccess | boolean | Yes   | true | Whether to enable zoom gestures.|

**Example**
E
ester.zhou 已提交
388

389 390 391 392 393
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
394
    controller: WebController = new WebController()
395 396
    build() {
      Column() {
E
ester.zhou 已提交
397 398
        Web({ src: 'www.example.com', controller: this.controller })
          .zoomAccess(true)
399 400 401 402 403 404 405 406 407 408 409 410
      }
    }
  }
  ```

### overviewModeAccess

overviewModeAccess(overviewModeAccess: boolean)

Sets whether to load web pages by using the overview mode. By default, this feature is enabled.

**Parameters**
E
ester.zhou 已提交
411

412 413
| Name               | Type   | Mandatory  | Default Value | Description           |
| ------------------ | ------- | ---- | ---- | --------------- |
E
ester.zhou 已提交
414
| overviewModeAccess | boolean | Yes   | true | Whether to load web pages by using the overview mode.|
415 416

**Example**
E
ester.zhou 已提交
417

418 419 420 421 422
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
423
    controller: WebController = new WebController()
424 425
    build() {
      Column() {
E
ester.zhou 已提交
426 427
        Web({ src: 'www.example.com', controller: this.controller })
          .overviewModeAccess(true)
428 429 430 431 432 433 434 435 436 437 438 439
      }
    }
  }
  ```

### databaseAccess

databaseAccess(databaseAccess: boolean)

Sets whether to enable database access. By default, this feature is disabled.

**Parameters**
E
ester.zhou 已提交
440

441 442
| Name           | Type   | Mandatory  | Default Value  | Description             |
| -------------- | ------- | ---- | ----- | ----------------- |
E
ester.zhou 已提交
443
| databaseAccess | boolean | Yes   | false | Whether to enable database access.|
444 445

**Example**
E
ester.zhou 已提交
446

447 448 449 450 451
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
452
    controller: WebController = new WebController()
453 454
    build() {
      Column() {
E
ester.zhou 已提交
455 456
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
457 458 459 460 461 462 463 464 465 466 467 468
      }
    }
  }
  ```

### geolocationAccess

geolocationAccess(geolocationAccess: boolean)

Sets whether to enable geolocation access. By default, this feature is enabled.

**Parameters**
E
ester.zhou 已提交
469

470 471 472
| Name              | Type   | Mandatory  | Default Value | Description           |
| ----------------- | ------- | ---- | ---- | --------------- |
| geolocationAccess | boolean | Yes   | true | Whether to enable geolocation access.|
473 474

**Example**
E
ester.zhou 已提交
475

476 477 478 479 480
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
481
    controller: WebController = new WebController()
482 483
    build() {
      Column() {
E
ester.zhou 已提交
484 485 486 487 488 489 490 491 492 493 494
        Web({ src: 'www.example.com', controller: this.controller })
          .geolocationAccess(true)
      }
    }
  }
  ```

### mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

495
Sets whether video playback must be started by user gestures. This API is not applicable to videos that do not have an audio track or whose audio track is muted.
E
ester.zhou 已提交
496 497 498

**Parameters**

499 500 501
| Name   | Type   | Mandatory  | Default Value | Description             |
| ------ | ------- | ---- | ---- | ----------------- |
| access | boolean | Yes   | true | Whether video playback must be started by user gestures.|
E
ester.zhou 已提交
502 503 504 505 506 507 508 509

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
510 511
    controller: WebController = new WebController()
    @State access: boolean = true
E
ester.zhou 已提交
512 513 514 515
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .mediaPlayGestureAccess(this.access)
516 517 518 519 520
      }
    }
  }
  ```

E
ester.zhou 已提交
521 522 523 524 525 526 527 528
### multiWindowAccess<sup>9+</sup>

multiWindowAccess(multiWindow: boolean)

Sets whether to enable the multi-window permission.

**Parameters**

529 530
| Name        | Type   | Mandatory  | Default Value  | Description        |
| ----------- | ------- | ---- | ----- | ------------ |
E
ester.zhou 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
| multiWindow | boolean | Yes   | false | Whether to enable the multi-window permission.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .multiWindowAccess(true)
      }
    }
  }
  ```

550 551 552 553
### horizontalScrollBarAccess<sup>9+</sup>

horizontalScrollBarAccess(horizontalScrollBar: boolean)

E
ester.zhou 已提交
554
Sets whether to display the horizontal scrollbar, including the default system scrollbar and custom scrollbar. By default, the horizontal scrollbar is displayed.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

**Parameters**

| Name        | Type   | Mandatory  | Default Value  | Description        |
| ----------- | ------- | ---- | ----- | ------------ |
| horizontalScrollBar | boolean | Yes   | true | Whether to display the horizontal scrollbar.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .horizontalScrollBarAccess(true)
      }
    }
  }
  ```

  ```html
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
      <title>Demo</title>
      <style>
        body {
          width:3000px;
          height:3000px;
          padding-right:170px;
          padding-left:170px;
          border:5px solid blueviolet
        }
      </style>
  </head>
  <body>
  Scroll Test
  </body>
  </html>
  ```

### verticalScrollBarAccess<sup>9+</sup>

verticalScrollBarAccess(verticalScrollBar: boolean)

E
ester.zhou 已提交
605
Sets whether to display the vertical scrollbar, including the default system scrollbar and custom scrollbar. By default, the vertical scrollbar is displayed.
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

**Parameters**

| Name        | Type   | Mandatory  | Default Value  | Description        |
| ----------- | ------- | ---- | ----- | ------------ |
| verticalScrollBarAccess | boolean | Yes   | true | Whether to display the vertical scrollbar.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .verticalScrollBarAccess(true)
      }
    }
  }
  ```

  ```html
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
      <title>Demo</title>
      <style>
        body {
          width:3000px;
          height:3000px;
          padding-right:170px;
          padding-left:170px;
          border:5px solid blueviolet
        }
      </style>
  </head>
  <body>
  Scroll Test
  </body>
  </html>
  ```


653 654 655 656 657 658 659
### cacheMode

cacheMode(cacheMode: CacheMode)

Sets the cache mode.

**Parameters**
E
ester.zhou 已提交
660

661 662
| Name      | Type                       | Mandatory  | Default Value              | Description     |
| --------- | --------------------------- | ---- | ----------------- | --------- |
663 664 665
| cacheMode | [CacheMode](#cachemode)| Yes   | CacheMode.Default | Cache mode to set.|

**Example**
E
ester.zhou 已提交
666

667 668 669 670 671
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
672 673
    controller: WebController = new WebController()
    @State mode: CacheMode = CacheMode.None
674 675
    build() {
      Column() {
E
ester.zhou 已提交
676 677
        Web({ src: 'www.example.com', controller: this.controller })
          .cacheMode(this.mode)
678 679 680 681 682
      }
    }
  }
  ```

E
ester.zhou 已提交
683
### textZoomRatio<sup>9+</sup>
684 685 686 687 688 689

textZoomRatio(textZoomRatio: number)

Sets the text zoom ratio of the page. The default value is **100**, which indicates 100%.

**Parameters**
E
ester.zhou 已提交
690

691 692 693
| Name          | Type  | Mandatory  | Default Value | Description           |
| ------------- | ------ | ---- | ---- | --------------- |
| textZoomRatio | number | Yes   | 100  | Text zoom ratio to set.|
694 695

**Example**
E
ester.zhou 已提交
696

697 698 699 700 701
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
702 703
    controller: WebController = new WebController()
    @State atio: number = 150
704 705
    build() {
      Column() {
E
ester.zhou 已提交
706 707
        Web({ src: 'www.example.com', controller: this.controller })
          .textZoomRatio(this.atio)
708 709 710 711 712
      }
    }
  }
  ```

E
ester.zhou 已提交
713 714 715 716 717 718 719 720
### initialScale<sup>9+</sup>

initialScale(percent: number)

Sets the scale factor of the entire page. The default value is 100%.

**Parameters**

721 722 723
| Name    | Type  | Mandatory  | Default Value | Description           |
| ------- | ------ | ---- | ---- | --------------- |
| percent | number | Yes   | 100  | Scale factor of the entire page.|
E
ester.zhou 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    @State percent: number = 100
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .initialScale(this.percent)
      }
    }
  }
  ```

743 744 745 746 747 748 749
### userAgent

userAgent(userAgent: string)

Sets the user agent.

**Parameters**
E
ester.zhou 已提交
750

751 752 753 754 755
| Name      | Type  | Mandatory  | Default Value | Description     |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | Yes   | -    | User agent to set.|

**Example**
E
ester.zhou 已提交
756

757 758 759 760 761
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
762 763
    controller: WebController = new WebController()
    @State userAgent:string = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
764 765
    build() {
      Column() {
E
ester.zhou 已提交
766 767
        Web({ src: 'www.example.com', controller: this.controller })
          .userAgent(this.userAgent)
768 769 770 771
      }
    }
  }
  ```
E
ester.zhou 已提交
772

E
ester.zhou 已提交
773 774 775 776 777 778 779 780
### webDebuggingAccess<sup>9+</sup>

webDebuggingAccess(webDebuggingAccess: boolean)

Sets whether to enable web debugging.

**Parameters**

781 782 783
| Name               | Type   | Mandatory  | Default Value  | Description         |
| ------------------ | ------- | ---- | ----- | ------------- |
| webDebuggingAccess | boolean | Yes   | false | Whether to enable web debugging.|
E
ester.zhou 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    @State webDebuggingAccess: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webDebuggingAccess(this.webDebuggingAccess)
      }
    }
  }
  ```

803
### blockNetwork<sup>9+</sup>
E
ester.zhou 已提交
804

805
blockNetwork(block: boolean)
E
ester.zhou 已提交
806

807
Sets whether to block online downloads.
E
ester.zhou 已提交
808

809
**Parameters**
810

811 812 813
| Name| Type| Mandatory| Default Value| Description                           |
| ------ | -------- | ---- | ------ | ----------------------------------- |
| block  | boolean  | Yes  | false  | Whether to block online downloads.|
814

815
**Example**
816

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State block: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .blockNetwork(this.block)
      }
    }
  }
  ```
E
ester.zhou 已提交
833

834
### defaultFixedFontSize<sup>9+</sup>
835

836
defaultFixedFontSize(size: number)
E
ester.zhou 已提交
837

838 839 840 841 842 843 844
Sets the default fixed font size for the web page.

**Parameters**

| Name| Type| Mandatory| Default Value| Description                    |
| ------ | -------- | ---- | ------ | ---------------------------- |
| size   | number   | Yes  | 13     | Default fixed font size to set, in px. The value ranges from -2^31 to 2^31-1. In actual rendering, values greater than 72 are handled as 72, and values less than 1 are handled as 1. |
845 846

**Example**
E
ester.zhou 已提交
847

848 849
  ```ts
  // xxx.ets
850
  import web_webview from '@ohos.web.webview'
851 852 853
  @Entry
  @Component
  struct WebComponent {
854 855
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 16
856 857
    build() {
      Column() {
E
ester.zhou 已提交
858
        Web({ src: 'www.example.com', controller: this.controller })
859
          .defaultFixedFontSize(this.fontSize)
860 861 862 863 864
      }
    }
  }
  ```

865
### defaultFontSize<sup>9+</sup>
866

867
defaultFontSize(size: number)
868

869
Sets the default font size for the web page.
870 871

**Parameters**
E
ester.zhou 已提交
872

873 874 875
| Name| Type| Mandatory| Default Value| Description                |
| ------ | -------- | ---- | ------ | ------------------------ |
| size   | number   | Yes  | 16     | Default font size to set, in px. The value ranges from -2^31 to 2^31-1. In actual rendering, values greater than 72 are handled as 72, and values less than 1 are handled as 1. |
876 877

**Example**
E
ester.zhou 已提交
878

879 880
  ```ts
  // xxx.ets
881
  import web_webview from '@ohos.web.webview'
882 883 884
  @Entry
  @Component
  struct WebComponent {
885 886
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
887 888 889
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
890
          .defaultFontSize(this.fontSize)
891 892 893 894 895
      }
    }
  }
  ```

896
### minFontSize<sup>9+</sup>
897

898
minFontSize(size: number)
899

900
Sets the minimum font size for the web page.
901 902

**Parameters**
E
ester.zhou 已提交
903

904 905 906
| Name| Type| Mandatory| Default Value| Description                |
| ------ | -------- | ---- | ------ | ------------------------ |
| size   | number   | Yes  | 8      | Minimum font size to set, in px. The value ranges from -2^31 to 2^31-1. In actual rendering, values greater than 72 are handled as 72, and values less than 1 are handled as 1. |
907 908

**Example**
E
ester.zhou 已提交
909

910 911
  ```ts
  // xxx.ets
912
  import web_webview from '@ohos.web.webview'
913 914 915
  @Entry
  @Component
  struct WebComponent {
916 917
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
918 919 920
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
921
          .minFontSize(this.fontSize)
922 923 924 925 926
      }
    }
  }
  ```

927
### minLogicalFontSize<sup>9+</sup>
928

929
minLogicalFontSize(size: number)
E
ester.zhou 已提交
930

931
Sets the minimum logical font size for the web page.
932

933
**Parameters**
E
ester.zhou 已提交
934

935 936 937
| Name| Type| Mandatory| Default Value| Description                |
| ------ | -------- | ---- | ------ | ------------------------ |
| size   | number   | Yes  | 8      | Minimum logical font size to set, in px. The value ranges from -2^31 to 2^31-1. In actual rendering, values greater than 72 are handled as 72, and values less than 1 are handled as 1. |
938 939

**Example**
E
ester.zhou 已提交
940

941 942
  ```ts
  // xxx.ets
943
  import web_webview from '@ohos.web.webview'
944 945 946
  @Entry
  @Component
  struct WebComponent {
947 948
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
949 950
    build() {
      Column() {
E
ester.zhou 已提交
951
        Web({ src: 'www.example.com', controller: this.controller })
952
          .minLogicalFontSize(this.fontSize)
E
ester.zhou 已提交
953
      }
954 955 956 957 958
    }
  }
  ```


959
### webFixedFont<sup>9+</sup>
960

961
webFixedFont(family: string)
E
ester.zhou 已提交
962

963
Sets the fixed font family for the web page.
964

965
**Parameters**
E
ester.zhou 已提交
966

967 968 969
| Name| Type| Mandatory| Default Value   | Description                    |
| ------ | -------- | ---- | --------- | ---------------------------- |
| family | string   | Yes  | monospace | Fixed font family to set.|
970 971

**Example**
E
ester.zhou 已提交
972

973 974
  ```ts
  // xxx.ets
975
  import web_webview from '@ohos.web.webview'
976 977 978
  @Entry
  @Component
  struct WebComponent {
979 980
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "monospace"
981 982 983
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
984
          .webFixedFont(this.family)
985 986 987 988 989
      }
    }
  }
  ```

990
### webSansSerifFont<sup>9+</sup>
991

992 993 994
webSansSerifFont(family: string)

Sets the sans serif font family for the web page.
995 996

**Parameters**
E
ester.zhou 已提交
997

998 999 1000
| Name| Type| Mandatory| Default Value    | Description                         |
| ------ | -------- | ---- | ---------- | --------------------------------- |
| family | string   | Yes  | sans-serif | Sans serif font family to set.|
1001 1002

**Example**
E
ester.zhou 已提交
1003

1004 1005
  ```ts
  // xxx.ets
1006
  import web_webview from '@ohos.web.webview'
1007 1008 1009
  @Entry
  @Component
  struct WebComponent {
1010 1011
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
1012 1013 1014
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1015
          .webSansSerifFont(this.family)
1016 1017 1018 1019 1020
      }
    }
  }
  ```

1021
### webSerifFont<sup>9+</sup>
1022

1023
webSerifFont(family: string)
1024

1025
Sets the serif font family for the web page.
1026 1027

**Parameters**
E
ester.zhou 已提交
1028

1029 1030 1031
| Name| Type| Mandatory| Default Value| Description                    |
| ------ | -------- | ---- | ------ | ---------------------------- |
| family | string   | Yes  | serif  | Serif font family to set.|
1032 1033

**Example**
E
ester.zhou 已提交
1034

1035 1036
  ```ts
  // xxx.ets
1037
  import web_webview from '@ohos.web.webview'
1038 1039 1040
  @Entry
  @Component
  struct WebComponent {
1041 1042
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "serif"
1043 1044 1045
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1046
          .webSerifFont(this.family)
1047 1048 1049 1050 1051
      }
    }
  }
  ```

1052
### webStandardFont<sup>9+</sup>
1053

1054
webStandardFont(family: string)
1055

1056
Sets the standard font family for the web page.
1057 1058

**Parameters**
E
ester.zhou 已提交
1059

1060 1061 1062
| Name| Type| Mandatory| Default Value    | Description                       |
| ------ | -------- | ---- | ---------- | ------------------------------- |
| family | string   | Yes  | sans serif | Standard font family to set.|
1063 1064

**Example**
E
ester.zhou 已提交
1065

1066 1067
  ```ts
  // xxx.ets
1068
  import web_webview from '@ohos.web.webview'
1069 1070 1071
  @Entry
  @Component
  struct WebComponent {
1072 1073
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
1074 1075 1076
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1077
          .webStandardFont(this.family)
1078 1079 1080 1081 1082
      }
    }
  }
  ```

1083
### webFantasyFont<sup>9+</sup>
1084

1085
webFantasyFont(family: string)
1086

1087
Sets the fantasy font family for the web page.
1088 1089

**Parameters**
E
ester.zhou 已提交
1090

1091 1092 1093
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | Yes  | fantasy | Fantasy font family to set.|
1094 1095

**Example**
E
ester.zhou 已提交
1096

1097 1098
  ```ts
  // xxx.ets
1099
  import web_webview from '@ohos.web.webview'
1100 1101 1102
  @Entry
  @Component
  struct WebComponent {
1103 1104
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "fantasy"
1105 1106 1107
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1108
          .webFantasyFont(this.family)
1109 1110 1111 1112 1113
      }
    }
  }
  ```

1114
### webCursiveFont<sup>9+</sup>
1115

1116
webCursiveFont(family: string)
1117

1118
Sets the cursive font family for the web page.
1119 1120

**Parameters**
E
ester.zhou 已提交
1121

1122 1123 1124
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | Yes  | cursive | Cursive font family to set.|
1125 1126

**Example**
E
ester.zhou 已提交
1127

1128 1129
  ```ts
  // xxx.ets
1130
  import web_webview from '@ohos.web.webview'
1131 1132 1133
  @Entry
  @Component
  struct WebComponent {
1134 1135
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "cursive"
1136 1137 1138
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1139
          .webCursiveFont(this.family)
1140 1141 1142 1143 1144
      }
    }
  }
  ```

1145
### darkMode<sup>9+</sup>
1146

1147
darkMode(mode: WebDarkMode)
1148

1149
Sets the web dark mode. By default, web dark mode is disabled. When it is enabled, the **\<Web>** component enables the dark theme defined for web pages if the theme has been defined in **prefer-color-scheme** of a media query, and remains unchanged otherwise. To enable the forcible dark mode, use this API with [forceDarkAccess](#forcedarkaccess9).
1150 1151

**Parameters**
E
ester.zhou 已提交
1152

1153 1154 1155
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | ----------- | ---- | --------------- | ------------------ |
|  mode  | [WebDarkMode](#webdarkmode9) | Yes  | WebDarkMode.Off | Web dark mode to set.|
1156

E
ester.zhou 已提交
1157 1158
**Example**

1159 1160
  ```ts
  // xxx.ets
1161
  import web_webview from '@ohos.web.webview'
1162 1163 1164
  @Entry
  @Component
  struct WebComponent {
1165 1166
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
1167 1168 1169
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1170
          .darkMode(this.mode)
1171 1172 1173 1174 1175
      }
    }
  }
  ```

1176
### forceDarkAccess<sup>9+</sup>
1177

1178
forceDarkAccess(access: boolean)
1179

1180
Sets whether to enable forcible dark mode for the web page. By default, this feature is turned off. This API is applicable only when dark mode is enabled in [darkMode](#darkmode9).
1181 1182

**Parameters**
E
ester.zhou 已提交
1183

1184 1185 1186
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | ------- | ---- | ----- | ------------------ |
| access | boolean | Yes  | false | Whether to enable forcible dark mode for the web page.|
1187

E
ester.zhou 已提交
1188 1189
**Example**

1190 1191
  ```ts
  // xxx.ets
1192
  import web_webview from '@ohos.web.webview'
1193 1194 1195
  @Entry
  @Component
  struct WebComponent {
1196 1197 1198
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
    @State access: boolean = true
1199 1200 1201
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1202 1203
          .darkMode(this.mode)
          .forceDarkAccess(this.access)
1204 1205 1206 1207 1208
      }
    }
  }
  ```

1209
### pinchSmooth<sup>9+</sup>
1210

1211
pinchSmooth(isEnabled: boolean)
1212

1213
Sets whether to enable smooth pinch mode for the web page.
1214 1215

**Parameters**
E
ester.zhou 已提交
1216

1217 1218 1219
| Name   | Type| Mandatory| Default Value| Description                  |
| --------- | -------- | ---- | ------ | -------------------------- |
| isEnabled | boolean  | Yes  | false  | Whether to enable smooth pinch mode for the web page.|
E
ester.zhou 已提交
1220 1221

**Example**
1222 1223

  ```ts
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'www.example.com', controller: this.controller })
        .pinchSmooth(true)
1234 1235
    }
  }
1236
}
1237 1238
  ```

1239
## Events
1240

1241
The universal events are not supported.
1242

1243 1244 1245 1246
### onAlert

onAlert(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

1247
Called when **alert()** is invoked to display an alert dialog box on the web page.
1248 1249

**Parameters**
E
ester.zhou 已提交
1250

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
1261
| boolean | If the callback returns **true**, the application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to instruct the **\<Web>** component to exit the current page based on the user operation. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
1262

E
ester.zhou 已提交
1263 1264
**Example**

1265 1266 1267 1268 1269
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1270
    controller: WebController = new WebController()
1271 1272
    build() {
      Column() {
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        Web({ src: 'www.example.com', controller: this.controller })
          .onAlert((event) => {
            AlertDialog.show({
              title: 'onAlert',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
1295 1296 1297 1298 1299 1300
          })
      }
    }
  }
  ```

1301
### onBeforeUnload
1302

1303
onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)
1304

1305
Called when this page is about to exit after the user refreshes or closes the page. This API takes effect only when the page has obtained focus.
1306 1307

**Parameters**
E
ester.zhou 已提交
1308

1309 1310 1311 1312 1313
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |
1314

E
ester.zhou 已提交
1315 1316
**Return value**

1317 1318
| Type     | Description                                      |
| ------- | ---------------------------------------- |
1319
| boolean | If the callback returns **true**, the application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to instruct the **\<Web>** component to exit the current page based on the user operation. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
1320 1321 1322 1323

**Example**

  ```ts
1324 1325 1326 1327
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1328
    controller: WebController = new WebController()
1329
  
1330 1331 1332
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1333 1334 1335
          .onBeforeUnload((event) => {
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
1336
            AlertDialog.show({
1337 1338 1339 1340
              title: 'onBeforeUnload',
              message: 'text',
              primaryButton: {
                value: 'cancel',
1341
                action: () => {
1342 1343 1344 1345 1346 1347 1348
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
1349 1350 1351
                }
              },
              cancel: () => {
1352
                event.result.handleCancel()
1353 1354
              }
            })
E
ester.zhou 已提交
1355 1356 1357 1358 1359 1360 1361
            return true
          })
      }
    }
  }
  ```

1362
### onConfirm
E
ester.zhou 已提交
1363

1364
onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)
E
ester.zhou 已提交
1365

1366
Called when **confirm()** is invoked by the web page.
E
ester.zhou 已提交
1367 1368 1369

**Parameters**

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
1380
| boolean | If the callback returns **true**, the application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to instruct the **\<Web>** component to exit the current page based on the user operation. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
          .onConfirm((event) => {
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            console.log("event.result:" + event.result)
            AlertDialog.show({
              title: 'onConfirm',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
E
ester.zhou 已提交
1418 1419 1420 1421 1422 1423
          })
      }
    }
  }
  ```

1424
### onPrompt<sup>9+</sup>
E
ester.zhou 已提交
1425

1426
onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean)
E
ester.zhou 已提交
1427 1428 1429

**Parameters**

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
1440
| boolean | If the callback returns **true**, the application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to instruct the **\<Web>** component to exit the current page based on the user operation. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
          .onPrompt((event) => {
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
            AlertDialog.show({
              title: 'onPrompt',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
1478 1479 1480 1481 1482 1483
          })
      }
    }
  }
  ```

1484
### onConsole
1485

1486
onConsole(callback: (event?: { message: ConsoleMessage }) => boolean)
1487

1488
Called to notify the host application of a JavaScript console message.
1489 1490

**Parameters**
E
ester.zhou 已提交
1491

1492 1493 1494
| Name    | Type                             | Description     |
| ------- | --------------------------------- | --------- |
| message | [ConsoleMessage](#consolemessage) | Console message.|
1495 1496

**Return value**
E
ester.zhou 已提交
1497

1498 1499 1500
| Type     | Description                                 |
| ------- | ----------------------------------- |
| boolean | Returns **true** if the message will not be printed to the console; returns **false** otherwise.|
1501 1502 1503 1504 1505 1506 1507 1508

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1509
    controller: WebController = new WebController()
1510 1511 1512 1513
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1514 1515 1516 1517 1518 1519
          .onConsole((event) => {
            console.log('getMessage:' + event.message.getMessage())
            console.log('getSourceId:' + event.message.getSourceId())
            console.log('getLineNumber:' + event.message.getLineNumber())
            console.log('getMessageLevel:' + event.message.getMessageLevel())
            return false
1520 1521 1522 1523 1524 1525
          })
      }
    }
  }
  ```

1526
### onDownloadStart
1527

1528
onDownloadStart(callback: (event?: { url: string, userAgent: string, contentDisposition: string, mimetype: string, contentLength: number }) => void)
1529 1530

**Parameters**
E
ester.zhou 已提交
1531

1532 1533 1534 1535 1536 1537
| Name               | Type         | Description                               |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | URL for the download task.                          |
| contentDisposition | string        | Content-Disposition response header returned by the server, which may be empty.|
| mimetype           | string        | MIME type of the content returned by the server.               |
| contentLength      | contentLength | Length of the content returned by the server.                        |
E
ester.zhou 已提交
1538 1539

**Example**
1540 1541 1542 1543 1544 1545

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1546
    controller: WebController = new WebController()
1547
  
1548 1549
    build() {
      Column() {
E
ester.zhou 已提交
1550
        Web({ src: 'www.example.com', controller: this.controller })
1551 1552 1553 1554 1555 1556
          .onDownloadStart((event) => {
            console.log('url:' + event.url)
            console.log('userAgent:' + event.userAgent)
            console.log('contentDisposition:' + event.contentDisposition)
            console.log('contentLength:' + event.contentLength)
            console.log('mimetype:' + event.mimetype)
1557 1558 1559 1560 1561 1562
          })
      }
    }
  }
  ```

1563
### onErrorReceive
1564

1565
onErrorReceive(callback: (event?: { request: WebResourceRequest, error: WebResourceError }) => void)
1566

E
ester.zhou 已提交
1567
Called when an error occurs during web page loading. For better results, simplify the implementation logic in the callback. This API is called when there is no network connection.
1568 1569

**Parameters**
E
ester.zhou 已提交
1570

1571 1572 1573 1574
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| error   | [WebResourceError](#webresourceerror)    | Encapsulation of a web page resource loading error.|
1575 1576 1577 1578 1579 1580 1581 1582

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1583
    controller: WebController = new WebController()
E
ester.zhou 已提交
1584
  
1585 1586
    build() {
      Column() {
E
ester.zhou 已提交
1587
        Web({ src: 'www.example.com', controller: this.controller })
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
          .onErrorReceive((event) => {
            console.log('getErrorInfo:' + event.error.getErrorInfo())
            console.log('getErrorCode:' + event.error.getErrorCode())
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
            }
          })
      }
    }
E
ester.zhou 已提交
1604 1605 1606
  }
  ```

1607
### onHttpErrorReceive
E
ester.zhou 已提交
1608

1609 1610
onHttpErrorReceive(callback: (event?: { request: WebResourceRequest, response: WebResourceResponse }) => void)

1611
Called when an HTTP error (the response code is greater than or equal to 400) occurs during web page resource loading.
E
ester.zhou 已提交
1612 1613 1614

**Parameters**

1615 1616 1617 1618
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| response | [WebResourceResponse](#webresourceresponse)    | Encapsulation of a resource response.|
E
ester.zhou 已提交
1619 1620 1621 1622 1623 1624 1625 1626

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1627
    controller: WebController = new WebController()
E
ester.zhou 已提交
1628 1629 1630 1631
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
          .onHttpErrorReceive((event) => {
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getResponseData:' + event.response.getResponseData())
            console.log('getResponseEncoding:' + event.response.getResponseEncoding())
            console.log('getResponseMimeType:' + event.response.getResponseMimeType())
            console.log('getResponseCode:' + event.response.getResponseCode())
            console.log('getReasonMessage:' + event.response.getReasonMessage())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
            for (let i of resph) {
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
E
ester.zhou 已提交
1652 1653 1654 1655 1656 1657
          })
      }
    }
  }
  ```

1658
### onPageBegin
E
ester.zhou 已提交
1659

1660
onPageBegin(callback: (event?: { url: string }) => void)
E
ester.zhou 已提交
1661

1662

1663
Called when the web page starts to be loaded. This API is called only for the main frame content, and not for the iframe or frameset content.
E
ester.zhou 已提交
1664 1665 1666

**Parameters**

1667 1668 1669 1670 1671
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|

**Example**
E
ester.zhou 已提交
1672 1673 1674 1675 1676 1677

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1678
    controller: WebController = new WebController()
1679
  
E
ester.zhou 已提交
1680 1681 1682
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1683 1684
          .onPageBegin((event) => {
            console.log('url:' + event.url)
E
ester.zhou 已提交
1685 1686
          })
      }
1687 1688 1689
    }
  }
  ```
E
ester.zhou 已提交
1690

1691
### onPageEnd
1692

1693
onPageEnd(callback: (event?: { url: string }) => void)
1694

1695

1696
Called when the web page loading is complete. This API takes effect only for the main frame content.
1697 1698

**Parameters**
E
ester.zhou 已提交
1699

1700 1701 1702
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|
1703

E
ester.zhou 已提交
1704 1705 1706 1707 1708 1709 1710
**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1711
    controller: WebController = new WebController()
1712
  
E
ester.zhou 已提交
1713 1714 1715
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1716 1717
          .onPageEnd((event) => {
            console.log('url:' + event.url)
E
ester.zhou 已提交
1718 1719 1720 1721 1722 1723
          })
      }
    }
  }
  ```

1724
### onProgressChange
E
ester.zhou 已提交
1725

1726
onProgressChange(callback: (event?: { newProgress: number }) => void)
E
ester.zhou 已提交
1727

1728
Called when the web page loading progress changes.
E
ester.zhou 已提交
1729 1730 1731

**Parameters**

1732 1733 1734
| Name        | Type  | Description                 |
| ----------- | ------ | --------------------- |
| newProgress | number | New loading progress. The value is an integer ranging from 0 to 100.|
E
ester.zhou 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
1744
  
E
ester.zhou 已提交
1745 1746 1747
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1748 1749 1750
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
E
ester.zhou 已提交
1751 1752 1753 1754 1755
      }
    }
  }
  ```

1756
### onTitleReceive
E
ester.zhou 已提交
1757

1758
onTitleReceive(callback: (event?: { title: string }) => void)
E
ester.zhou 已提交
1759

1760
Called when the document title of the web page is changed.
E
ester.zhou 已提交
1761 1762 1763

**Parameters**

1764 1765 1766
| Name  | Type  | Description         |
| ----- | ------ | ------------- |
| title | string | Document title.|
E
ester.zhou 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
1776
  
E
ester.zhou 已提交
1777 1778 1779
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1780 1781 1782
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
E
ester.zhou 已提交
1783 1784 1785 1786 1787
      }
    }
  }
  ```

1788
### onRefreshAccessedHistory
E
ester.zhou 已提交
1789

1790
onRefreshAccessedHistory(callback: (event?: { url: string, isRefreshed: boolean }) => void)
E
ester.zhou 已提交
1791

E
ester.zhou 已提交
1792
Called when loading of the web page is complete. This API is used by an application to update the historical link it accessed.
E
ester.zhou 已提交
1793 1794 1795

**Parameters**

1796 1797 1798 1799
| Name        | Type   | Description                                    |
| ----------- | ------- | ---------------------------------------- |
| url         | string  | URL to be accessed.                                 |
| isRefreshed | boolean | Whether the page is reloaded. The value **true** means that the page is reloaded by invoking the [refresh](#refresh) API, and **false** means the opposite.|
E
ester.zhou 已提交
1800 1801 1802 1803 1804 1805 1806 1807

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1808 1809
    controller: WebController = new WebController()
  
E
ester.zhou 已提交
1810 1811
    build() {
      Column() {
1812 1813 1814
        Web({ src: 'www.example.com', controller: this.controller })
          .onRefreshAccessedHistory((event) => {
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
E
ester.zhou 已提交
1815 1816 1817 1818 1819 1820
          })
      }
    }
  }
  ```

1821
### onRenderExited<sup>9+</sup>
E
ester.zhou 已提交
1822

1823
onRenderExited(callback: (event?: { renderExitReason: RenderExitReason }) => void)
E
ester.zhou 已提交
1824

1825
Called when the rendering process exits abnormally.
E
ester.zhou 已提交
1826 1827 1828

**Parameters**

1829 1830 1831
| Name             | Type                                    | Description            |
| ---------------- | ---------------------------------------- | ---------------- |
| renderExitReason | [RenderExitReason](#renderexitreason)| Cause for the abnormal exit of the rendering process.|
E
ester.zhou 已提交
1832 1833 1834 1835 1836 1837 1838 1839

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1840 1841
    controller: WebController = new WebController()
  
E
ester.zhou 已提交
1842 1843
    build() {
      Column() {
1844 1845 1846 1847
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
            console.log('reason:' + event.renderExitReason)
          })
E
ester.zhou 已提交
1848 1849 1850 1851 1852
      }
    }
  }
  ```

1853
### onShowFileSelector<sup>9+</sup>
E
ester.zhou 已提交
1854

1855
onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => boolean)
E
ester.zhou 已提交
1856

1857
Called to process an HTML form whose input type is **file**, in response to the tapping of the **Select File** button.
E
ester.zhou 已提交
1858 1859 1860

**Parameters**

1861 1862 1863 1864 1865 1866 1867 1868 1869
| Name         | Type                                    | Description             |
| ------------ | ---------------------------------------- | ----------------- |
| result       | [FileSelectorResult](#fileselectorresult9) | File selection result to be sent to the **\<Web>** component.|
| fileSelector | [FileSelectorParam](#fileselectorparam9) | Information about the file selector.      |

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
1870
| boolean | The value **true** means that the pop-up window provided by the system is displayed. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
1871 1872 1873 1874 1875 1876 1877 1878

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1879 1880
    controller: WebController = new WebController()

E
ester.zhou 已提交
1881 1882
    build() {
      Column() {
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
        Web({ src: 'www.example.com', controller: this.controller })
          .onShowFileSelector((event) => {
            AlertDialog.show({
              title: event.fileSelector.getTitle(),
              message: 'isCapture:' + event.fileSelector.isCapture() + " mode:" + event.fileSelector.getMode() + 'acceptType:' + event.fileSelector.getAcceptType(),
              confirm: {
                value: 'upload',
                action: () => {
                  let fileList: Array<string> = [
                    '/data/storage/el2/base/test',
                  ]
                  event.result.handleFileList(fileList)
                }
              },
              cancel: () => {
                let fileList: Array<string> = []
                event.result.handleFileList(fileList)
              }
            })
            return true
          })
E
ester.zhou 已提交
1904 1905 1906 1907 1908
      }
    }
  }
  ```

1909
### onResourceLoad<sup>9+</sup>
E
ester.zhou 已提交
1910

1911
onResourceLoad(callback: (event: {url: string}) => void)
E
ester.zhou 已提交
1912

1913
Called to notify the **\<Web>** component of the URL of the loaded resource file.
E
ester.zhou 已提交
1914

E
ester.zhou 已提交
1915
**Parameters**
E
ester.zhou 已提交
1916

1917 1918 1919
| Name | Type  | Description          |
| ---- | ------ | -------------- |
| url  | string | URL of the loaded resource file.|
E
ester.zhou 已提交
1920 1921 1922 1923 1924 1925 1926 1927

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1928 1929
    controller: WebController = new WebController()
  
E
ester.zhou 已提交
1930 1931
    build() {
      Column() {
1932 1933 1934 1935
        Web({ src: 'www.example.com', controller: this.controller })
          .onResourceLoad((event) => {
            console.log('onResourceLoad: ' + event.url)
          })
E
ester.zhou 已提交
1936 1937 1938 1939 1940
      }
    }
  }
  ```

1941
### onScaleChange<sup>9+</sup>
E
ester.zhou 已提交
1942

1943
onScaleChange(callback: (event: {oldScale: number, newScale: number}) => void)
E
ester.zhou 已提交
1944

1945
Called when the display ratio of this page changes.
E
ester.zhou 已提交
1946 1947 1948

**Parameters**

1949 1950 1951 1952
| Name     | Type  | Description        |
| -------- | ------ | ------------ |
| oldScale | number | Display ratio of the page before the change.|
| newScale | number | Display ratio of the page after the change.|
E
ester.zhou 已提交
1953 1954 1955 1956 1957 1958 1959 1960

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1961 1962
    controller: WebController = new WebController()
  
E
ester.zhou 已提交
1963 1964
    build() {
      Column() {
1965 1966 1967 1968
        Web({ src: 'www.example.com', controller: this.controller })
          .onScaleChange((event) => {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
          })
E
ester.zhou 已提交
1969 1970 1971 1972 1973
      }
    }
  }
  ```

1974
### onUrlLoadIntercept
E
ester.zhou 已提交
1975

1976
onUrlLoadIntercept(callback: (event?: { data:string | WebResourceRequest }) => boolean)
E
ester.zhou 已提交
1977

E
ester.zhou 已提交
1978
Called when the **\<Web>** component is about to access a URL. This API is used to determine whether to block the access, which is allowed by default.
E
ester.zhou 已提交
1979 1980 1981

**Parameters**

1982 1983 1984 1985 1986 1987 1988 1989 1990
| Name | Type                                    | Description     |
| ---- | ---------------------------------------- | --------- |
| data | string / [WebResourceRequest](#webresourcerequest) | URL information.|

**Return value**

| Type     | Description                      |
| ------- | ------------------------ |
| boolean | Returns **true** if the access is blocked; returns **false** otherwise.|
E
ester.zhou 已提交
1991

1992 1993 1994 1995 1996 1997 1998
**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
1999 2000
    controller: WebController = new WebController()
  
2001 2002
    build() {
      Column() {
2003 2004 2005 2006 2007
        Web({ src: 'www.example.com', controller: this.controller })
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
            return true
          })
E
ester.zhou 已提交
2008
      }
2009 2010 2011 2012
    }
  }
  ```

2013
### onInterceptRequest<sup>9+</sup>
E
ester.zhou 已提交
2014

2015
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
2016

2017
Called when the **\<Web>** component is about to access a URL. This API is used to block the URL and return the response data.
2018

2019
**Parameters**
2020

2021 2022 2023
| Name    | Type                                    | Description       |
| ------- | ---------------------------------------- | ----------- |
| request | [WebResourceRequest](#webresourcerequest) | Information about the URL request.|
2024 2025

**Return value**
E
ester.zhou 已提交
2026

2027 2028 2029
| Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- |
| [WebResourceResponse](#webresourceresponse) | If response data is returned, the data is loaded based on the response data. If no response data is returned, null is returned, indicating that the data is loaded in the original mode.|
2030

2031
**Example**
2032

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
    @State webdata: string = "<!DOCTYPE html>\n" +
    "<html>\n"+
    "<head>\n"+
    "<title>intercept test</title>\n"+
    "</head>\n"+
    "<body>\n"+
    "<h1>intercept test</h1>\n"+
    "</body>\n"+
    "</html>"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onInterceptRequest((event) => {
            console.log('url:' + event.request.getRequestUrl())
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
            var length = this.heads.push(head1)
            length = this.heads.push(head2)
            this.responseweb.setResponseHeader(this.heads)
            this.responseweb.setResponseData(this.webdata)
            this.responseweb.setResponseEncoding('utf-8')
            this.responseweb.setResponseMimeType('text/html')
            this.responseweb.setResponseCode(200)
            this.responseweb.setReasonMessage('OK')
            return this.responseweb
          })
      }
    }
  }
  ```
2077

2078
### onHttpAuthRequest<sup>9+</sup>
E
ester.zhou 已提交
2079

2080
onHttpAuthRequest(callback: (event?: { handler: HttpAuthHandler, host: string, realm: string}) => boolean)
2081

2082
Called when an HTTP authentication request is received.
2083

2084
**Parameters**
2085

2086 2087 2088 2089 2090
| Name    | Type                                | Description            |
| ------- | ------------------------------------ | ---------------- |
| handler | [HttpAuthHandler](#httpauthhandler9) | User operation.  |
| host    | string                               | Host to which HTTP authentication credentials apply.|
| realm   | string                               | Realm to which HTTP authentication credentials apply. |
2091 2092

**Return value**
E
ester.zhou 已提交
2093

2094 2095 2096
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
2097

2098
**Example**
2099

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    httpAuth: boolean = false
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
              title: 'onHttpAuthRequest',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.handler.cancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
                  if (this.httpAuth == false) {
                    web_webview.WebDataBase.saveHttpAuthCredentials(
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
                    event.handler.cancel()
                  }
                }
              },
              cancel: () => {
                event.handler.cancel()
              }
            })
            return true
          })
      }
    }
  }
  ```
### onSslErrorEventReceive<sup>9+</sup>
2148

2149
onSslErrorEventReceive(callback: (event: { handler: SslErrorHandler, error: SslError }) => void)
2150

2151
Called when an SSL error occurs during resource loading.
2152

2153
**Parameters**
2154

2155 2156 2157 2158
| Name    | Type                                | Description          |
| ------- | ------------------------------------ | -------------- |
| handler | [SslErrorHandler](#sslerrorhandler9) | User operation.|
| error   | [SslError](#sslerror9)          | Error code.          |
2159

2160
**Example**
2161

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onSslErrorEventReceive((event) => {
            AlertDialog.show({
              title: 'onSslErrorEventReceive',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
                  event.handler.handleConfirm()
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
                  event.handler.handleCancel()
                }
              },
              cancel: () => {
                event.handler.handleCancel()
              }
            })
            return true
          })
      }
    }
  }
  ```

### onClientAuthenticationRequest<sup>9+</sup>

onClientAuthenticationRequest(callback: (event: {handler : ClientAuthenticationHandler, host : string, port : number, keyTypes : Array<string>, issuers : Array<string>}) => void)

2204
Called when an SSL client certificate request is received.
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258

**Parameters**

| Name     | Type                                    | Description           |
| -------- | ---------------------------------------- | --------------- |
| handler  | [ClientAuthenticationHandler](#clientauthenticationhandler9) | User operation. |
| host     | string                                   | Host name of the server that requests a certificate.   |
| port     | number                                   | Port number of the server that requests a certificate.   |
| keyTypes | Array<string>                            | Acceptable asymmetric private key types.   |
| issuers  | Array<string>                            | Issuer of the certificate that matches the private key.|

  **Example**
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'onClientAuthenticationRequest',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
                  event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem")
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
                  event.handler.cancel()
                }
              },
              cancel: () => {
                event.handler.ignore()
              }
            })
            return true
          })
      }
    }
  }
  ```

### onPermissionRequest<sup>9+</sup>

onPermissionRequest(callback: (event?: { request: PermissionRequest }) => void)

2259
Called when a permission request is received.
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347

**Parameters**

| Name    | Type                                    | Description          |
| ------- | ---------------------------------------- | -------------- |
| request | [PermissionRequest](#permissionrequest9) | User operation.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              primaryButton: {
                value: 'deny',
                action: () => {
                  event.request.deny()
                }
              },
              secondaryButton: {
                value: 'onConfirm',
                action: () => {
                  event.request.grant(event.request.getAccessibleResource())
                }
              },
              cancel: () => {
                event.request.deny()
              }
            })
          })
      }
    }
  }
  ```

### onContextMenuShow<sup>9+</sup>

onContextMenuShow(callback: (event?: { param: WebContextMenuParam, result: WebContextMenuResult }) => boolean)

Shows a context menu after the user clicks the right mouse button or long presses a specific element, such as an image or a link.

**Parameters**

| Name   | Type                                    | Description       |
| ------ | ---------------------------------------- | ----------- |
| param  | [WebContextMenuParam](#webcontextmenuparam9) | Parameters related to the context menu.    |
| result | [WebContextMenuResult](#webcontextmenuresult9) | Result of the context menu.|

**Return value**

| Type     | Description                      |
| ------- | ------------------------ |
| boolean | The value **true** means a custom menu, and **false** means the default menu.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onContextMenuShow((event) => {
            console.info("x coord = " + event.param.x())
            console.info("link url = " + event.param.getLinkUrl())
            return true
        })
      }
    }
  }
  ```

### onScroll<sup>9+</sup>

onScroll(callback: (event: {xOffset: number, yOffset: number}) => void)

2348
Called when the scrollbar of the page scrolls.
2349 2350 2351 2352 2353

**Parameters**

| Name    | Type  | Description        |
| ------- | ------ | ------------ |
E
ester.zhou 已提交
2354 2355
| xOffset | number | Position of the scrollbar on the x-axis relative to the leftmost of the web page.|
| yOffset | number | Position of the scrollbar on the y-axis relative to the top of the web page.|
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .onScroll((event) => {
            console.info("x = " + event.xOffset)
            console.info("y = " + event.yOffset)
        })
      }
    }
  }
  ```

### onGeolocationShow

onGeolocationShow(callback: (event?: { origin: string, geolocation: JsGeolocation }) => void)

Registers a callback for receiving a request to obtain the geolocation information.

**Parameters**

| Name        | Type                           | Description          |
| ----------- | ------------------------------- | -------------- |
| origin      | string                          | Index of the origin.    |
| geolocation | [JsGeolocation](#jsgeolocation) | User operation.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationShow((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
                event.geolocation.invoke(event.origin, true, true)
              }
            },
            cancel: () => {
              event.geolocation.invoke(event.origin, false, true)
            }
          })
        })
      }
    }
  }
  ```

### onGeolocationHide

onGeolocationHide(callback: () => void)

2426
Called to notify the user that the request for obtaining the geolocation information received when **[onGeolocationShow](#ongeolocationshow)** is called has been canceled.
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457

**Parameters**

| Name     | Type      | Description                |
| -------- | ---------- | -------------------- |
| callback | () => void | Callback invoked when the request for obtaining geolocation information has been canceled. |

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationHide(() => {
          console.log("onGeolocationHide...")
        })
      }
    }
  }
  ```

### onFullScreenEnter<sup>9+</sup>

onFullScreenEnter(callback: (event: { handler: FullScreenExitHandler }) => void)

2458
Called when the component enters full screen mode.
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490

**Parameters**

| Name    | Type                                    | Description          |
| ------- | ---------------------------------------- | -------------- |
| handler | [FullScreenExitHandler](#fullscreenexithandler9) | Function handle for exiting full screen mode.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController()
    handler: FullScreenExitHandler = null
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenEnter((event) => {
          console.log("onFullScreenEnter...")
          this.handler = event.handler
        })
      }
    }
  }
  ```

### onFullScreenExit<sup>9+</sup>

onFullScreenExit(callback: () => void)

2491
Called when the component exits full screen mode.
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595

**Parameters**

| Name     | Type      | Description         |
| -------- | ---------- | ------------- |
| callback | () => void | Callback invoked when the component exits full screen mode.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController()
    handler: FullScreenExitHandler = null
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenExit(() => {
          console.log("onFullScreenExit...")
          this.handler.exitFullScreen()
        })
        .onFullScreenEnter((event) => {
          this.handler = event.handler
        })
      }
    }
  }
  ```

### onWindowNew<sup>9+</sup>

onWindowNew(callback: (event: {isAlert: boolean, isUserTrigger: boolean, targetUrl: string, handler: ControllerHandler}) => void)

Registers a callback for window creation.

**Parameters**

| Name          | Type                                    | Description                      |
| ------------- | ---------------------------------------- | -------------------------- |
| isAlert       | boolean                                  | Whether to open the target URL in a new window. The value **true** means to open the target URL in a new window, and **false** means to open the target URL in a new tab.|
| isUserTrigger | boolean                                  | Whether the creation is triggered by the user. The value **true** means that the creation is triggered by the user, and **false** means the opposite.  |
| targetUrl     | string                                   | Target URL.                    |
| handler       | [ControllerHandler](#controllerhandler9) | **WebController** instance for setting the new window. |

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
        .multiWindowAccess(true)
        .onWindowNew((event) => {
          console.log("onWindowNew...")
          var popController: web_webview.WebviewController = new web_webview.WebviewController()
          event.handler.setWebController(popController)
        })
      }
    }
  }
  ```

### onWindowExit<sup>9+</sup>

onWindowExit(callback: () => void)

Registers a callback for window closure.

**Parameters**

| Name     | Type      | Description        |
| -------- | ---------- | ------------ |
| callback | () => void | Callback invoked when the window closes.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
        .onWindowExit(() => {
          console.log("onWindowExit...")
        })
      }
    }
  }
  ```

### onSearchResultReceive<sup>9+</sup>

onSearchResultReceive(callback: (event?: {activeMatchOrdinal: number, numberOfMatches: number, isDoneCounting: boolean}) => void): WebAttribute

2596
Called to notify the caller of the search result on the web page.
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630

**Parameters**

| Name               | Type   | Description                                    |
| ------------------ | ------- | ---------------------------------------- |
| activeMatchOrdinal | number  | Sequence number of the current match, which starts from 0.                      |
| numberOfMatches    | number  | Total number of matches.                           |
| isDoneCounting     | boolean | Whether the search operation on the current page is complete. This API may be called multiple times until **isDoneCounting** is **true**.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting)
          })
      }
    }
  }
  ```

### onDataResubmitted<sup>9+</sup>

onDataResubmitted(callback: (event: {handler: DataResubmissionHandler}) => void)

2631
Called when the web form data is resubmitted.
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663

**Parameters**

| Name | Type                                            | Description              |
| ------- | ---------------------------------------------------- | ---------------------- |
| handler | [DataResubmissionHandler](#dataresubmissionhandler9) | Handler for resubmitting web form data.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onDataResubmitted((event) => {
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

### onPageVisible<sup>9+</sup>

onPageVisible(callback: (event: {url: string}) => void)

2664
Called when the old page is not displayed and the new page is about to be visible.
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695

**Parameters**

| Name| Type| Description                                         |
| ------ | -------- | ------------------------------------------------- |
| url    | string   | URL of the new page that is able to be visible when the old page is not displayed.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onPageVisible((event) => {
          console.log('onPageVisible url:' + event.url)
        })
      }
    }
  }
  ```

### onInterceptKeyEvent<sup>9+</sup>

onInterceptKeyEvent(callback: (event: KeyEvent) => boolean)

E
ester.zhou 已提交
2696
Called when the key event is intercepted and before it is consumed by the Webview.
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722

**Parameters**

| Name| Type                                               | Description            |
| ------ | ------------------------------------------------------- | -------------------- |
| event  | [KeyEvent](ts-universal-events-key.md#keyevent) | Key event that is triggered.|

**Return value**

| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | Whether to continue to transfer the key event to the Webview kernel.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onInterceptKeyEvent((event) => {
2723
            if (event.keyCode == 2017 || event.keyCode == 2018) {
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
            console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`)
            return true;
          }
          return false;
        })
      }
    }
  }
  ```

### onTouchIconUrlReceived<sup>9+</sup>

onTouchIconUrlReceived(callback: (event: {url: string, precomposed: boolean}) => void)

2738
Called when an apple-touch-icon URL is received.
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770

**Parameters**

| Name     | Type| Description                          |
| ----------- | -------- | ---------------------------------- |
| url         | string   | Received apple-touch-icon URL.|
| precomposed | boolean  | Whether the apple-touch-icon is precomposed.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.baidu.com', controller: this.controller })
         .onTouchIconUrlReceived((event) => {
          console.log('onTouchIconUrlReceived:' + JSON.stringify(event))
        })
      }
    }
  }
  ```

### onFaviconReceived<sup>9+</sup>

onFaviconReceived(callback: (event: {favicon: image.PixelMap}) => void)

2771
Called when this web page receives a new favicon.
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870

**Parameters**

| Name | Type                                      | Description                           |
| ------- | ---------------------------------------------- | ----------------------------------- |
| favicon | [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object of the received favicon.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  import image from "@ohos.multimedia.image"
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State icon: image.PixelMap = undefined;
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onFaviconReceived((event) => {
          console.log('onFaviconReceived:' + JSON.stringify(event))
          this.icon = event.favicon;
        })
      }
    }
  }
  ```

## ConsoleMessage

Implements the **ConsoleMessage** object. For the sample code, see [onConsole](#onconsole).

### getLineNumber

getLineNumber(): number

Obtains the number of rows in this console message.

**Return value**

| Type    | Description                  |
| ------ | -------------------- |
| number | Number of rows in the console message.|

### getMessage

getMessage(): string

Obtains the log information of this console message.

**Return value**

| Type    | Description                    |
| ------ | ---------------------- |
| string | Log information of the console message.|

### getMessageLevel

getMessageLevel(): MessageLevel

Obtains the level of this console message.

**Return value**

| Type                               | Description                    |
| --------------------------------- | ---------------------- |
| [MessageLevel](#messagelevel)| Level of the console message.|

### getSourceId

getSourceId(): string

Obtains the path and name of the web page source file.

**Return value**

| Type    | Description           |
| ------ | ------------- |
| string | Path and name of the web page source file.|

## JsResult

Implements the **JsResult** object, which indicates the result returned to the **\<Web>** component to indicate the user operation performed in the dialog box. For the sample code, see [onAlert Event](#onalert).

### handleCancel

handleCancel(): void

Notifies the **\<Web>** component of the user's cancel operation in the dialog box.

### handleConfirm

handleConfirm(): void

Notifies the **\<Web>** component of the user's confirm operation in the dialog box.

### handlePromptConfirm<sup>9+</sup>
2871 2872 2873 2874 2875 2876

handlePromptConfirm(result: string): void

Notifies the **\<Web>** component of the user's confirm operation in the dialog box as well as the dialog box content.

**Parameters**
E
ester.zhou 已提交
2877

2878 2879 2880 2881
| Name   | Type  | Mandatory  | Default Value | Description       |
| ------ | ------ | ---- | ---- | ----------- |
| result | string | Yes   | -    | User input in the dialog box.|

E
ester.zhou 已提交
2882 2883
## FullScreenExitHandler<sup>9+</sup>

2884
Implements a **FullScreenExitHandler** object for listening for exiting full screen mode. For the sample code, see [onFullScreenEnter](#onfullscreenenter9).
E
ester.zhou 已提交
2885 2886 2887 2888 2889 2890 2891 2892 2893

### exitFullScreen<sup>9+</sup>

exitFullScreen(): void

Exits full screen mode.

## ControllerHandler<sup>9+</sup>

2894
Implements a **WebviewController** object for new **\<Web>** components. For the sample code, see [onWindowNew](#onwindownew9).
E
ester.zhou 已提交
2895 2896 2897

### setWebController<sup>9+</sup>

2898
setWebController(controller: WebviewController): void
E
ester.zhou 已提交
2899

2900
Sets a **WebviewController** object.
E
ester.zhou 已提交
2901 2902 2903

**Parameters**

2904 2905 2906
| Name       | Type         | Mandatory  | Default Value | Description                     |
| ---------- | ------------- | ---- | ---- | ------------------------- |
| controller | [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | Yes   | -    | **WebviewController** object of the **\<Web>** component.|
E
ester.zhou 已提交
2907

2908 2909
## WebResourceError

2910
Implements the **WebResourceError** object. For the sample code, see [onErrorReceive](#onerrorreceive).
2911 2912 2913 2914 2915 2916 2917 2918

### getErrorCode

getErrorCode(): number

Obtains the error code for resource loading.

**Return value**
E
ester.zhou 已提交
2919

2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
| Type    | Description         |
| ------ | ----------- |
| number | Error code for resource loading.|

### getErrorInfo

getErrorInfo(): string

Obtains error information about resource loading.

**Return value**
E
ester.zhou 已提交
2931

2932 2933 2934 2935 2936 2937
| Type    | Description          |
| ------ | ------------ |
| string | Error information about resource loading.|

## WebResourceRequest

2938
Implements the **WebResourceRequest** object. For the sample code, see [onErrorReceive](#onerrorreceive).
2939 2940 2941 2942 2943 2944 2945 2946

### getRequestHeader

getResponseHeader() : Array\<Header\>

Obtains the information about the resource request header.

**Return value**
E
ester.zhou 已提交
2947

2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
| Type                        | Description        |
| -------------------------- | ---------- |
| Array\<[Header](#header)\> | Information about the resource request header.|

### getRequestUrl

getRequestUrl(): string

Obtains the URL of the resource request.

**Return value**
E
ester.zhou 已提交
2959

2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
| Type    | Description           |
| ------ | ------------- |
| string | URL of the resource request.|

### isMainFrame

isMainFrame(): boolean

Checks whether the resource request is in the main frame.

**Return value**
E
ester.zhou 已提交
2971

2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
| Type     | Description              |
| ------- | ---------------- |
| boolean | Whether the resource request is in the main frame.|

### isRedirect

isRedirect(): boolean

Checks whether the resource request is redirected by the server.

**Return value**
E
ester.zhou 已提交
2983

2984 2985
| Type     | Description              |
| ------- | ---------------- |
E
ester.zhou 已提交
2986
| boolean | Whether the resource request is redirected by the server.|
2987 2988 2989 2990 2991 2992 2993 2994

### isRequestGesture

isRequestGesture(): boolean

Checks whether the resource request is associated with a gesture (for example, a tap).

**Return value**
E
ester.zhou 已提交
2995

2996 2997
| Type     | Description                  |
| ------- | -------------------- |
E
ester.zhou 已提交
2998
| boolean | Whether the resource request is associated with a gesture (for example, a tap).|
2999 3000

## Header
E
ester.zhou 已提交
3001

E
ester.zhou 已提交
3002
Describes the request/response header returned by the **\<Web>** component.
E
ester.zhou 已提交
3003

3004 3005 3006 3007
| Name         | Type    | Description           |
| ----------- | ------ | ------------- |
| headerKey   | string | Key of the request/response header.  |
| headerValue | string | Value of the request/response header.|
E
ester.zhou 已提交
3008 3009


3010
## WebResourceResponse
E
ester.zhou 已提交
3011

3012
Implements the **WebResourceResponse** object. For the sample code, see [onHttpErrorReceive](#onhttperrorreceive).
E
ester.zhou 已提交
3013

3014
### getReasonMessage
E
ester.zhou 已提交
3015

3016
getReasonMessage(): string
E
ester.zhou 已提交
3017

3018
Obtains the status code description of the resource response.
E
ester.zhou 已提交
3019

3020
**Return value**
E
ester.zhou 已提交
3021

3022 3023 3024
| Type    | Description           |
| ------ | ------------- |
| string | Status code description of the resource response.|
E
ester.zhou 已提交
3025

3026
### getResponseCode
E
ester.zhou 已提交
3027

3028
getResponseCode(): number
E
ester.zhou 已提交
3029

3030
Obtains the status code of the resource response.
E
ester.zhou 已提交
3031

3032
**Return value**
E
ester.zhou 已提交
3033

3034 3035 3036
| Type    | Description         |
| ------ | ----------- |
| number | Status code of the resource response.|
E
ester.zhou 已提交
3037

3038
### getResponseData
E
ester.zhou 已提交
3039

3040
getResponseData(): string
E
ester.zhou 已提交
3041

3042
Obtains the data in the resource response.
E
ester.zhou 已提交
3043

3044
**Return value**
E
ester.zhou 已提交
3045

3046 3047 3048
| Type    | Description       |
| ------ | --------- |
| string | Data in the resource response.|
E
ester.zhou 已提交
3049

3050
### getResponseEncoding
E
ester.zhou 已提交
3051

3052 3053 3054 3055 3056
getResponseEncoding(): string

Obtains the encoding string of the resource response.

**Return value**
E
ester.zhou 已提交
3057

3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
| Type    | Description        |
| ------ | ---------- |
| string | Encoding string of the resource response.|

### getResponseHeader

getResponseHeader() : Array\<Header\>

Obtains the resource response header.

**Return value**
E
ester.zhou 已提交
3069

3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
| Type                        | Description      |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | Resource response header.|

### getResponseMimeType

getResponseMimeType(): string

Obtains the MIME type of the resource response.

**Return value**
E
ester.zhou 已提交
3081

3082 3083 3084 3085 3086 3087
| Type    | Description                |
| ------ | ------------------ |
| string | MIME type of the resource response.|

### setResponseData<sup>9+</sup>

3088
setResponseData(data: string | number)
3089 3090 3091 3092

Sets the data in the resource response.

**Parameters**
E
ester.zhou 已提交
3093

3094 3095 3096
| Name| Type        | Mandatory| Default Value| Description                                                    |
| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ |
| data   | string \| number | Yes  | -      | Resource response data to set. When set to a number, the value indicates a file handle.|
3097 3098 3099 3100 3101 3102 3103 3104

### setResponseEncoding<sup>9+</sup>

setResponseEncoding(encoding: string)

Sets the encoding string of the resource response.

**Parameters**
E
ester.zhou 已提交
3105

3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116
| Name     | Type  | Mandatory  | Default Value | Description        |
| -------- | ------ | ---- | ---- | ------------ |
| encoding | string | Yes   | -    | Encoding string to set.|

### setResponseMimeType<sup>9+</sup>

setResponseMimeType(mimeType: string)

Sets the MIME type of the resource response.

**Parameters**
E
ester.zhou 已提交
3117

3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128
| Name     | Type  | Mandatory  | Default Value | Description                |
| -------- | ------ | ---- | ---- | -------------------- |
| mimeType | string | Yes   | -    | MIME type to set.|

### setReasonMessage<sup>9+</sup>

setReasonMessage(reason: string)

Sets the status code description of the resource response.

**Parameters**
E
ester.zhou 已提交
3129

3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
| Name   | Type  | Mandatory  | Default Value | Description           |
| ------ | ------ | ---- | ---- | --------------- |
| reason | string | Yes   | -    | Status code description to set.|

### setResponseHeader<sup>9+</sup>

setResponseHeader(header: Array\<Header\>)

Sets the resource response header.

**Parameters**
E
ester.zhou 已提交
3141

3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
| Name   | Type                      | Mandatory  | Default Value | Description      |
| ------ | -------------------------- | ---- | ---- | ---------- |
| header | Array\<[Header](#header)\> | Yes   | -    | Resource response header to set.|

### setResponseCode<sup>9+</sup>

setResponseCode(code: number)

Sets the status code of the resource response.

**Parameters**
E
ester.zhou 已提交
3153

3154 3155 3156 3157
| Name | Type  | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ------------- |
| code | number | Yes   | -    | Status code to set.|

3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
### setResponseIsReady<sup>9+</sup>

setResponseIsReady(IsReady: boolean)

Sets whether the resource response data is ready.

**Parameters**

| Name | Type| Mandatory| Default Value| Description                  |
| ------- | -------- | ---- | ------ | -------------------------- |
| IsReady | boolean  | Yes  | true   | Whether the resource response data is ready.|

3170 3171
## FileSelectorResult<sup>9+</sup>

3172
Notifies the **\<Web>** component of the file selection result. For the sample code, see [onShowFileSelector](#onshowfileselector9).
3173 3174 3175 3176 3177 3178 3179 3180

### handleFileList<sup>9+</sup>

handleFileList(fileList: Array\<string\>): void

Instructs the **\<Web>** component to select a file.

**Parameters**
E
ester.zhou 已提交
3181

3182 3183 3184 3185 3186 3187
| Name     | Type           | Mandatory  | Default Value | Description        |
| -------- | --------------- | ---- | ---- | ------------ |
| fileList | Array\<string\> | Yes   | -    | List of files to operate.|

## FileSelectorParam<sup>9+</sup>

3188
Implements the **FileSelectorParam** object. For the sample code, see [onShowFileSelector](#onshowfileselector9).
3189 3190 3191 3192 3193 3194 3195 3196

### getTitle<sup>9+</sup>

getTitle(): string

Obtains the title of the file selector.

**Return value**
E
ester.zhou 已提交
3197

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
| Type    | Description        |
| ------ | ---------- |
| string | Title of the file selector.|

### getMode<sup>9+</sup>

getMode(): FileSelectorMode

Obtains the mode of the file selector.

**Return value**
E
ester.zhou 已提交
3209

3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode)| Mode of the file selector.|

### getAcceptType<sup>9+</sup>

getAcceptType(): Array\<string\>

Obtains the file filtering type.

3220
**Return value**
E
ester.zhou 已提交
3221

3222 3223 3224
| Type             | Description       |
| --------------- | --------- |
| Array\<string\> | File filtering type.|
E
ester.zhou 已提交
3225

3226
### isCapture<sup>9+</sup>
3227

3228
isCapture(): boolean
3229

3230
Checks whether multimedia capabilities are invoked.
3231

3232
**Return value**
E
ester.zhou 已提交
3233

3234 3235 3236
| Type     | Description          |
| ------- | ------------ |
| boolean | Whether multimedia capabilities are invoked.|
3237

3238
## HttpAuthHandler<sup>9+</sup>
E
ester.zhou 已提交
3239

3240
Implements the **HttpAuthHandler** object. For the sample code, see [onHttpAuthRequest](#onhttpauthrequest9).
E
ester.zhou 已提交
3241

3242
### cancel<sup>9+</sup>
E
ester.zhou 已提交
3243

3244
cancel(): void
E
ester.zhou 已提交
3245

3246
Cancels HTTP authentication as requested by the user.
3247

3248
### confirm<sup>9+</sup>
E
ester.zhou 已提交
3249

3250
confirm(userName: string, pwd: string): boolean
3251

3252
Performs HTTP authentication with the user name and password provided by the user.
E
ester.zhou 已提交
3253

3254
**Parameters**
E
ester.zhou 已提交
3255

3256 3257 3258 3259
| Name     | Type  | Mandatory  | Default Value | Description      |
| -------- | ------ | ---- | ---- | ---------- |
| userName | string | Yes   | -    | HTTP authentication user name.|
| pwd      | string | Yes   | -    | HTTP authentication password. |
E
ester.zhou 已提交
3260

3261
**Return value**
E
ester.zhou 已提交
3262

3263 3264
| Type     | Description                   |
| ------- | --------------------- |
3265
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
3266

3267
### isHttpAuthInfoSaved<sup>9+</sup>
E
ester.zhou 已提交
3268

3269
isHttpAuthInfoSaved(): boolean
3270

3271
Uses the account name and password cached on the server for authentication.
E
ester.zhou 已提交
3272

3273
**Return value**
E
ester.zhou 已提交
3274

3275 3276 3277
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
E
ester.zhou 已提交
3278

3279
## SslErrorHandler<sup>9+</sup>
3280

3281
Implements an **SslErrorHandler** object. For the sample code, see [onSslErrorEventReceive Event](#onsslerroreventreceive9).
3282

3283
### handleCancel<sup>9+</sup>
E
ester.zhou 已提交
3284

3285
handleCancel(): void
3286

3287
Cancels this request.
E
ester.zhou 已提交
3288

3289
### handleConfirm<sup>9+</sup>
E
ester.zhou 已提交
3290

3291
handleConfirm(): void
E
ester.zhou 已提交
3292

3293
Continues using the SSL certificate.
E
ester.zhou 已提交
3294

3295
## ClientAuthenticationHandler<sup>9+</sup>
E
ester.zhou 已提交
3296

3297
Implements a **ClientAuthenticationHandler** object returned by the **\<Web>** component. For the sample code, see [onClientAuthenticationRequest](#onclientauthenticationrequest9).
E
ester.zhou 已提交
3298

3299
### confirm<sup>9+</sup>
E
ester.zhou 已提交
3300

3301
confirm(priKeyFile : string, certChainFile : string): void
E
ester.zhou 已提交
3302

3303
Uses the specified private key and client certificate chain.
E
ester.zhou 已提交
3304

3305
**Parameters**
E
ester.zhou 已提交
3306

3307 3308 3309 3310
| Name          | Type  | Mandatory  | Description              |
| ------------- | ------ | ---- | ------------------ |
| priKeyFile    | string | Yes   | File that stores the private key, which is a directory including the file name. |
| certChainFile | string | Yes   | File that stores the certificate chain, which is a directory including the file name.|
E
ester.zhou 已提交
3311

3312
### cancel<sup>9+</sup>
3313

3314
cancel(): void
3315

3316
Cancels the client certificate request sent by the same host and port server. No additional event will be reported for requests from the same host and port server.
3317

3318
### ignore<sup>9+</sup>
3319

3320
ignore(): void
E
ester.zhou 已提交
3321

3322
Ignores this request.
3323

3324
## PermissionRequest<sup>9+</sup>
E
ester.zhou 已提交
3325

3326
Implements the **PermissionRequest** object. For the sample code, see [onPermissionRequest](#onpermissionrequest9).
3327

3328
### deny<sup>9+</sup>
3329

3330
deny(): void
3331

3332
Denies the permission requested by the web page.
3333

3334
### getOrigin<sup>9+</sup>
E
ester.zhou 已提交
3335

3336
getOrigin(): string
3337

3338
Obtains the origin of this web page.
E
ester.zhou 已提交
3339

3340
**Return value**
3341

3342 3343 3344
| Type    | Description          |
| ------ | ------------ |
| string | Origin of the web page that requests the permission.|
E
ester.zhou 已提交
3345

3346
### getAccessibleResource<sup>9+</sup>
E
ester.zhou 已提交
3347

3348 3349 3350
getAccessibleResource(): Array\<string\>

Obtains the list of accessible resources requested for the web page. For details about the resource types, see [ProtectedResourceType](#protectedresourcetype9).
E
ester.zhou 已提交
3351

3352
**Return value**
E
ester.zhou 已提交
3353

3354 3355 3356
| Type             | Description           |
| --------------- | ------------- |
| Array\<string\> | List of accessible resources requested by the web page.|
3357

3358
### grant<sup>9+</sup>
E
ester.zhou 已提交
3359

3360
grant(resources: Array\<string\>): void
3361

3362
Grants the permission for resources requested by the web page.
3363

3364
**Parameters**
3365

3366 3367 3368
| Name      | Type           | Mandatory  | Default Value | Description         |
| --------- | --------------- | ---- | ---- | ------------- |
| resources | Array\<string\> | Yes   | -    | List of resources that can be requested by the web page with the permission to grant.|
E
ester.zhou 已提交
3369

3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403
## ContextMenuSourceType<sup>9+</sup>
| Name                  | Description        |
| -------------------- | ---------- |
| None        | Other event sources. |
| Mouse       | Mouse event. |
| LongPress   | Long press event. |

## ContextMenuMediaType<sup>9+</sup>

| Name          | Description         |
| ------------ | ----------- |
| None      | Non-special media or other media types.|
| Image     | Image.    |

## ContextMenuInputFieldType<sup>9+</sup>

| Name          | Description         |
| ------------ | ----------- |
| None      | Non-input field.      |
| PlainText | Plain text field, such as the text, search, or email field.  |
| Password  | Password field.    |
| Number    | Numeric field.    |
| Telephone | Phone number field.|
| Other     | Field of any other type.    |

## ContextMenuEditStateFlags<sup>9+</sup>

| Name        | Description        |
| ------------ | ----------- |
| NONE         | Editing is not allowed.  |
| CAN_CUT      | The cut operation is allowed.  |
| CAN_COPY     | The copy operation is allowed.  |
| CAN_PASTE    | The paste operation is allowed.  |
| CAN_SELECT_ALL  | The select all operation is allowed.|
3404

3405
## WebContextMenuParam<sup>9+</sup>
E
ester.zhou 已提交
3406

3407
Implements a context menu, which is displayed after the user clicks the right mouse button or long presses a specific element, such as an image or a link. For the sample code, see [onContextMenuShow](#oncontextmenushow9).
3408

3409
### x<sup>9+</sup>
3410

3411
x(): number
3412

3413
Obtains the X coordinate of the context menu.
E
ester.zhou 已提交
3414

3415
**Return value**
3416

3417 3418 3419
| Type    | Description                |
| ------ | ------------------ |
| number | If the display is normal, a non-negative integer is returned. Otherwise, **-1** is returned.|
E
ester.zhou 已提交
3420

3421
### y<sup>9+</sup>
3422

3423
y(): number
3424

3425
Obtains the Y coordinate of the context menu.
3426 3427

**Return value**
E
ester.zhou 已提交
3428

3429 3430 3431
| Type    | Description                |
| ------ | ------------------ |
| number | If the display is normal, a non-negative integer is returned. Otherwise, **-1** is returned.|
3432

3433
### getLinkUrl<sup>9+</sup>
E
ester.zhou 已提交
3434

3435
getLinkUrl(): string
3436

3437
Obtains the URL of the destination link.
3438

3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
**Return value**

| Type    | Description                       |
| ------ | ------------------------- |
| string | If it is a link that is being long pressed, the URL that has passed the security check is returned.|

### getUnfilteredLinkUrl<sup>9+</sup>

getUnfilteredLinkUrl(): string

Obtains the URL of the destination link.
3450 3451

**Return value**
E
ester.zhou 已提交
3452

3453 3454 3455
| Type    | Description                   |
| ------ | --------------------- |
| string | If it is a link that is being long pressed, the original URL is returned.|
3456

3457
### getSourceUrl<sup>9+</sup>
E
ester.zhou 已提交
3458

3459
getSourceUrl(): string
3460

3461
Obtain the source URL.
3462

3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
**Return value**

| Type    | Description                      |
| ------ | ------------------------ |
| string | If the selected element has the **src** attribute, the URL in the **src** is returned.|

### existsImageContents<sup>9+</sup>

existsImageContents(): boolean

Checks whether image content exists.
3474 3475

**Return value**
E
ester.zhou 已提交
3476

3477 3478 3479
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | The value **true** means that there is image content in the element being long pressed, and **false** means the opposite.|
3480

3481
### getMediaType<sup>9+</sup>
E
ester.zhou 已提交
3482

3483
getMediaType(): ContextMenuMediaType
E
ester.zhou 已提交
3484

3485
Obtains the media type of this web page element.
E
ester.zhou 已提交
3486

3487
**Return value**
E
ester.zhou 已提交
3488

3489 3490 3491
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuMediaType](#contextmenumediatype9) | Media type of the web page element.|
E
ester.zhou 已提交
3492

3493
### getSelectionText<sup>9+</sup>
E
ester.zhou 已提交
3494

3495
getSelectionText(): string
E
ester.zhou 已提交
3496

3497
Obtains the selected text.
E
ester.zhou 已提交
3498

3499
**Return value**
3500

3501 3502 3503
| Type     | Description                       |
| ------- | ------------------------- |
| string | Selected text for the context menu. If no text is selected, null is returned.|
E
ester.zhou 已提交
3504

3505 3506 3507 3508 3509 3510 3511
### getSourceType<sup>9+</sup>

getSourceType(): ContextMenuSourceType

Obtains the event source of the context menu.

**Return value**
E
ester.zhou 已提交
3512

3513 3514 3515
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuSourceType](#contextmenusourcetype9) | Event source of the context menu.|
E
ester.zhou 已提交
3516

3517
### getInputFieldType<sup>9+</sup>
E
ester.zhou 已提交
3518

3519
getInputFieldType(): ContextMenuInputFieldType
E
ester.zhou 已提交
3520

3521
Obtains the input field type of this web page element.
E
ester.zhou 已提交
3522

3523
**Return value**
3524

3525 3526 3527
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuInputFieldType](#contextmenuinputfieldtype9) | Input field type.|
E
ester.zhou 已提交
3528

3529
### isEditable<sup>9+</sup>
3530

3531
isEditable(): boolean
E
ester.zhou 已提交
3532

3533
Checks whether this web page element is editable.
E
ester.zhou 已提交
3534

3535
**Return value**
E
ester.zhou 已提交
3536

3537 3538 3539
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the web page element is editable; returns **false** otherwise.|
E
ester.zhou 已提交
3540

3541
### getEditStateFlags<sup>9+</sup>
E
ester.zhou 已提交
3542

3543
getEditStateFlags(): number
E
ester.zhou 已提交
3544

3545
Obtains the edit state flag of this web page element.
3546

3547
**Return value**
E
ester.zhou 已提交
3548

3549 3550 3551
| Type     | Description                       |
| ------- | ------------------------- |
| number | Edit state flag of the web page element. For details, see [ContextMenuEditStateFlags](#contextmenueditstateflags9).|
E
ester.zhou 已提交
3552

3553
## WebContextMenuResult<sup>9+</sup>
E
ester.zhou 已提交
3554

3555
Implements a **WebContextMenuResult** object. For the sample code, see [onContextMenuShow](#oncontextmenushow9).
E
ester.zhou 已提交
3556

3557
### closeContextMenu<sup>9+</sup>
3558

3559
closeContextMenu(): void
3560

3561
Closes this context menu. This API must be called when no operations in **WebContextMenuResult** are performed.
3562

3563
### copyImage<sup>9+</sup>
E
ester.zhou 已提交
3564

3565
copyImage(): void
3566

3567
Copies the image specified in **WebContextMenuParam**.
E
ester.zhou 已提交
3568

3569
### copy<sup>9+</sup>
3570

3571
copy(): void
3572

3573
Performs the copy operation related to this context menu.
3574

3575
### paste<sup>9+</sup>
E
ester.zhou 已提交
3576

3577
paste(): void
3578

3579
Performs the paste operation related to this context menu.
E
ester.zhou 已提交
3580

3581
### cut<sup>9+</sup>
3582

3583
cut(): void
3584

3585
Performs the cut operation related to this context menu.
3586

3587
### selectAll<sup>9+</sup>
E
ester.zhou 已提交
3588

3589
selectAll(): void
3590

3591
Performs the select all operation related to this context menu.
E
ester.zhou 已提交
3592

3593
## JsGeolocation
3594

3595
Implements the **PermissionRequest** object. For the sample code, see [onGeolocationShow Event](#ongeolocationshow).
E
ester.zhou 已提交
3596

3597
### invoke
E
ester.zhou 已提交
3598

3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
invoke(origin: string, allow: boolean, retain: boolean): void

Sets the geolocation permission status of a web page.

**Parameters**

| Name   | Type   | Mandatory  | Default Value | Description                                    |
| ------ | ------- | ---- | ---- | ---------------------------------------- |
| origin | string  | Yes   | -    | Index of the origin.                              |
| allow  | boolean | Yes   | -    | Geolocation permission status.                            |
| retain | boolean | Yes   | -    | Whether the geolocation permission status can be saved to the system. You can manage the geolocation permissions saved to the system through [GeolocationPermissions<sup>9+</sup>](../apis/js-apis-webview.md#geolocationpermissions).|

## WebController

Implements a **WebController** to control the behavior of the **\<Web>** component. A **WebController** can control only one **\<Web>** component, and the APIs in the **WebController** can be invoked only after it has been bound to the target **\<Web>** component.

This API is deprecated since API version 9. You are advised to use [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller).

### Creating an Object

```
webController: WebController = new WebController()
```

### requestFocus<sup>(deprecated)</sup>

requestFocus()

Requests focus for this web page.

This API is deprecated since API version 9. You are advised to use [requestFocus<sup>9+</sup>](../apis/js-apis-webview.md#requestfocus).
E
ester.zhou 已提交
3630

3631
**Example**
E
ester.zhou 已提交
3632

3633 3634 3635 3636 3637
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3638
    controller: WebController = new WebController()
3639 3640 3641
  
    build() {
      Column() {
3642
        Button('requestFocus')
3643
          .onClick(() => {
3644
            this.controller.requestFocus()
3645 3646 3647 3648 3649 3650
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
3651

3652
### accessBackward<sup>(deprecated)</sup>
E
ester.zhou 已提交
3653

3654
accessBackward(): boolean
3655

3656
Checks whether going to the previous page can be performed on the current page.
3657

3658
This API is deprecated since API version 9. You are advised to use [accessBackward<sup>9+</sup>](../apis/js-apis-webview.md#accessbackward).
E
ester.zhou 已提交
3659

3660 3661 3662 3663 3664
**Return value**

| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if going to the previous page can be performed on the current page; returns **false** otherwise.|
3665 3666

**Example**
E
ester.zhou 已提交
3667

3668 3669 3670 3671
  ```ts
  // xxx.ets
  @Entry
  @Component
3672
  struct WebComponent {
3673
    controller: WebController = new WebController()
3674
  
3675 3676
    build() {
      Column() {
3677 3678 3679 3680
        Button('accessBackward')
          .onClick(() => {
            let result = this.controller.accessBackward()
            console.log('result:' + result)
3681
          })
3682
        Web({ src: 'www.example.com', controller: this.controller })
3683 3684 3685 3686 3687
      }
    }
  }
  ```

3688
### accessForward<sup>(deprecated)</sup>
E
ester.zhou 已提交
3689

3690
accessForward(): boolean
E
ester.zhou 已提交
3691

3692
Checks whether going to the next page can be performed on the current page.
E
ester.zhou 已提交
3693

3694
This API is deprecated since API version 9. You are advised to use [accessForward<sup>9+</sup>](../apis/js-apis-webview.md#accessforward).
E
ester.zhou 已提交
3695

3696
**Return value**
E
ester.zhou 已提交
3697

3698 3699 3700
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if going to the next page can be performed on the current page; returns **false** otherwise.|
3701 3702

**Example**
E
ester.zhou 已提交
3703

3704 3705 3706 3707 3708
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3709
    controller: WebController = new WebController()
3710
  
3711 3712
    build() {
      Column() {
3713 3714 3715 3716 3717 3718
        Button('accessForward')
          .onClick(() => {
            let result = this.controller.accessForward()
            console.log('result:' + result)
          })
        Web({ src: 'www.example.com', controller: this.controller })
3719 3720 3721 3722 3723
      }
    }
  }
  ```

3724
### accessStep<sup>(deprecated)</sup>
E
ester.zhou 已提交
3725

3726
accessStep(step: number): boolean
E
ester.zhou 已提交
3727

3728
Performs a specific number of steps forward or backward from the current page.
E
ester.zhou 已提交
3729

3730
This API is deprecated since API version 9. You are advised to use [accessStep<sup>9+</sup>](../apis/js-apis-webview.md#accessstep).
E
ester.zhou 已提交
3731

3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
**Parameters**

| Name | Type  | Mandatory  | Default Value | Description                 |
| ---- | ------ | ---- | ---- | --------------------- |
| step | number | Yes   | -    | Number of the steps to take. A positive number means to go forward, and a negative number means to go backward.|

**Return value**

| Type     | Description       |
| ------- | --------- |
| boolean | Whether going forward or backward from the current page is successful.|
E
ester.zhou 已提交
3743

3744
**Example**
E
ester.zhou 已提交
3745

3746 3747 3748 3749 3750
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3751
    controller: WebController = new WebController()
3752
    @State steps: number = 2
3753 3754 3755
  
    build() {
      Column() {
3756
        Button('accessStep')
3757
          .onClick(() => {
3758 3759
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
3760 3761 3762 3763 3764 3765 3766
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3767
### backward<sup>(deprecated)</sup>
E
ester.zhou 已提交
3768

3769
backward(): void
E
ester.zhou 已提交
3770

3771 3772 3773
Goes to the previous page based on the history stack. This API is generally used together with **accessBackward**.

This API is deprecated since API version 9. You are advised to use [backward<sup>9+</sup>](../apis/js-apis-webview.md#backward).
E
ester.zhou 已提交
3774

3775
**Example**
E
ester.zhou 已提交
3776

3777 3778 3779 3780 3781
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3782
    controller: WebController = new WebController()
3783 3784 3785
  
    build() {
      Column() {
3786
        Button('backward')
3787
          .onClick(() => {
3788
            this.controller.backward()
3789 3790 3791 3792 3793 3794 3795
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3796
### forward<sup>(deprecated)</sup>
E
ester.zhou 已提交
3797

3798
forward(): void
E
ester.zhou 已提交
3799

3800 3801 3802
Goes to the next page based on the history stack. This API is generally used together with **accessForward**.

This API is deprecated since API version 9. You are advised to use [forward<sup>9+</sup>](../apis/js-apis-webview.md#forward).
E
ester.zhou 已提交
3803 3804 3805 3806 3807 3808 3809 3810

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3811
    controller: WebController = new WebController()
3812
  
E
ester.zhou 已提交
3813 3814
    build() {
      Column() {
3815
        Button('forward')
E
ester.zhou 已提交
3816
          .onClick(() => {
3817
            this.controller.forward()
E
ester.zhou 已提交
3818 3819 3820 3821 3822 3823 3824
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3825
### backOrForward<sup>9+</sup>
E
ester.zhou 已提交
3826

3827 3828 3829 3830 3831
backOrForward(step: number): void

Performs a specific number of steps forward or backward on the current page based on the history stack. No redirection will be performed if the corresponding page does not exist in the history stack.

**Parameters**
E
ester.zhou 已提交
3832

3833 3834 3835
| Name | Type  | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | ----------- |
| step | number | Yes   | -    | Number of the steps to take.|
E
ester.zhou 已提交
3836 3837 3838 3839 3840 3841 3842 3843

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3844
    controller: WebController = new WebController()
3845 3846
    @State step: number = -2
  
E
ester.zhou 已提交
3847 3848
    build() {
      Column() {
3849
        Button('backOrForward')
E
ester.zhou 已提交
3850
          .onClick(() => {
3851
            this.controller.backOrForward(this.step)
E
ester.zhou 已提交
3852 3853 3854 3855 3856 3857 3858
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3859
### deleteJavaScriptRegister<sup>(deprecated)</sup>
E
ester.zhou 已提交
3860

3861
deleteJavaScriptRegister(name: string)
E
ester.zhou 已提交
3862

3863
Deletes a specific application JavaScript object that is registered with the window through **registerJavaScriptProxy**. The deletion takes effect immediately, with no need for invoking the [refresh](#refresh) API.
3864

3865
This API is deprecated since API version 9. You are advised to use [deleteJavaScriptRegister<sup>9+</sup>](../apis/js-apis-webview.md#deletejavascriptregister).
E
ester.zhou 已提交
3866

3867 3868 3869 3870 3871
**Parameters**

| Name | Type  | Mandatory  | Default Value | Description                                    |
| ---- | ------ | ---- | ---- | ---------------------------------------- |
| name | string | Yes   | -    | Name of the registered JavaScript object, which can be used to invoke the corresponding object on the application side from the web side.|
3872 3873

**Example**
E
ester.zhou 已提交
3874

3875 3876 3877 3878 3879
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3880
    controller: WebController = new WebController()
3881
    @State name: string = 'Object'
3882 3883 3884
  
    build() {
      Column() {
3885
        Button('deleteJavaScriptRegister')
3886
          .onClick(() => {
3887
            this.controller.deleteJavaScriptRegister(this.name)
3888 3889 3890 3891 3892 3893 3894
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3895
### getHitTest<sup>(deprecated)</sup>
3896

3897
getHitTest(): HitTestType
3898

3899
Obtains the element type of the area being clicked.	
3900

3901
This API is deprecated since API version 9. You are advised to use [getHitTest<sup>9+</sup>](../apis/js-apis-webview.md#gethittest).
3902

3903
**Return value**
3904

3905 3906 3907
| Type                             | Description         |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype)| Element type of the area being clicked.|
3908

E
ester.zhou 已提交
3909
**Example**
3910

E
ester.zhou 已提交
3911 3912 3913 3914 3915
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3916
    controller: WebController = new WebController()
3917
  
E
ester.zhou 已提交
3918 3919
    build() {
      Column() {
3920
        Button('getHitTest')
E
ester.zhou 已提交
3921
          .onClick(() => {
3922 3923
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
E
ester.zhou 已提交
3924 3925 3926 3927 3928 3929
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
3930

3931 3932
### getHitTestValue<sup>9+</sup>
getHitTestValue(): HitTestValue
E
ester.zhou 已提交
3933

3934
Obtains the element information of the area being clicked.
3935

3936
**Return value**
3937

3938 3939 3940
| Type                            | Description        |
| ------------------------------ | ---------- |
| [HitTestValue](#hittestvalue9) | Element information of the area being clicked.|
3941 3942

**Example**
E
ester.zhou 已提交
3943

3944
  ```ts
3945
  // xxx.ets
3946 3947 3948
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3949
    controller: WebController = new WebController()
3950
  
3951 3952
    build() {
      Column() {
3953
        Button('getHitTestValue')
E
ester.zhou 已提交
3954
          .onClick(() => {
3955 3956 3957
            let hitValue = this.controller.getHitTestValue()
            console.log("hitType: " + hitValue.getType())
            console.log("extra: " + hitValue.getExtra())
E
ester.zhou 已提交
3958
          })
3959
        Web({ src: 'www.example.com', controller: this.controller })
3960 3961 3962
      }
    }
  }
3963
  ```
E
ester.zhou 已提交
3964

3965 3966
### getWebId<sup>9+</sup>
getWebId(): number
E
ester.zhou 已提交
3967

3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
Obtains the index value of this **\<Web>** component, which can be used for **\<Web>** component management.

**Return value**

| Type    | Description          |
| ------ | ------------ |
| number | Index value of the current **\<Web>** component.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Button('getWebId')
          .onClick(() => {
            let id = this.controller.getWebId()
            console.log("id: " + id)
          })
        Web({ src: 'www.example.com', controller: this.controller })
E
ester.zhou 已提交
3993 3994
      }
    }
E
ester.zhou 已提交
3995
  }
3996
  ```
E
ester.zhou 已提交
3997

3998 3999
### getTitle<sup>9+</sup>
getTitle(): string
E
ester.zhou 已提交
4000

4001
Obtains the title of the current web page.
E
ester.zhou 已提交
4002 4003 4004

**Return value**

4005 4006 4007
| Type    | Description      |
| ------ | -------- |
| string | Title of the current web page.|
E
ester.zhou 已提交
4008 4009 4010 4011 4012 4013 4014 4015

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4016
    controller: WebController = new WebController()
4017
  
E
ester.zhou 已提交
4018 4019
    build() {
      Column() {
4020
        Button('getTitle')
E
ester.zhou 已提交
4021
          .onClick(() => {
4022 4023
            let title = this.controller.getTitle()
            console.log("title: " + title)
E
ester.zhou 已提交
4024 4025 4026 4027 4028 4029 4030
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4031 4032
### getPageHeight<sup>9+</sup>
getPageHeight(): number
E
ester.zhou 已提交
4033

4034
Obtains the height of the current web page.
E
ester.zhou 已提交
4035 4036 4037

**Return value**

4038 4039 4040
| Type    | Description        |
| ------ | ---------- |
| number | Height of the current web page.|
E
ester.zhou 已提交
4041

4042
**Example**
E
ester.zhou 已提交
4043

4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Button('getPageHeight')
          .onClick(() => {
            let pageHeight = this.controller.getPageHeight()
            console.log("pageHeight: " + pageHeight)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getDefaultUserAgent<sup>9+</sup>
getDefaultUserAgent(): string

Obtains the default user agent of the current web page.
E
ester.zhou 已提交
4068 4069 4070

**Return value**

4071 4072 4073
| Type    | Description     |
| ------ | ------- |
| string | Default user agent.|
E
ester.zhou 已提交
4074

4075
**Example**
E
ester.zhou 已提交
4076

4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Button('getDefaultUserAgent')
          .onClick(() => {
            let userAgent = this.controller.getDefaultUserAgent()
            console.log("userAgent: " + userAgent)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
4096

4097
### loadData<sup>(deprecated)</sup>
E
ester.zhou 已提交
4098

4099
loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string })
E
ester.zhou 已提交
4100

4101
Loads data. If **baseUrl** is empty, the specified character string will be loaded using the data protocol.
E
ester.zhou 已提交
4102

4103
If **baseUrl** is set to a data URL, the encoded string will be loaded by the **\<Web>** component using the data protocol.
E
ester.zhou 已提交
4104

4105
If **baseUrl** is set to an HTTP or HTTPS URL, the encoded string will be processed by the **\<Web>** component as a non-encoded string in a manner similar to **loadUrl**.
E
ester.zhou 已提交
4106

4107
This API is deprecated since API version 9. You are advised to use [loadData<sup>9+</sup>](../apis/js-apis-webview.md#loaddata).
E
ester.zhou 已提交
4108

4109 4110 4111 4112 4113 4114 4115 4116 4117
**Parameters**

| Name       | Type  | Mandatory  | Default Value | Description                                    |
| ---------- | ------ | ---- | ---- | ---------------------------------------- |
| data       | string | Yes   | -    | Character string obtained after being Base64 or URL encoded.             |
| mimeType   | string | Yes   | -    | Media type (MIME).                             |
| encoding   | string | Yes   | -    | Encoding type, which can be Base64 or URL.               |
| baseUrl    | string | No   | -    | URL (HTTP/HTTPS/data compliant), which is assigned by the **\<Web>** component to **window.origin**.|
| historyUrl | string | No   | -    | Historical record URL. If this parameter is not empty, it can be managed in historical records to implement page going backward and forward. This parameter is invalid when **baseUrl** is left empty.|
E
ester.zhou 已提交
4118 4119 4120 4121 4122 4123 4124 4125

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4126
    controller: WebController = new WebController()
E
ester.zhou 已提交
4127 4128 4129
  
    build() {
      Column() {
4130
        Button('loadData')
E
ester.zhou 已提交
4131
          .onClick(() => {
4132 4133 4134 4135 4136
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
            })
E
ester.zhou 已提交
4137 4138 4139 4140 4141 4142 4143
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4144
### loadUrl<sup>(deprecated)</sup>
E
ester.zhou 已提交
4145

4146
loadUrl(options: { url: string | Resource, headers?: Array\<Header\> })
4147

4148
Loads a URL using the specified HTTP header.
E
ester.zhou 已提交
4149

4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161
The object injected through **loadUrl** is valid only in the current document. It will be invalid on a new page navigated to through **loadUrl**.

The object injected through **registerJavaScriptProxy** is still valid on a new page redirected through **loadUrl**.

This API is deprecated since API version 9. You are advised to use [loadUrl<sup>9+</sup>](../apis/js-apis-webview.md#loadurl).

**Parameters**

| Name    | Type                      | Mandatory  | Default Value | Description          |
| ------- | -------------------------- | ---- | ---- | -------------- |
| url     | string                     | Yes   | -    | URL to load.    |
| headers | Array\<[Header](#header)\> | No   | []   | Additional HTTP request header of the URL.|
E
ester.zhou 已提交
4162 4163 4164 4165 4166 4167 4168 4169

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4170
    controller: WebController = new WebController()
E
ester.zhou 已提交
4171 4172 4173
  
    build() {
      Column() {
4174
        Button('loadUrl')
E
ester.zhou 已提交
4175
          .onClick(() => {
4176
            this.controller.loadUrl({ url: 'www.example.com' })
E
ester.zhou 已提交
4177 4178 4179 4180 4181 4182 4183
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4184
### onActive<sup>(deprecated)</sup>
E
ester.zhou 已提交
4185

4186
onActive(): void
E
ester.zhou 已提交
4187

4188
Called when the **\<Web>** component enters the active state.
E
ester.zhou 已提交
4189

4190
This API is deprecated since API version 9. You are advised to use [onActive<sup>9+</sup>](../apis/js-apis-webview.md#onactive).
E
ester.zhou 已提交
4191 4192 4193 4194 4195 4196 4197 4198

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4199
    controller: WebController = new WebController()
E
ester.zhou 已提交
4200 4201 4202
  
    build() {
      Column() {
4203
        Button('onActive')
E
ester.zhou 已提交
4204
          .onClick(() => {
4205
            this.controller.onActive()
E
ester.zhou 已提交
4206 4207 4208 4209 4210 4211 4212
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4213
### onInactive<sup>(deprecated)</sup>
E
ester.zhou 已提交
4214

4215
onInactive(): void
E
ester.zhou 已提交
4216

4217
Called when the **\<Web>** component enters the inactive state.
E
ester.zhou 已提交
4218

4219
This API is deprecated since API version 9. You are advised to use [onInactive<sup>9+</sup>](../apis/js-apis-webview.md#oninactive).
E
ester.zhou 已提交
4220 4221 4222 4223 4224 4225 4226 4227

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4228
    controller: WebController = new WebController()
E
ester.zhou 已提交
4229 4230 4231
  
    build() {
      Column() {
4232
        Button('onInactive')
E
ester.zhou 已提交
4233
          .onClick(() => {
4234
            this.controller.onInactive()
E
ester.zhou 已提交
4235 4236 4237 4238 4239 4240 4241
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4242 4243
### zoom<sup>(deprecated)</sup>
zoom(factor: number): void
E
ester.zhou 已提交
4244

4245
Sets a zoom factor for the current web page.
E
ester.zhou 已提交
4246

4247
This API is deprecated since API version 9. You are advised to use [zoom<sup>9+</sup>](../apis/js-apis-webview.md#zoom).
E
ester.zhou 已提交
4248

4249 4250 4251 4252 4253
**Parameters**

| Name   | Type  | Mandatory  | Description                          |
| ------ | ------ | ---- | ------------------------------ |
| factor | number | Yes   | Zoom factor to set. A positive value indicates zoom-in, and a negative value indicates zoom-out.|
4254 4255

**Example**
E
ester.zhou 已提交
4256 4257 4258 4259 4260 4261

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4262
    controller: WebController = new WebController()
4263
    @State factor: number = 1
E
ester.zhou 已提交
4264 4265 4266
  
    build() {
      Column() {
4267
        Button('zoom')
E
ester.zhou 已提交
4268
          .onClick(() => {
4269
            this.controller.zoom(this.factor)
E
ester.zhou 已提交
4270 4271 4272 4273 4274 4275 4276
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4277 4278
### zoomIn<sup>9+</sup>
zoomIn(): boolean
E
ester.zhou 已提交
4279

4280
Zooms in on this web page by 20%.
E
ester.zhou 已提交
4281 4282 4283

**Return value**

4284 4285 4286
| Type     | Description         |
| ------- | ----------- |
| boolean | Operation result.|
E
ester.zhou 已提交
4287 4288 4289 4290 4291 4292 4293 4294

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4295
    controller: WebController = new WebController()
E
ester.zhou 已提交
4296 4297 4298
  
    build() {
      Column() {
4299
        Button('zoomIn')
E
ester.zhou 已提交
4300
          .onClick(() => {
4301 4302
            let result = this.controller.zoomIn()
            console.log("result: " + result)
E
ester.zhou 已提交
4303 4304 4305 4306 4307 4308 4309
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4310 4311
### zoomOut<sup>9+</sup>
zoomOut(): boolean
E
ester.zhou 已提交
4312

4313
Zooms out of this web page by 20%.
E
ester.zhou 已提交
4314

4315
**Return value**
E
ester.zhou 已提交
4316

4317 4318 4319
| Type     | Description         |
| ------- | ----------- |
| boolean | Operation result.|
E
ester.zhou 已提交
4320 4321 4322 4323 4324 4325 4326 4327

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4328
    controller: WebController = new WebController()
E
ester.zhou 已提交
4329 4330 4331
  
    build() {
      Column() {
4332
        Button('zoomOut')
E
ester.zhou 已提交
4333
          .onClick(() => {
4334 4335
            let result = this.controller.zoomOut()
            console.log("result: " + result)
E
ester.zhou 已提交
4336 4337 4338 4339 4340 4341 4342
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4343
### refresh<sup>(deprecated)</sup>
E
ester.zhou 已提交
4344

4345
refresh()
E
ester.zhou 已提交
4346

4347
Called when the **\<Web>** component refreshes the web page.
E
ester.zhou 已提交
4348

4349
This API is deprecated since API version 9. You are advised to use [refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh).
E
ester.zhou 已提交
4350 4351 4352 4353 4354 4355 4356 4357

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4358
    controller: WebController = new WebController()
E
ester.zhou 已提交
4359 4360 4361
  
    build() {
      Column() {
4362
        Button('refresh')
E
ester.zhou 已提交
4363
          .onClick(() => {
4364
            this.controller.refresh()
E
ester.zhou 已提交
4365 4366 4367 4368 4369 4370 4371
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4372
### registerJavaScriptProxy<sup>(deprecated)</sup>
E
ester.zhou 已提交
4373

4374 4375 4376 4377 4378
registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })

Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. You must invoke the [refresh](#refresh) API for the registration to take effect.

This API is deprecated since API version 9. You are advised to use [registerJavaScriptProxy<sup>9+</sup>](../apis/js-apis-webview.md#registerjavascriptproxy).
E
ester.zhou 已提交
4379 4380 4381

**Parameters**

4382 4383 4384 4385 4386
| Name       | Type           | Mandatory  | Default Value | Description                                    |
| ---------- | --------------- | ---- | ---- | ---------------------------------------- |
| object     | object          | Yes   | -    | Application-side JavaScript object to be registered. Methods can be declared, but attributes cannot. The parameters and return value can only be of the string, number, or Boolean type.|
| name       | string          | Yes   | -    | Name of the object to be registered, which is the same as that invoked in the window. After registration, the window can use this name to access the JavaScript object at the application side.|
| methodList | Array\<string\> | Yes   | -    | Methods of the JavaScript object to be registered at the application side.                |
E
ester.zhou 已提交
4387 4388 4389 4390 4391 4392 4393

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
4394
  struct Index {
E
ester.zhou 已提交
4395
    controller: WebController = new WebController()
4396 4397 4398 4399 4400 4401 4402 4403
    testObj = {
      test: (data) => {
        return "ArkUI Web Component"
      },
      toString: () => {
        console.log('Web Component toString')
      }
    }
E
ester.zhou 已提交
4404 4405
    build() {
      Column() {
4406 4407 4408 4409 4410 4411 4412
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
            })
E
ester.zhou 已提交
4413
          })
4414 4415 4416
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
E
ester.zhou 已提交
4417 4418 4419 4420 4421
      }
    }
  }
  ```

4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438
  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
      }
  </script>
  </html>
  
  ```
E
ester.zhou 已提交
4439

4440
### runJavaScript<sup>(deprecated)</sup>
E
ester.zhou 已提交
4441

4442
runJavaScript(options: { script: string, callback?: (result: string) => void })
E
ester.zhou 已提交
4443

4444 4445 4446 4447 4448 4449 4450 4451 4452 4453
Executes a JavaScript script. This API uses an asynchronous callback to return the script execution result. **runJavaScript** can be invoked only after **loadUrl** is executed. For example, it can be invoked in **onPageEnd**.

This API is deprecated since API version 9. You are advised to use [runJavaScript<sup>9+</sup>](../apis/js-apis-webview.md#runjavascript).

**Parameters**

| Name     | Type                    | Mandatory  | Default Value | Description                                    |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | Yes   | -    | JavaScript script.                           |
| callback | (result: string) => void | No   | -    | Callback used to return the result. Returns **null** if the JavaScript script fails to be executed or no value is returned.|
E
ester.zhou 已提交
4454 4455 4456 4457 4458 4459 4460 4461

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4462
    controller: WebController = new WebController()
4463
    @State webResult: string = ''
E
ester.zhou 已提交
4464 4465
    build() {
      Column() {
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
        Text(this.webResult).fontSize(20)
        Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          this.controller.runJavaScript({
            script: 'test()',
            callback: (result: string)=> {
              this.webResult = result
              console.info(`The test() return value is: ${result}`)
            }})
          console.info('url: ', e.url)
        })
E
ester.zhou 已提交
4478 4479 4480 4481 4482
      }
    }
  }
  ```

4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
        console.log('Ark WebComponent')
        return "This value is from index.html"
    }
    </script>
  </html>
E
ester.zhou 已提交
4498

4499
  ```
E
ester.zhou 已提交
4500

4501
### stop<sup>(deprecated)</sup>
E
ester.zhou 已提交
4502

4503 4504 4505 4506 4507
stop()

Stops page loading.

This API is deprecated since API version 9. You are advised to use [stop<sup>9+</sup>](../apis/js-apis-webview.md#stop).
E
ester.zhou 已提交
4508 4509 4510 4511 4512 4513 4514 4515

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4516
    controller: WebController = new WebController()
E
ester.zhou 已提交
4517 4518 4519
  
    build() {
      Column() {
4520
        Button('stop')
E
ester.zhou 已提交
4521
          .onClick(() => {
4522
            this.controller.stop()
E
ester.zhou 已提交
4523 4524 4525 4526 4527 4528 4529
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4530
### clearHistory<sup>(deprecated)</sup>
E
ester.zhou 已提交
4531

4532
clearHistory(): void
E
ester.zhou 已提交
4533

4534
Clears the browsing history.
E
ester.zhou 已提交
4535

4536
This API is deprecated since API version 9. You are advised to use [clearHistory<sup>9+</sup>](../apis/js-apis-webview.md#clearhistory).
E
ester.zhou 已提交
4537 4538 4539 4540 4541 4542 4543 4544

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4545
    controller: WebController = new WebController()
E
ester.zhou 已提交
4546 4547 4548
  
    build() {
      Column() {
4549
        Button('clearHistory')
E
ester.zhou 已提交
4550
          .onClick(() => {
4551
            this.controller.clearHistory()
E
ester.zhou 已提交
4552 4553 4554 4555 4556 4557 4558
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4559
### clearSslCache
E
ester.zhou 已提交
4560

4561 4562 4563
clearSslCache(): void

Clears the user operation corresponding to the SSL certificate error event recorded by the **\<Web>** component.
E
ester.zhou 已提交
4564 4565 4566 4567 4568 4569 4570 4571

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4572
    controller: WebController = new WebController()
4573

E
ester.zhou 已提交
4574 4575
    build() {
      Column() {
4576
        Button('clearSslCache')
E
ester.zhou 已提交
4577
          .onClick(() => {
4578
            this.controller.clearSslCache()
E
ester.zhou 已提交
4579 4580 4581 4582 4583 4584 4585
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4586
### clearClientAuthenticationCache
E
ester.zhou 已提交
4587

4588 4589 4590
clearClientAuthenticationCache(): void

Clears the user operation corresponding to the client certificate request event recorded by the **\<Web>** component.
E
ester.zhou 已提交
4591 4592 4593 4594 4595 4596 4597 4598

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4599
    controller: WebController = new WebController()
4600

E
ester.zhou 已提交
4601 4602
    build() {
      Column() {
4603
        Button('clearClientAuthenticationCache')
E
ester.zhou 已提交
4604
          .onClick(() => {
4605
            this.controller.clearClientAuthenticationCache()
E
ester.zhou 已提交
4606 4607 4608 4609 4610 4611 4612
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4613
### getCookieManager<sup>9+</sup>
E
ester.zhou 已提交
4614

4615
getCookieManager(): WebCookie
E
ester.zhou 已提交
4616

4617
Obtains the cookie management object of the **\<Web>** component.
E
ester.zhou 已提交
4618 4619 4620

**Return value**

4621 4622 4623
| Type       | Description                                      |
| --------- | ---------------------------------------- |
| WebCookie | Cookie management object. For details, see [WebCookie](#webcookie).|
E
ester.zhou 已提交
4624 4625 4626 4627 4628 4629 4630 4631

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4632
    controller: WebController = new WebController()
E
ester.zhou 已提交
4633 4634 4635
  
    build() {
      Column() {
4636
        Button('getCookieManager')
E
ester.zhou 已提交
4637
          .onClick(() => {
4638
            let cookieManager = this.controller.getCookieManager()
E
ester.zhou 已提交
4639 4640 4641 4642 4643 4644 4645
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4646 4647 4648 4649 4650 4651 4652
### createWebMessagePorts<sup>9+</sup>

createWebMessagePorts(): Array\<WebMessagePort\>

Creates web message ports.

**Return value**
E
ester.zhou 已提交
4653 4654


4655 4656 4657
| Type                                      | Description        |
| ---------------------------------------- | ---------- |
| Array\<[WebMessagePort](#webmessageport9)\> | List of web message ports.|
E
ester.zhou 已提交
4658 4659 4660 4661 4662 4663 4664 4665

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4666
    controller: WebController = new WebController()
4667
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
4668 4669
    build() {
      Column() {
4670
        Button('createWebMessagePorts')
E
ester.zhou 已提交
4671
          .onClick(() => {
4672 4673
            this.ports = this.controller.createWebMessagePorts()
            console.log("createWebMessagePorts size:" + this.ports.length)
E
ester.zhou 已提交
4674 4675 4676 4677 4678 4679 4680
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4681
### postMessage<sup>9+</sup>
E
ester.zhou 已提交
4682

4683
postMessage(options: { message: WebMessageEvent, uri: string}): void
E
ester.zhou 已提交
4684

4685
Sends a web message to an HTML5 window.
E
ester.zhou 已提交
4686 4687 4688

**Parameters**

4689 4690 4691 4692
| Name    | Type                                | Mandatory  | Default Value | Description             |
| ------- | ------------------------------------ | ---- | ---- | ----------------- |
| message | [WebMessageEvent](#webmessageevent9) | Yes   | -    | Message to send, including the data and message port.|
| uri     | string                               | Yes   | -    | URI for receiving the message.       |
E
ester.zhou 已提交
4693 4694 4695 4696

**Example**

  ```ts
4697
  // index.ets
E
ester.zhou 已提交
4698 4699 4700
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4701
    controller: WebController = new WebController()
4702 4703 4704 4705
    ports: WebMessagePort[] = null
    @State sendFromEts: string = 'Send this message from ets to HTML'
    @State receivedFromHtml: string = 'Display received message send from HTML'

E
ester.zhou 已提交
4706 4707
    build() {
      Column() {
4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743
        // Display the received HTML content.
        Text(this.receivedFromHtml)
        // Send the content in the text box to an HTML window.
        TextInput({placeholder: 'Send this message from ets to HTML'})
        .onChange((value: string) => {
          this.sendFromEts = value
        })

        // 1. Create two message ports.
        Button('1.CreateWebMessagePorts')
          .onClick(() => {
            this.ports = this.controller.createWebMessagePorts()
            console.log("createWebMessagePorts size:" + this.ports.length)
          })

        // 2. Send one of the message ports to the HTML side, which can then save and use the port.
        Button('2.PostMessagePort')
          .onClick(() => {
            var sendPortArray = new Array(this.ports[1])
            var msgEvent = new WebMessageEvent()
            msgEvent.setData("__init_port__")
            msgEvent.setPorts(sendPortArray)
            this.controller.postMessage({message: msgEvent, uri: "*"})
          })

        // 3. Register a callback for the other message port on the application side.
        Button('3.RegisterCallback')
          .onClick(() => {
              this.ports[0].onMessageEvent((result: string) => {
                var msg = 'Got msg from HTML: ' + result
                this.receivedFromHtml = msg
              })
          })

        // 4. Use the port on the application side to send messages to the message port that has been sent to the HTML.
        Button('4.SendDataToHtml5')
E
ester.zhou 已提交
4744
          .onClick(() => {
4745 4746 4747
            var msg = new WebMessageEvent()
            msg.setData(this.sendFromEts)
            this.ports[0].postMessageEvent(msg)
E
ester.zhou 已提交
4748
          })
4749 4750 4751
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .javaScriptAccess(true)
          .fileAccess(true)
E
ester.zhou 已提交
4752 4753 4754 4755
      }
    }
  }

4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768
  // index.html
  <!DOCTYPE html>
  <html>
      <body>
          <h1>Web Message Port Demo</h1>
          <div style="font-size: 24pt;">
            <input type="button" value="5.SendToEts" onclick="PostMsgToEts(msgFromJS.value);" /><br/>
            <input id="msgFromJS" type="text" value="send this message from HTML to ets" style="font-size: 16pt;" /><br/>
          </div>
          <p class="output">display received message send from ets</p>
      </body>
      <script src="index.js"></script>
  </html>
E
ester.zhou 已提交
4769

4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781
  // index.js
  var h5Port;
  var output = document.querySelector('.output');
  window.addEventListener('message', function(event) {
    if (event.data == '__init_port__') {
      if(event.ports[0] != null) {
        h5Port = event.ports[0]; // 1. Save the port number sent from the eTS side.
        h5Port.onmessage = function(event) {
          // 2. Receive the message sent from the eTS side.
          var msg = 'Got message from ets:' + event.data;
          output.innerHTML = msg;
        }
E
ester.zhou 已提交
4782 4783
      }
    }
4784 4785 4786 4787 4788
  })

  // 3. Use h5Port to send messages to the eTS side.
  function PostMsgToEts(data) {
    h5Port.postMessage(data)
E
ester.zhou 已提交
4789 4790 4791
  }
  ```

4792
### getUrl<sup>9+</sup>
E
ester.zhou 已提交
4793

4794
getUrl(): string
E
ester.zhou 已提交
4795

4796
Obtains the URL of this page.
E
ester.zhou 已提交
4797

4798
**Return value**
E
ester.zhou 已提交
4799

4800 4801 4802
| Type    | Description         |
| ------ | ----------- |
| string | URL of the current page.|
E
ester.zhou 已提交
4803 4804 4805 4806 4807 4808 4809 4810

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4811
    controller: WebController = new WebController()
E
ester.zhou 已提交
4812 4813
    build() {
      Column() {
4814
        Button('getUrl')
E
ester.zhou 已提交
4815
          .onClick(() => {
4816
            console.log("url: " + this.controller.getUrl())
E
ester.zhou 已提交
4817 4818 4819 4820 4821 4822 4823
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4824
### searchAllAsync<sup>9+</sup>
E
ester.zhou 已提交
4825

4826
searchAllAsync(searchString: string): void
E
ester.zhou 已提交
4827

4828
Searches the web page for content that matches the keyword specified by **'searchString'** and highlights the matches on the page. This API returns the result asynchronously through [onSearchResultReceive](#onsearchresultreceive9).
E
ester.zhou 已提交
4829 4830 4831

**Parameters**

4832 4833 4834
| Name         | Type  | Mandatory  | Default Value | Description   |
| ------------ | ------ | ---- | ---- | ------- |
| searchString | string | Yes   | -    | Search keyword.|
E
ester.zhou 已提交
4835 4836 4837 4838 4839 4840 4841 4842

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4843
    controller: WebController = new WebController()
4844 4845
    @State searchString: string = "xxx"

E
ester.zhou 已提交
4846 4847
    build() {
      Column() {
4848 4849 4850 4851 4852 4853 4854 4855 4856
        Button('searchString')
          .onClick(() => {
            this.controller.searchAllAsync(this.searchString)
          })
        Button('clearMatches')
          .onClick(() => {
            this.controller.clearMatches()
          })
        Button('searchNext')
E
ester.zhou 已提交
4857
          .onClick(() => {
4858
            this.controller.searchNext(true)
E
ester.zhou 已提交
4859 4860
          })
        Web({ src: 'www.example.com', controller: this.controller })
4861 4862 4863 4864
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting)
          })
E
ester.zhou 已提交
4865 4866 4867 4868 4869
      }
    }
  }
  ```

4870
### clearMatches<sup>9+</sup>
E
ester.zhou 已提交
4871

4872
clearMatches(): void
E
ester.zhou 已提交
4873

4874
Clears the matches found through [searchAllAsync](#searchallasync9).
E
ester.zhou 已提交
4875 4876 4877

**Example**

4878 4879 4880 4881 4882
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4883
    controller: WebController = new WebController()
4884

4885 4886
    build() {
      Column() {
4887
        Button('clearMatches')
4888
          .onClick(() => {
4889
            this.controller.clearMatches()
4890 4891 4892 4893 4894 4895 4896
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4897
### searchNext<sup>9+</sup>
4898

4899
searchNext(forward: boolean): void
4900

4901
Searches for and highlights the next match.
4902

E
ester.zhou 已提交
4903
**Parameters**
4904

4905 4906 4907 4908
| Name    | Type   | Mandatory  | Default Value | Description       |
| ------- | ------- | ---- | ---- | ----------- |
| forward | boolean | Yes   | -    | Whether to search forward.|

4909 4910

**Example**
E
ester.zhou 已提交
4911

4912 4913 4914 4915 4916
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4917
    controller: WebController = new WebController()
4918

4919 4920
    build() {
      Column() {
4921
        Button('searchNext')
4922
          .onClick(() => {
4923
            this.controller.searchNext(true)
4924 4925 4926 4927 4928 4929 4930
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4931 4932
## HitTestValue<sup>9+</sup>
Implements the **HitTestValue** object. For the sample code, see [getHitTestValue](#gethittestvalue9).
E
ester.zhou 已提交
4933

4934 4935
### getType<sup>9+</sup>
getType(): HitTestType
E
ester.zhou 已提交
4936

4937
Obtains the element type of the area being clicked.
E
ester.zhou 已提交
4938 4939 4940

**Return value**

4941 4942 4943
| Type                             | Description           |
| ------------------------------- | ------------- |
| [HitTestType](#hittesttype)| Element type of the area being clicked.|
4944

4945 4946
### getExtra<sup>9+</sup>
getExtra(): string
4947

4948
Obtains the extra information of the area being clicked. If the area being clicked is an image or a link, the extra information is the URL of the image or link.
4949

4950
**Return value**
4951

4952 4953 4954
| Type    | Description          |
| ------ | ------------ |
| string | Extra information of the area being clicked.|
4955

E
ester.zhou 已提交
4956

4957
## WebCookie
4958

4959
Manages behavior of cookies in **\<Web>** components. All **\<Web>** components in an application share a **WebCookie**. You can use the **getCookieManager** API in **controller** to obtain the **WebCookie** for subsequent cookie management.
4960

4961 4962
### setCookie<sup>9+</sup>
setCookie(url: string, value: string): boolean
4963

4964
Sets the cookie. This API returns the result synchronously. Returns **true** if the operation is successful; returns **false** otherwise.
4965 4966

**Parameters**
E
ester.zhou 已提交
4967

4968 4969 4970 4971
| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | Yes   | -    | URL of the cookie to set.|
| value | string | Yes   | -    | Value of the cookie to set.        |
E
ester.zhou 已提交
4972 4973 4974

**Return value**

4975 4976 4977
| Type     | Description           |
| ------- | ------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
4978 4979

**Example**
E
ester.zhou 已提交
4980

4981 4982 4983 4984 4985
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4986
    controller: WebController = new WebController()
4987
  
4988 4989
    build() {
      Column() {
4990
        Button('setCookie')
4991
          .onClick(() => {
E
ester.zhou 已提交
4992
            let result = this.controller.getCookieManager().setCookie("https://www.example.com", "a=b")
4993
            console.log("result: " + result)
4994 4995 4996 4997 4998 4999 5000
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5001 5002
### getCookie<sup>9+</sup>
getCookie(url: string): string
5003

5004
Obtains the cookie value corresponding to the specified URL.
5005 5006

**Parameters**
E
ester.zhou 已提交
5007

5008 5009 5010 5011 5012 5013 5014 5015 5016
| Name | Type  | Mandatory  | Default Value | Description             |
| ---- | ------ | ---- | ---- | ----------------- |
| url  | string | Yes   | -    | URL of the cookie value to obtain.|

**Return value**

| Type    | Description               |
| ------ | ----------------- |
| string | Cookie value corresponding to the specified URL.|
5017 5018

**Example**
E
ester.zhou 已提交
5019

5020 5021
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5022
  import web_webview from '@ohos.web.webview'
5023 5024 5025
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5026
    controller: WebController = new WebController()
5027
  
5028 5029
    build() {
      Column() {
5030
        Button('getCookie')
5031
          .onClick(() => {
5032 5033
            let value = web_webview.WebCookieManager.getCookie('www.example.com')
            console.log("value: " + value)
5034 5035 5036 5037 5038 5039 5040
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5041 5042
### setCookie<sup>9+</sup>
setCookie(url: string, value: string): boolean
5043

5044
Sets a cookie value for the specified URL.
5045 5046

**Parameters**
E
ester.zhou 已提交
5047

5048 5049 5050 5051 5052 5053 5054 5055 5056 5057
| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | Yes   | -    | URL of the cookie to set.|
| value | string | Yes   | -    | Cookie value to set.    |

**Return value**

| Type     | Description           |
| ------- | ------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
5058 5059

**Example**
E
ester.zhou 已提交
5060

5061 5062
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5063
  import web_webview from '@ohos.web.webview'
5064 5065 5066
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5067
    controller: WebController = new WebController()
5068
  
5069 5070
    build() {
      Column() {
5071
        Button('setCookie')
5072
          .onClick(() => {
5073 5074
            let result = web_webview.WebCookieManager.setCookie('www.example.com', 'a=b')
            console.log("result: " + result)
5075 5076 5077 5078 5079 5080
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
5081

5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114
### saveCookieSync<sup>9+</sup>
saveCookieSync(): boolean

Saves the cookies in the memory to the drive. This API returns the result synchronously.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| boolean | Operation result.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Button('saveCookieSync')
          .onClick(() => {
            let result = web_webview.WebCookieManager.saveCookieSync()
            console.log("result: " + result)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
5115 5116
### saveCookieAsync<sup>9+</sup>
saveCookieAsync(): Promise\<boolean>
5117

5118
Saves the cookies in the memory to the drive. This API uses a promise to return the value.
5119 5120 5121

**Return value**

5122 5123 5124
| Type               | Description                         |
| ----------------- | --------------------------- |
| Promise\<boolean> | Promise used to return the result.|
5125 5126

**Example**
E
ester.zhou 已提交
5127

5128 5129
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5130
  import web_webview from '@ohos.web.webview'
5131 5132 5133
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5134
    controller: WebController = new WebController()
5135
  
5136 5137
    build() {
      Column() {
5138
        Button('saveCookieAsync')
5139
          .onClick(() => {
5140 5141 5142
            web_webview.WebCookieManager.saveCookieAsync()
              .then (function(result) {
                console.log("result: " + result)
5143
              })
5144 5145
              .catch(function(error) {
                console.error("error: " + error)
5146 5147 5148 5149 5150 5151 5152 5153
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5154 5155
### saveCookieAsync<sup>9+</sup>
saveCookieAsync(callback: AsyncCallback\<boolean>): void
5156

5157
Saves the cookies in the memory to the drive. This API uses an asynchronous callback to return the result.
5158 5159

**Parameters**
E
ester.zhou 已提交
5160

5161 5162 5163
| Name     | Type                   | Mandatory  | Default Value | Description                        |
| -------- | ----------------------- | ---- | ---- | ---------------------------- |
| callback | AsyncCallback\<boolean> | Yes   | -    | Callback used to return the operation result.|
5164 5165

**Example**
E
ester.zhou 已提交
5166

5167 5168
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5169
  import web_webview from '@ohos.web.webview'
5170 5171 5172
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5173
    controller: WebController = new WebController()
5174
  
5175 5176
    build() {
      Column() {
5177
        Button('saveCookieAsync')
5178
          .onClick(() => {
5179 5180
            web_webview.WebCookieManager.saveCookieAsync(function(result) {
              console.log("result: " + result)
5181 5182 5183 5184 5185 5186
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
5187
  ```
5188

5189 5190
### isCookieAllowed<sup>9+</sup>
isCookieAllowed(): boolean
E
ester.zhou 已提交
5191

5192
Checks whether the **WebCookieManager** instance has the permission to send and receive cookies.
5193 5194

**Return value**
E
ester.zhou 已提交
5195

5196 5197 5198
| Type     | Description                 |
| ------- | ------------------- |
| boolean | Whether the **WebCookieManager** instance has the permission to send and receive cookies.|
5199 5200

**Example**
E
ester.zhou 已提交
5201

5202 5203
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5204
  import web_webview from '@ohos.web.webview'
5205 5206 5207
  @Entry
  @Component
  struct WebComponent {
5208 5209
    controller: WebController = new WebController()
  
5210 5211
    build() {
      Column() {
5212
        Button('isCookieAllowed')
5213
          .onClick(() => {
5214 5215
            let result = web_webview.WebCookieManager.isCookieAllowed()
            console.log("result: " + result)
5216 5217 5218 5219 5220 5221 5222
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5223 5224
### putAcceptCookieEnabled<sup>9+</sup>
putAcceptCookieEnabled(accept: boolean): void
5225

5226
Sets whether the **WebCookieManager** instance has the permission to send and receive cookies.
5227 5228

**Parameters**
E
ester.zhou 已提交
5229

5230 5231 5232
| Name   | Type   | Mandatory  | Default Value | Description                 |
| ------ | ------- | ---- | ---- | --------------------- |
| accept | boolean | Yes   | -    | Whether the **WebCookieManager** instance has the permission to send and receive cookies.|
5233 5234

**Example**
E
ester.zhou 已提交
5235

5236 5237
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5238
  import web_webview from '@ohos.web.webview'
5239 5240 5241
  @Entry
  @Component
  struct WebComponent {
5242 5243
    controller: WebController = new WebController()
  
5244 5245
    build() {
      Column() {
5246
        Button('putAcceptCookieEnabled')
5247
          .onClick(() => {
5248
            web_webview.WebCookieManager.putAcceptCookieEnabled(false)
5249 5250 5251 5252 5253 5254 5255
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5256 5257
### isThirdPartyCookieAllowed<sup>9+</sup>
isThirdCookieAllowed(): boolean
E
ester.zhou 已提交
5258

5259
Checks whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
5260 5261

**Return value**
E
ester.zhou 已提交
5262

5263 5264 5265
| Type     | Description                    |
| ------- | ---------------------- |
| boolean | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.|
5266 5267

**Example**
E
ester.zhou 已提交
5268

5269 5270
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5271
  import web_webview from '@ohos.web.webview'
5272 5273 5274
  @Entry
  @Component
  struct WebComponent {
5275 5276
    controller: WebController = new WebController()
  
5277 5278
    build() {
      Column() {
5279
        Button('isThirdPartyCookieAllowed')
5280
          .onClick(() => {
5281 5282
            let result = web_webview.WebCookieManager.isThirdPartyCookieAllowed()
            console.log("result: " + result)
5283 5284 5285 5286 5287 5288 5289
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5290 5291
### putAcceptThirdPartyCookieEnabled<sup>9+</sup>
putAcceptThirdPartyCookieEnabled(accept: boolean): void
5292

5293
Sets whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.
5294 5295 5296

**Parameters**

5297 5298 5299
| Name   | Type   | Mandatory  | Default Value | Description                    |
| ------ | ------- | ---- | ---- | ------------------------ |
| accept | boolean | Yes   | -    | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.|
5300 5301

**Example**
E
ester.zhou 已提交
5302

5303 5304
  ```ts
  // xxx.ets
5305
  import web_webview from '@ohos.web.webview'
5306 5307 5308
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5309
    controller: WebController = new WebController()
5310
  
5311 5312
    build() {
      Column() {
5313
        Button('putAcceptThirdPartyCookieEnabled')
5314
          .onClick(() => {
5315
            web_webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(false)
5316 5317 5318 5319 5320 5321 5322
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5323 5324
### existCookie<sup>9+</sup>
existCookie(): boolean
5325

5326
Checks whether cookies exist.
5327

5328 5329 5330 5331 5332
**Return value**

| Type     | Description         |
| ------- | ----------- |
| boolean | Whether cookies exist.|
5333 5334

**Example**
E
ester.zhou 已提交
5335

5336 5337
  ```ts
  // xxx.ets
5338
  import web_webview from '@ohos.web.webview'
5339 5340 5341
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5342
    controller: WebController = new WebController()
5343
  
5344 5345
    build() {
      Column() {
5346
        Button('existCookie')
5347
          .onClick(() => {
5348 5349
            let result = web_webview.WebCookieManager.existCookie()
            console.log("result: " + result)
5350 5351 5352 5353 5354 5355 5356
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5357 5358
### deleteEntireCookie<sup>9+</sup>
deleteEntireCookie(): void
5359

5360
Deletes all cookies.
5361 5362

**Example**
E
ester.zhou 已提交
5363

5364 5365
  ```ts
  // xxx.ets
5366
  import web_webview from '@ohos.web.webview'
5367 5368 5369
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5370
    controller: WebController = new WebController()
5371
  
5372 5373
    build() {
      Column() {
5374
        Button('deleteEntireCookie')
5375
          .onClick(() => {
5376
            web_webview.WebCookieManager.deleteEntireCookie()
5377 5378 5379 5380 5381 5382 5383
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

5384 5385
### deleteSessionCookie<sup>9+</sup>
deleteSessionCookie(): void
5386

5387
Deletes all session cookies.
5388 5389

**Example**
E
ester.zhou 已提交
5390

5391 5392
  ```ts
  // xxx.ets
5393
  import web_webview from '@ohos.web.webview'
5394 5395 5396
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5397
    controller: WebController = new WebController()
5398
  
5399 5400
    build() {
      Column() {
5401 5402 5403
        Button('deleteSessionCookie')
          .onClick(() => {
            web_webview.WebCookieManager.deleteSessionCookie()
5404
          })
5405
        Web({ src: 'www.example.com', controller: this.controller })
5406 5407 5408 5409 5410
      }
    }
  }
  ```

E
ester.zhou 已提交
5411
## MessageLevel
5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422

| Name   | Description   |
| ----- | :---- |
| Debug | Debug level.|
| Error | Error level.|
| Info  | Information level.|
| Log   | Log level.|
| Warn  | Warning level. |

## RenderExitReason

E
ester.zhou 已提交
5423
Enumerates the reasons why the rendering process exits.
5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466

| Name                        | Description               |
| -------------------------- | ----------------- |
| ProcessAbnormalTermination | The rendering process exits abnormally.        |
| ProcessWasKilled           | The rendering process receives a SIGKILL message or is manually terminated.|
| ProcessCrashed             | The rendering process crashes due to segmentation or other errors.   |
| ProcessOom                 | The program memory is running low.          |
| ProcessExitUnknown         | Other reason.            |

## MixedMode

| Name        | Description                                |
| ---------- | ---------------------------------- |
| All        | HTTP and HTTPS hybrid content can be loaded. This means that all insecure content can be loaded.|
| Compatible | HTTP and HTTPS hybrid content can be loaded in compatibility mode. This means that some insecure content may be loaded.          |
| None       | HTTP and HTTPS hybrid content cannot be loaded.              |

## CacheMode
| Name     | Description                                  |
| ------- | ------------------------------------ |
| Default | The cache that has not expired is used to load the resources. If the resources do not exist in the cache, they will be obtained from the Internet.|
| None    | The cache is used to load the resources. If the resources do not exist in the cache, they will be obtained from the Internet.    |
| Online  | The cache is not used to load the resources. All resources are obtained from the Internet.              |
| Only    | The cache alone is used to load the resources.                       |

## FileSelectorMode
| Name                  | Description        |
| -------------------- | ---------- |
| FileOpenMode         | Open and upload a file. |
| FileOpenMultipleMode | Open and upload multiple files. |
| FileOpenFolderMode   | Open and upload a folder.|
| FileSaveMode         | Save a file.   |

 ## HitTestType

| Name           | Description                      |
| ------------- | ------------------------ |
| EditText      | Editable area.                 |
| Email         | Email address.                 |
| HttpAnchor    | Hyperlink whose **src** is **http**.          |
| HttpAnchorImg | Image with a hyperlink, where **src** is **http**.|
| Img           | HTML::img tag.            |
| Map           | Geographical address.                   |
5467
| Phone         | Phone number.                   |
5468 5469
| Unknown       | Unknown content.                   |

E
ester.zhou 已提交
5470 5471 5472 5473
## SslError<sup>9+</sup>

Enumerates the error codes returned by **onSslErrorEventReceive** API.

5474 5475 5476 5477 5478 5479
| Name          | Description         |
| ------------ | ----------- |
| Invalid      | Minor error.      |
| HostMismatch | The host name does not match.    |
| DateInvalid  | The certificate has an invalid date.    |
| Untrusted    | The certificate issuer is not trusted.|
E
ester.zhou 已提交
5480

5481 5482
## ProtectedResourceType<sup>9+</sup>

5483 5484
| Name       | Description           | Remarks                        |
| --------- | ------------- | -------------------------- |
E
ester.zhou 已提交
5485
| MidiSysex | MIDI SYSEX resource.| Currently, only permission events can be reported. MIDI devices are not yet supported.|
5486

5487 5488 5489 5490 5491 5492
## WebDarkMode<sup>9+</sup>
| Name     | Description                                  |
| ------- | ------------------------------------ |
| Off     | The web dark mode is disabled.                    |
| On      | The web dark mode is enabled.                    |
| Auto    | The web dark mode setting follows the system settings.                |
E
ester.zhou 已提交
5493

5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590
## WebAsyncController

Implements a **WebAsyncController** object, which can be used to control the behavior of a **\<Web>** component with asynchronous callbacks. A **WebAsyncController **object controls one **\<Web>** component.

### Creating an Object

```
webController: WebController = new WebController();
webAsyncController: WebAsyncController = new WebAsyncController(webController);
```

### storeWebArchive<sup>9+</sup>

storeWebArchive(baseName: string, autoName: boolean, callback: AsyncCallback\<string>): void

Stores this web page. This API uses an asynchronous callback to return the result.

**Parameters**

| Name      | Type                                    | Mandatory   | Description                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| baseName | string | Yes | Save path. The value cannot be null. |
| autoName | boolean | Yes | Whether to automatically generate a file name.<br/>The value **false** means not to automatically generate a file name.<br/>The value **true** means to automatically generate a file name based on the URL of current page and the **baseName** value. In this case, **baseName** is regarded as a directory. |
| callback | AsyncCallback\<string> | Yes    | Callback used to return the save path if the operation is successful and null otherwise. |

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
            let webAsyncController = new web_webview.WebAsyncController(this.controller)
            webAsyncController.storeWebArchive("/data/storage/el2/base/", true, (filename) => {
              if (filename != null) {
                console.info(`save web archive success: ${filename}`)
              }
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### storeWebArchive<sup>9+</sup>

storeWebArchive(baseName: string, autoName: boolean): Promise\<string>

Stores this web page. This API uses a promise to return the result.

**Parameters**

| Name      | Type                                     | Mandatory   | Description                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| baseName | string | Yes | Save path. The value cannot be null. |
| autoName | boolean | Yes | Whether to automatically generate a file name.<br>The value **false** means not to automatically generate a file name.<br>The value **true** means to automatically generate a file name based on the URL of current page and the **baseName** value. In this case, **baseName** is regarded as a directory. |

**Return value**

| Type              | Description                               |
| --------------- | -------------------------------- |
| Promise\<string> | Promise used to return the save path if the operation is successful and null otherwise. |

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
            let webAsyncController = new web_webview.WebAsyncController(this.controller);
            webAsyncController.storeWebArchive("/data/storage/el2/base/", true)
              .then(filename => {
                if (filename != null) {
                  console.info(`save web archive success: ${filename}`)
                }
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
5591 5592
## WebMessagePort<sup>9+</sup>

5593
Implements a **WebMessagePort** instance, which can be used to send and receive messages.
E
ester.zhou 已提交
5594 5595 5596 5597 5598 5599 5600 5601 5602

### close<sup>9+</sup>
close(): void

Disables this message port.

### postMessageEvent<sup>9+</sup>
postMessageEvent(message: WebMessageEvent): void

E
ester.zhou 已提交
5603
Sends messages. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5604 5605 5606

**Parameters**

5607 5608 5609
| Name    | Type                                | Mandatory  | Default Value | Description   |
| ------- | ------------------------------------ | ---- | ---- | ------- |
| message | [WebMessageEvent](#webmessageevent9) | Yes   | -    | Message to send.|
E
ester.zhou 已提交
5610 5611 5612 5613 5614 5615 5616 5617

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5618 5619
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5620 5621 5622 5623 5624

    build() {
      Column() {
        Button('postMessageEvent')
          .onClick(() => {
E
ester.zhou 已提交
5625 5626 5627
            var msg = new WebMessageEvent()
            msg.setData("post message from ets to html5")
            this.ports[0].postMessageEvent(msg)
E
ester.zhou 已提交
5628 5629 5630 5631 5632 5633 5634 5635 5636 5637
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### onMessageEvent<sup>9+</sup>
onMessageEvent(callback: (result: string) => void): void

E
ester.zhou 已提交
5638
Registers a callback to receive messages from an HTML5 page. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5639 5640 5641

**Parameters**

5642 5643 5644
| Name     | Type    | Mandatory  | Default Value | Description      |
| -------- | -------- | ---- | ---- | ---------- |
| callback | function | Yes   | -    | Callback for receiving messages.|
E
ester.zhou 已提交
5645 5646 5647 5648 5649 5650 5651 5652

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5653 5654
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681

    build() {
      Column() {
        Button('onMessageEvent')
          .onClick(() => {
            this.ports[0].onMessageEvent((result: string) => {
              console.log("received message from html5, on message:" + result);
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```


## WebMessageEvent<sup>9+</sup>

Implements the **WebMessageEvent** object to encapsulate the message and port.

### getData<sup>9+</sup>
getData(): string

Obtains the messages stored in this object.

**Return value**

5682 5683
| Type    | Description            |
| ------ | -------------- |
E
ester.zhou 已提交
5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697
| string | Message stored in the object of this type.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    build() {
      Column() {
        Button('getPorts')
          .onClick(() => {
            var msgEvent = new WebMessageEvent();
E
ester.zhou 已提交
5698 5699 5700
            msgEvent.setData("message event data")
            var messageData = msgEvent.getData()
            console.log("message is:" + messageData)
E
ester.zhou 已提交
5701 5702 5703 5704 5705 5706 5707 5708 5709
          })
      }
    }
  }
  ```

### setData<sup>9+</sup>
setData(data: string): void

E
ester.zhou 已提交
5710
Sets the message in this object. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5711 5712 5713

**Parameters**

5714 5715 5716
| Name | Type  | Mandatory  | Default Value | Description   |
| ---- | ------ | ---- | ---- | ------- |
| data | string | Yes   | -    | Message to send.|
E
ester.zhou 已提交
5717 5718 5719 5720 5721 5722 5723 5724

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5725 5726
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5727 5728 5729 5730 5731

    build() {
      Column() {
        Button('setData')
          .onClick(() => {
E
ester.zhou 已提交
5732 5733 5734
            var msg = new WebMessageEvent()
            msg.setData("post message from ets to HTML5")
            this.ports[0].postMessageEvent(msg)
E
ester.zhou 已提交
5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
### getPorts<sup>9+</sup>
getPorts(): Array\<WebMessagePort\>

Obtains the message port stored in this object.

**Return value**

5748 5749
| Type                                      | Description              |
| ---------------------------------------- | ---------------- |
E
ester.zhou 已提交
5750 5751 5752 5753 5754 5755 5756 5757 5758
| Array\<[WebMessagePort](#webmessageport9)\> | Message port stored in the object of this type.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5759
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5760 5761 5762 5763
    build() {
      Column() {
        Button('getPorts')
          .onClick(() => {
E
ester.zhou 已提交
5764 5765 5766 5767 5768
            var sendPortArray = new Array(this.ports[0])
            var msgEvent = new WebMessageEvent()
            msgEvent.setPorts(sendPortArray)
            var getPorts = msgEvent.getPorts()
            console.log("Ports is:" + getPorts)
E
ester.zhou 已提交
5769 5770 5771 5772 5773 5774 5775 5776 5777
          })
      }
    }
  }
  ```

### setPorts<sup>9+</sup>
setPorts(ports: Array\<WebMessagePort\>): void

E
ester.zhou 已提交
5778
Sets the message port in this object. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5779 5780 5781

**Parameters**

5782 5783 5784
| Name  | Type                                    | Mandatory  | Default Value | Description     |
| ----- | ---------------------------------------- | ---- | ---- | --------- |
| ports | Array\<[WebMessagePort](#webmessageport9)\> | Yes   | -    | Message port.|
E
ester.zhou 已提交
5785 5786 5787 5788 5789 5790 5791 5792

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5793 5794
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5795 5796 5797 5798 5799
  
    build() {
      Column() {
        Button('setPorts')
          .onClick(() => {
E
ester.zhou 已提交
5800 5801 5802 5803 5804
            var sendPortArray = new Array(this.ports[1])
            var msgEvent = new WebMessageEvent()
            msgEvent.setData("__init_ports__")
            msgEvent.setPorts(sendPortArray)
            this.controller.postMessage({message: msgEvent, uri: "*"})
E
ester.zhou 已提交
5805 5806 5807 5808 5809 5810
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868

## DataResubmissionHandler<sup>9+</sup>

Implements the **DataResubmissionHandler** object for resubmitting or canceling the web form data.

### resend<sup>9+</sup>

resend(): void

Resends the web form data.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onDataResubmitted((event) => {
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

###  cancel<sup>9+</sup>

cancel(): void

Cancels the resending of web form data.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onDataResubmitted((event) => {
          console.log('onDataResubmitted')
          event.handler.cancel();
        })
      }
    }
  }
  ```