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

3 4 5 6
> **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 已提交
7

E
ester.zhou 已提交
8 9
The **<Web\>** component can be used to display web pages.

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: WebController | WebviewController})
E
ester.zhou 已提交
20 21 22 23

> **NOTE**
>
> Transition animation is not supported.
24 25

**Parameters**
E
ester.zhou 已提交
26 27 28 29

| Name       | Type                           | Mandatory  | Description   |
| ---------- | ------------------------------- | ---- | ------- |
| src        | [ResourceStr](ts-types.md)                           | Yes   | Address of a web page resource.|
E
ester.zhou 已提交
30
| controller | [WebController](#webcontroller) or WebviewController |Yes   | Controller.   |
31 32

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

E
ester.zhou 已提交
34
  Example of loading online web pages:
35 36 37 38 39
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
40
    controller: WebController = new WebController()
41 42
    build() {
      Column() {
E
ester.zhou 已提交
43
        Web({ src: 'www.example.com', controller: this.controller })
44 45 46 47
      }
    }
  }
  ```
E
ester.zhou 已提交
48 49 50
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
E
ester.zhou 已提交
51

E
ester.zhou 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
  @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 已提交
65 66 67 68 69
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
70
    controller: WebController = new WebController()
E
ester.zhou 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
      }
    }
  }
  ```

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

## Attributes
90 91 92 93 94 95 96 97 98 99

The **\<Web>** component has network attributes.

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

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

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

101 102 103 104 105
| Name             | Type   | Mandatory  | Default Value | Description                                |
| ---------------- | ------- | ---- | ---- | ------------------------------------ |
| domStorageAccess | boolean | Yes   | false | Whether to enable the DOM Storage API.|

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

107 108 109 110 111
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
112
    controller: WebController = new WebController()
113 114
    build() {
      Column() {
E
ester.zhou 已提交
115 116
        Web({ src: 'www.example.com', controller: this.controller })
          .domStorageAccess(true)
117 118 119 120 121 122 123 124 125
      }
    }
  }
  ```

### fileAccess

fileAccess(fileAccess: boolean)

E
ester.zhou 已提交
126
Sets whether to enable access to the file system in the application. Access to the files in **rawfile** specified through [$rawfile(filepath/filename)](../../quick-start/resource-categories-and-access.md) are not affected by the setting.
127 128

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

130 131
| Name       | Type   | Mandatory  | Default Value | Description                                    |
| ---------- | ------- | ---- | ---- | ---------------------------------------- |
E
ester.zhou 已提交
132
| fileAccess | boolean | Yes   | true | Whether to enable access to the file system in the application. By default, this feature is enabled.|
133 134

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

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

### fileFromUrlAccess<sup>9+</sup>

fileFromUrlAccess(fileFromUrlAccess: boolean)

E
ester.zhou 已提交
155
Sets whether to allow the use of JavaScript scripts on web pages for access to content in the application file system. By default, this feature is disabled. Access to the files in **rawfile** specified through [$rawfile(filepath/filename)](../../quick-start/resource-categories-and-access.md) are not affected by the setting.
156 157

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

159 160
| Name              | Type   | Mandatory  | Default Value  | Description                                    |
| ----------------- | ------- | ---- | ----- | ---------------------------------------- |
E
ester.zhou 已提交
161
| fileFromUrlAccess | boolean | Yes   | false | Whether to allow the use of JavaScript scripts on web pages for access to content in the application file system. By default, this feature is disabled.|
162 163

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

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

### imageAccess

imageAccess(imageAccess: boolean)

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

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

188 189
| Name        | Type   | Mandatory  | Default Value | Description           |
| ----------- | ------- | ---- | ---- | --------------- |
E
ester.zhou 已提交
190
| imageAccess | boolean | Yes   | true | Whether to enable automatic image loading. By default, this feature is enabled.|
191 192 193 194 195 196 197

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

### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
E
ester.zhou 已提交
211
    controller: WebController | WebviewController})
212 213 214 215

Injects a JavaScript object into the window. Methods of this object can be invoked in the window. The parameters cannot be updated.

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

217 218
| Name       | Type           | Mandatory  | Default Value | Description                     |
| ---------- | --------------- | ---- | ---- | ------------------------- |
E
ester.zhou 已提交
219
| object     | object          | Yes   | -    | Object to be registered. Methods can be declared, but attributes cannot.   |
220 221
| 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. |
E
ester.zhou 已提交
222
| controller | [WebController](#webcontroller) or WebviewController | Yes   | -    | Controller.   |
223 224

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

226 227 228 229 230
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
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 261 262 263 264
    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()
265 266
    testObj = {
      test: (data1, data2, data3) => {
E
ester.zhou 已提交
267 268 269 270
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
271 272
      },
      toString: () => {
E
ester.zhou 已提交
273
        console.log('toString' + "interface instead.")
274 275 276 277
      }
    }
    build() {
      Column() {
E
ester.zhou 已提交
278 279 280 281 282 283 284
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
285 286 287 288 289 290 291 292 293 294 295 296 297
        })
      }
    }
  }
  ```

### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

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

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

299 300 301 302 303
| Name             | Type   | Mandatory  | Default Value | Description               |
| ---------------- | ------- | ---- | ---- | ------------------- |
| javaScriptAccess | boolean | Yes   | true | Whether JavaScript scripts can be executed.|

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

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

### 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 已提交
327

328 329
| Name      | Type                       | Mandatory  | Default Value | Description     |
| --------- | --------------------------- | ---- | ---- | --------- |
E
ester.zhou 已提交
330
| mixedMode | [MixedMode](#mixedmode)| Yes   | MixedMode.None | Mixed content to load.|
331 332

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

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

### 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 已提交
357

358 359 360 361 362
| 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 已提交
363

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

### zoomAccess

zoomAccess(zoomAccess: boolean)

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

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

387 388 389 390 391
| Name       | Type   | Mandatory  | Default Value | Description         |
| ---------- | ------- | ---- | ---- | ------------- |
| zoomAccess | boolean | Yes   | true | Whether to enable zoom gestures.|

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

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

### 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 已提交
415

416 417
| Name               | Type   | Mandatory  | Default Value | Description           |
| ------------------ | ------- | ---- | ---- | --------------- |
E
ester.zhou 已提交
418
| overviewModeAccess | boolean | Yes   | true | Whether to load web pages by using the overview mode.|
419 420

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

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

### databaseAccess

databaseAccess(databaseAccess: boolean)

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

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

445 446
| Name           | Type   | Mandatory  | Default Value | Description             |
| -------------- | ------- | ---- | ---- | ----------------- |
E
ester.zhou 已提交
447
| databaseAccess | boolean | Yes   | false | Whether to enable database access.|
448 449

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

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

### geolocationAccess

geolocationAccess(geolocationAccess: boolean)

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

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

474 475
| Name           | Type   | Mandatory  | Default Value | Description             |
| -------------- | ------- | ---- | ---- | ----------------- |
E
ester.zhou 已提交
476
| geolocationAccess | boolean | Yes   | true    | Whether to enable geolocation access.|
477 478

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

480 481 482 483 484
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
485
    controller: WebController = new WebController()
486 487
    build() {
      Column() {
E
ester.zhou 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        Web({ src: 'www.example.com', controller: this.controller })
          .geolocationAccess(true)
      }
    }
  }
  ```

### mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

Sets whether a manual click is required for video playback.

**Parameters**

| Name      | Type  | Mandatory  | Default Value | Description     |
| --------- | ------ | ---- | ---- | --------- |
| access | boolean | Yes   | true    | Whether a manual click is required for video playback.|

**Example**

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

E
ester.zhou 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
### multiWindowAccess<sup>9+</sup>

multiWindowAccess(multiWindow: boolean)

Sets whether to enable the multi-window permission.

**Parameters**

| Name           | Type   | Mandatory  | Default Value | Description             |
| -------------- | ------- | ---- | ---- | ----------------- |
| 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)
      }
    }
  }
  ```

554 555 556 557 558 559 560
### cacheMode

cacheMode(cacheMode: CacheMode)

Sets the cache mode.

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

562 563 564 565 566
| Name      | Type                       | Mandatory  | Default Value | Description     |
| --------- | --------------------------- | ---- | ---- | --------- |
| cacheMode | [CacheMode](#cachemode)| Yes   | CacheMode.Default | Cache mode to set.|

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

568 569 570 571 572
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
573 574
    controller: WebController = new WebController()
    @State mode: CacheMode = CacheMode.None
575 576
    build() {
      Column() {
E
ester.zhou 已提交
577 578
        Web({ src: 'www.example.com', controller: this.controller })
          .cacheMode(this.mode)
579 580 581 582 583
      }
    }
  }
  ```

E
ester.zhou 已提交
584
### textZoomRatio<sup>9+</sup>
585 586 587 588 589 590

textZoomRatio(textZoomRatio: number)

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

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

592 593 594 595 596
| Name         | Type  | Mandatory  | Default Value | Description           |
| ------------ | ------ | ---- | ---- | --------------- |
| textZoomRatio | number | Yes   | 100 | Text zoom ratio to set.|

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

598 599 600 601 602
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
603 604
    controller: WebController = new WebController()
    @State atio: number = 150
605 606
    build() {
      Column() {
E
ester.zhou 已提交
607 608
        Web({ src: 'www.example.com', controller: this.controller })
          .textZoomRatio(this.atio)
609 610 611 612 613
      }
    }
  }
  ```

E
ester.zhou 已提交
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
### initialScale<sup>9+</sup>

initialScale(percent: number)

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

**Parameters**

| Name         | Type  | Mandatory  | Default Value | Description           |
| ------------ | ------ | ---- | ---- | --------------- |
| percent | number | Yes   | 100 | Scale factor of the entire page.|

**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)
      }
    }
  }
  ```

644 645 646 647 648 649 650
### userAgent

userAgent(userAgent: string)

Sets the user agent.

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

652 653 654 655 656
| Name      | Type  | Mandatory  | Default Value | Description     |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | Yes   | -    | User agent to set.|

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

658 659 660 661 662
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
663 664
    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'
665 666
    build() {
      Column() {
E
ester.zhou 已提交
667 668
        Web({ src: 'www.example.com', controller: this.controller })
          .userAgent(this.userAgent)
669 670 671 672
      }
    }
  }
  ```
E
ester.zhou 已提交
673

E
ester.zhou 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
### webDebuggingAccess<sup>9+</sup>

webDebuggingAccess(webDebuggingAccess: boolean)

Sets whether to enable web debugging.

**Parameters**

| Name      | Type  | Mandatory  | Default Value | Description     |
| --------- | ------ | ---- | ---- | --------- |
| webDebuggingAccess | boolean | Yes   | false    | Whether to enable web debugging.|

**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)
      }
    }
  }
  ```

>  **NOTE**<br>
E
ester.zhou 已提交
705
>
E
ester.zhou 已提交
706
>  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).
E
ester.zhou 已提交
707 708 709

## Events

E
ester.zhou 已提交
710
The universal events are not supported.
E
ester.zhou 已提交
711

712 713 714 715 716 717 718
### onAlert

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

Triggered when **alert()** is invoked to display an alert dialog box on the web page.

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

720 721 722 723 724 725 726
| 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) | The user's operation. |

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

728 729 730 731 732
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | If the callback returns **false**, the default dialog box is displayed. If the callback returns **true**, a system application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to notify the **\<Web>** component of the user's operation.|

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

734 735 736 737 738
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
739
    controller: WebController = new WebController()
740 741
    build() {
      Column() {
E
ester.zhou 已提交
742
        Web({ src: 'www.example.com', controller: this.controller })
743 744
          .onAlert((event) => {
            AlertDialog.show({
E
ester.zhou 已提交
745
              title: 'onAlert',
746
              message: 'text',
E
ester.zhou 已提交
747 748 749 750 751 752 753 754
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
755 756 757 758 759 760 761 762
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
E
ester.zhou 已提交
763
            return true
764 765 766 767 768 769 770 771 772 773
          })
      }
    }
  }
  ```

### onBeforeUnload

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

E
ester.zhou 已提交
774
Triggered when the current page is about to exit after the user refreshes or closes the page. If the user refreshes the page, this callback is invoked only when the page has obtained focus.
775 776

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

778 779 780 781 782 783 784
| 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) | The user's operation. |

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

786 787 788 789 790
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | If the callback returns **false**, the default dialog box is displayed. If the callback returns **true**, a system application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to notify the **\<Web>** component of the user's operation.|

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

792 793 794 795 796
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
797
    controller: WebController = new WebController()
798 799 800 801 802
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onBeforeUnload((event) => {
E
ester.zhou 已提交
803 804
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
E
ester.zhou 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
            AlertDialog.show({
              title: 'onBeforeUnload',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
E
ester.zhou 已提交
824
            return true
825 826 827 828 829 830 831 832 833 834 835 836 837
          })
      }
    }
  }
  ```

### onConfirm

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

Triggered when **confirm()** is invoked by the web page.

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

839 840 841 842 843 844 845
| 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) | The user's operation. |

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

847 848 849 850 851
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | If the callback returns **false**, the default dialog box is displayed. If the callback returns **true**, a system application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to notify the **\<Web>** component of the user's operation.|

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

853 854 855 856 857
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
858
    controller: WebController = new WebController()
859 860 861 862 863
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onConfirm((event) => {
E
ester.zhou 已提交
864 865 866
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            console.log("event.result:" + event.result)
867
            AlertDialog.show({
E
ester.zhou 已提交
868
              title: 'onConfirm',
869
              message: 'text',
E
ester.zhou 已提交
870 871 872 873 874 875 876 877
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
878 879 880 881 882 883 884 885
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
E
ester.zhou 已提交
886
            return true
887 888 889 890 891 892 893 894 895 896 897
          })
      }
    }
  }
  ```

### onPrompt<sup>9+</sup>

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

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

899 900 901 902 903 904 905
| 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) | The user's operation. |

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

907 908 909 910 911
| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | If the callback returns **false**, the default dialog box is displayed. If the callback returns **true**, a system application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to notify the **\<Web>** component of the user's operation.|

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

913 914 915 916 917
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
918
    controller: WebController = new WebController()
E
ester.zhou 已提交
919
  
920 921
    build() {
      Column() {
E
ester.zhou 已提交
922 923
        Web({ src: 'www.example.com', controller: this.controller })
          .onPrompt((event) => {
E
ester.zhou 已提交
924 925 926
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
E
ester.zhou 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
            AlertDialog.show({
              title: 'onPrompt',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
E
ester.zhou 已提交
946
            return true
E
ester.zhou 已提交
947 948
          })
      }
949 950 951 952 953 954 955 956 957 958 959
    }
  }
  ```

### onConsole

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

Triggered to notify the host application of a JavaScript console message.

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

961 962 963 964 965
| Name    | Type                             | Description     |
| ------- | --------------------------------- | --------- |
| message | [ConsoleMessage](#consolemessage) | Console message.|

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

967 968 969 970 971
| Type     | Description                                 |
| ------- | ----------------------------------- |
| boolean | Returns **true** if the message will not be printed to the console; returns **false** otherwise.|

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

973 974 975 976 977
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
978
    controller: WebController = new WebController()
979 980 981 982 983
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onConsole((event) => {
E
ester.zhou 已提交
984 985 986 987 988
            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
989 990 991 992 993 994 995 996 997 998 999
          })
      }
    }
  }
  ```

### onDownloadStart

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

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

1001 1002 1003 1004 1005 1006 1007 1008
| 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.                        |

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

1010 1011 1012 1013 1014
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1015
    controller: WebController = new WebController()
1016 1017 1018 1019 1020
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onDownloadStart((event) => {
E
ester.zhou 已提交
1021 1022 1023 1024 1025
            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)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
          })
      }
    }
  }
  ```

### onErrorReceive

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

Triggered when an error occurs during web page loading. For better results, simplify the implementation logic in the callback.

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

1040 1041 1042 1043 1044 1045
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| error   | [WebResourceError](#webresourceerror)    | Encapsulation of a web page resource loading error.|

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

1047 1048 1049 1050 1051
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1052
    controller: WebController = new WebController()
1053 1054 1055 1056 1057
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onErrorReceive((event) => {
E
ester.zhou 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066
            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)
1067
            for (let i of result) {
E
ester.zhou 已提交
1068
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
            }
          })
      }
    }
  }
  ```

### onHttpErrorReceive

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

Triggered when an HTTP error (the response code is greater than or equal to 400) occurs during web page resource loading.

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

1084 1085 1086 1087 1088 1089
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| error   | [WebResourceError](#webresourceerror)    | Encapsulation of a web page resource loading error.|

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

1091 1092 1093 1094 1095
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1096
    controller: WebController = new WebController()
1097 1098 1099 1100 1101
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpErrorReceive((event) => {
E
ester.zhou 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
            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)
1113
            for (let i of result) {
E
ester.zhou 已提交
1114
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
1115
            }
E
ester.zhou 已提交
1116 1117
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
1118
            for (let i of resph) {
E
ester.zhou 已提交
1119
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
            }
          })
      }
    }
  }
  ```

### onPageBegin

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


Triggered when the web page starts to be loaded. This API is triggered only for the main frame content, and not for the iframe or frameset content.

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

1136 1137 1138 1139 1140
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|

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

1142 1143 1144 1145 1146
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1147
    controller: WebController = new WebController()
1148 1149 1150 1151 1152
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onPageBegin((event) => {
E
ester.zhou 已提交
1153
            console.log('url:' + event.url)
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
          })
      }
    }
  }
  ```

### onPageEnd

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


Triggered when the web page loading is complete. This API is triggered only for the main frame content.

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

1169 1170 1171 1172 1173
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|

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

1175 1176 1177 1178 1179
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1180
    controller: WebController = new WebController()
1181 1182 1183 1184 1185
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onPageEnd((event) => {
E
ester.zhou 已提交
1186
            console.log('url:' + event.url)
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
          })
      }
    }
  }
  ```

### onProgressChange

onProgressChange(callback: (event?: { newProgress: number }) => void)

Triggered when the web page loading progress changes.

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

1201 1202 1203 1204
| Name        | Type  | Description                 |
| ----------- | ------ | --------------------- |
| newProgress | number | New loading progress. The value is an integer ranging from 0 to 100.|

E
ester.zhou 已提交
1205 1206
**Example**

1207 1208 1209 1210 1211
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1212
    controller: WebController = new WebController()
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
    }
  }
  ```

### onTitleReceive

onTitleReceive(callback: (event?: { title: string }) => void)

Triggered when the document title of the web page is changed.

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

1233 1234 1235 1236
| Name  | Type  | Description         |
| ----- | ------ | ------------- |
| title | string | Document title.|

E
ester.zhou 已提交
1237 1238
**Example**

1239 1240 1241 1242 1243
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1244
    controller: WebController = new WebController()
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
    }
  }
  ```

### onRefreshAccessedHistory

onRefreshAccessedHistory(callback: (event?: { url: string, isRefreshed: boolean }) => void)

Triggered when loading of the web page is complete. This API is used by an application to update the historical link it accessed.

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

1265 1266 1267
| Name        | Type   | Description                             |
| ----------- | ------- | --------------------------------- |
| url         | string  | URL to be accessed.                          |
E
ester.zhou 已提交
1268 1269 1270
| 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.|

**Example**
1271 1272 1273 1274 1275 1276

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1277
    controller: WebController = new WebController()
1278 1279 1280 1281 1282
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onRefreshAccessedHistory((event) => {
E
ester.zhou 已提交
1283
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
          })
      }
    }
  }
  ```

### onRenderExited

onRenderExited(callback: (event?: { renderExitReason: RenderExitReason }) => void)

Triggered when the rendering process exits abnormally.

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

1298 1299 1300 1301
| Name             | Type                                    | Description            |
| ---------------- | ---------------------------------------- | ---------------- |
| renderExitReason | [RenderExitReason](#renderexitreason)| Cause for the abnormal exit of the rendering process.|

E
ester.zhou 已提交
1302 1303
**Example**

1304 1305 1306 1307 1308
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1309
    controller: WebController = new WebController()
1310 1311 1312 1313 1314
  
    build() {
      Column() {
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
E
ester.zhou 已提交
1315
            console.log('reason:' + event.renderExitReason)
1316 1317 1318 1319 1320 1321 1322 1323
          })
      }
    }
  }
  ```

### onShowFileSelector<sup>9+</sup>

E
ester.zhou 已提交
1324
onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => boolean)
1325 1326 1327 1328

Triggered to process an HTML form whose input type is **file**, in response to the tapping of the **Select File** button.

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

1330 1331 1332 1333 1334
| Name         | Type                                    | Description             |
| ------------ | ---------------------------------------- | ----------------- |
| result       | [FileSelectorResult](#fileselectorresult9) | File selection result to be sent to the **\<Web>** component.|
| fileSelector | [FileSelectorParam](#fileselectorparam9) | Information about the file selector.      |

E
ester.zhou 已提交
1335 1336 1337 1338
**Return value**

| Type     | Description                                 |
| ------- | ----------------------------------- |
E
ester.zhou 已提交
1339
| boolean | The value **true** means that the pop-up window provided by the system is displayed. The value **false** means that the default web pop-up window is displayed.|
E
ester.zhou 已提交
1340 1341 1342 1343

**Example**

  ```ts
1344 1345 1346 1347
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1348
    controller: WebController = new WebController()
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

    build() {
      Column() {
        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)
              }
            })
E
ester.zhou 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
            return true
          })
      }
    }
  }
  ```

### onResourceLoad<sup>9+</sup>

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

Invoked to notify the **\<Web>** component of the URL of the loaded resource file.

**Parameters**

| Name | Type                                    | Description     |
| ---- | ---------------------------------------- | --------- |
| url | string | URL of the loaded resource file.|

**Example**

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

### onScaleChange<sup>9+</sup>

onScaleChange(callback: (event: {oldScale: number, newScale: number}) => void)

Invoked when the display ratio of this page changes.

**Parameters**

| Name | Type                                    | Description     |
| ---- | ---------------------------------------- | --------- |
| oldScale | number | Display ratio of the page before the change.|
| newScale | number | Display ratio of the page after the change.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController()
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onScaleChange((event) => {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
          })
      }
    }
  }
  ```

### onUrlLoadIntercept

onUrlLoadIntercept(callback: (event?: { data:string | WebResourceRequest }) => boolean)

Triggered when the **\<Web>** component is about to access a URL. This API is used to determine whether to block the access.

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

1451 1452 1453 1454 1455
| Name | Type                                    | Description     |
| ---- | ---------------------------------------- | --------- |
| data | string / [WebResourceRequest](#webresourcerequest) | URL information.|

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

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
| Type     | Description                      |
| ------- | ------------------------ |
| boolean | Returns **true** if the access is blocked; returns **false** otherwise.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1468
    controller: WebController = new WebController()
1469 1470 1471 1472 1473 1474
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
E
ester.zhou 已提交
1475
            return true
1476 1477 1478 1479 1480 1481 1482 1483
          })
      }
    }
  }
  ```

### onInterceptRequest<sup>9+</sup>

E
ester.zhou 已提交
1484
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
1485

E
ester.zhou 已提交
1486
Invoked when the **\<Web>** component is about to access a URL. This API is used to block the URL and return the response data.
1487 1488

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

1490 1491 1492 1493 1494
| Name    | Type                                    | Description       |
| ------- | ---------------------------------------- | ----------- |
| request | [WebResourceRequest](#webresourcerequest) | Information about the URL request.|

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

1496 1497
| Type                                      | Description                         |
| ---------------------------------------- | --------------------------- |
E
ester.zhou 已提交
1498 1499 1500
| [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.|

**Example**
1501 1502 1503 1504 1505 1506

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1507 1508 1509
    controller: WebController = new WebController()
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
    @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() {
E
ester.zhou 已提交
1521
        Web({ src: 'www.example.com', controller: this.controller })
1522
          .onInterceptRequest((event) => {
E
ester.zhou 已提交
1523
            console.log('url:' + event.request.getRequestUrl())
1524 1525 1526 1527 1528 1529 1530 1531
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
E
ester.zhou 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540
            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
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
          })
      }
    }
  }
  ```

### onHttpAuthRequest<sup>9+</sup>

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

E
ester.zhou 已提交
1551
Invoked when an HTTP authentication request is received.
1552 1553

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

1555 1556 1557 1558 1559 1560 1561
| Name    | Type                                | Description            |
| ------- | ------------------------------------ | ---------------- |
| handler | [HttpAuthHandler](#httpauthhandler9) | The user's operation.  |
| host    | string                               | Host to which HTTP authentication credentials apply.|
| realm   | string                               | Realm to which HTTP authentication credentials apply. |

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

1563 1564
| Type     | Description                   |
| ------- | --------------------- |
E
ester.zhou 已提交
1565
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
1566 1567 1568 1569 1570

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1571
  import web_webview from '@ohos.web.webview'
1572 1573 1574
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1575 1576
    controller: WebController = new WebController()
    httpAuth: boolean = false
E
ester.zhou 已提交
1577
  
1578 1579
    build() {
      Column() {
E
ester.zhou 已提交
1580 1581 1582 1583 1584 1585 1586 1587
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
              title: 'onHttpAuthRequest',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
E
ester.zhou 已提交
1588
                  event.handler.cancel()
1589
                }
E
ester.zhou 已提交
1590 1591 1592 1593
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
E
ester.zhou 已提交
1594
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
E
ester.zhou 已提交
1595 1596 1597 1598 1599 1600 1601
                  if (this.httpAuth == false) {
                    web_webview.WebDataBase.saveHttpAuthCredentials(
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
E
ester.zhou 已提交
1602
                    event.handler.cancel()
E
ester.zhou 已提交
1603 1604 1605 1606
                  }
                }
              },
              cancel: () => {
E
ester.zhou 已提交
1607
                event.handler.cancel()
1608
              }
E
ester.zhou 已提交
1609
            })
E
ester.zhou 已提交
1610
            return true
1611
          })
E
ester.zhou 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
      }
    }
  }
  ```
### onSslErrorEventReceive<sup>9+</sup>

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

Invoked when an SSL error occurs during resource loading.

**Parameters**

| Name    | Type                          | Description            |
| ------- | ------------------------------------ | ----------------    |
| handler | [SslErrorHandler](#sslerrorhandler9) | The user's operation.|
| error   | [SslError](#sslerror9)       | Error code.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1637
    controller: WebController = new WebController()
E
ester.zhou 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
  
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onSslErrorEventReceive((event) => {
            AlertDialog.show({
              title: 'onSslErrorEventReceive',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
E
ester.zhou 已提交
1649
                  event.handler.handleConfirm()
E
ester.zhou 已提交
1650 1651 1652 1653 1654
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
E
ester.zhou 已提交
1655
                  event.handler.handleCancel()
E
ester.zhou 已提交
1656 1657 1658
                }
              },
              cancel: () => {
E
ester.zhou 已提交
1659
                event.handler.handleCancel()
E
ester.zhou 已提交
1660 1661
              }
            })
E
ester.zhou 已提交
1662
            return true
E
ester.zhou 已提交
1663 1664 1665 1666 1667 1668 1669 1670
          })
      }
    }
  }
  ```

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

E
ester.zhou 已提交
1671
onClientAuthenticationRequest(callback: (event: {handler : ClientAuthenticationHandler, host : string, port : number, keyTypes : Array\<string>, issuers : Array\<string>}) => void)
E
ester.zhou 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681

Invoked when an SSL client certificate request is received.

**Parameters**

| Name  | Type                            | Description            |
| ------- | ------------------------------------ | ----------------    |
| handler | [ClientAuthenticationHandler](#clientauthenticationhandler9) | The user's operation.|
| host    | string          | Host name of the server that requests a certificate.|
| port    | number          | Port number of the server that requests a certificate.|
E
ester.zhou 已提交
1682 1683
| keyTypes| Array\<string>  | Acceptable asymmetric private key types.|
| issuers | Array\<string>  | Issuer of the certificate that matches the private key.|
E
ester.zhou 已提交
1684 1685 1686 1687 1688 1689 1690 1691

  **Example**
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1692
    controller: WebController = new WebController()
E
ester.zhou 已提交
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'onClientAuthenticationRequest',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
E
ester.zhou 已提交
1704
                  event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem")
E
ester.zhou 已提交
1705 1706 1707 1708 1709
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
E
ester.zhou 已提交
1710
                  event.handler.cancel()
E
ester.zhou 已提交
1711 1712 1713
                }
              },
              cancel: () => {
E
ester.zhou 已提交
1714
                event.handler.ignore()
E
ester.zhou 已提交
1715 1716
              }
            })
E
ester.zhou 已提交
1717
            return true
E
ester.zhou 已提交
1718 1719
          })
      }
1720 1721 1722
    }
  }
  ```
E
ester.zhou 已提交
1723

1724 1725 1726 1727 1728 1729 1730
### onPermissionRequest<sup>9+</sup>

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

Invoked when a permission request is received.

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

1732 1733 1734 1735
| Name    | Type                                | Description            |
| ------- | ------------------------------------ | ---------------- |
| request | [PermissionRequest](#permissionrequest9) | The user's operation.  |

E
ester.zhou 已提交
1736 1737 1738 1739 1740 1741 1742
**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1743
    controller: WebController = new WebController()
E
ester.zhou 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              primaryButton: {
                value: 'deny',
                action: () => {
E
ester.zhou 已提交
1754
                  event.request.deny()
E
ester.zhou 已提交
1755 1756 1757 1758 1759
                }
              },
              secondaryButton: {
                value: 'onConfirm',
                action: () => {
E
ester.zhou 已提交
1760
                  event.request.grant(event.request.getAccessibleResource())
E
ester.zhou 已提交
1761 1762 1763
                }
              },
              cancel: () => {
E
ester.zhou 已提交
1764
                event.request.deny()
E
ester.zhou 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
              }
            })
          })
      }
    }
  }
  ```

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

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

E
ester.zhou 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
Invoked when a context menu is displayed upon a long press on a specific element (such as an image or 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)

Invoked when the scrollbar of the page scrolls.

**Parameters**

| Name    | Type                                | Description            |
| ------- | ------------------------------------ | ---------------- |
| xOffset   | number   | Position of the scrollbar on the x-axis.|
| yOffset  | number | Position of the scrollbar on the y-axis.|

**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) | The user's 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)

Triggered to notify the user that the request for obtaining the geolocation information received when **[onGeolocationShow](#ongeolocationshow)** is called has been canceled.

**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)

Registers a callback for the component's entering into full screen mode.

**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>
E
ester.zhou 已提交
1957

E
ester.zhou 已提交
1958
onFullScreenExit(callback: () => void)
E
ester.zhou 已提交
1959

E
ester.zhou 已提交
1960
Registers a callback for the component's exiting full screen mode.
E
ester.zhou 已提交
1961

E
ester.zhou 已提交
1962
**Parameters**
E
ester.zhou 已提交
1963

E
ester.zhou 已提交
1964 1965 1966
| Name     | Type                        | Description         |
| ----------- | ------------------------------- | ---------------- |
| callback     | () => void           | Callback invoked when the component exits full screen mode.|
E
ester.zhou 已提交
1967 1968 1969 1970 1971 1972 1973 1974

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1975 1976
    controller:WebController = new WebController()
    handler: FullScreenExitHandler = null
E
ester.zhou 已提交
1977 1978
    build() {
      Column() {
E
ester.zhou 已提交
1979 1980 1981 1982 1983 1984 1985
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenExit(() => {
          console.log("onFullScreenExit...")
          this.handler.exitFullScreen()
        })
        .onFullScreenEnter((event) => {
          this.handler = event.handler
E
ester.zhou 已提交
1986 1987 1988 1989 1990 1991
        })
      }
    }
  }
  ```

E
ester.zhou 已提交
1992
### onWindowNew<sup>9+</sup>
E
ester.zhou 已提交
1993

E
ester.zhou 已提交
1994
onWindowNew(callback: (event: {isAlert: boolean, isUserTrigger: boolean, targetUrl: string, handler: ControllerHandler}) => void)
E
ester.zhou 已提交
1995

E
ester.zhou 已提交
1996
Registers a callback for window creation.
E
ester.zhou 已提交
1997 1998 1999

**Parameters**

E
ester.zhou 已提交
2000 2001 2002 2003 2004 2005
| 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.|
E
ester.zhou 已提交
2006 2007 2008 2009 2010 2011 2012 2013

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2014
    controller:WebController = new WebController()
E
ester.zhou 已提交
2015 2016
    build() {
      Column() {
E
ester.zhou 已提交
2017 2018 2019 2020 2021 2022
        Web({ src:'www.example.com', controller: this.controller })
        .multiWindowAccess(true)
        .onWindowNew((event) => {
          console.log("onWindowNew...")
          var popController: WebController = new WebController()
          event.handler.setWebController(popController)
E
ester.zhou 已提交
2023 2024 2025 2026 2027 2028
        })
      }
    }
  }
  ```

E
ester.zhou 已提交
2029
### onWindowExit<sup>9+</sup>
E
ester.zhou 已提交
2030

E
ester.zhou 已提交
2031
onWindowExit(callback: () => void)
E
ester.zhou 已提交
2032

E
ester.zhou 已提交
2033
Registers a callback for window closure.
E
ester.zhou 已提交
2034 2035 2036 2037 2038

**Parameters**

| Name     | Type                        | Description         |
| ----------- | ------------------------------- | ---------------- |
E
ester.zhou 已提交
2039
| callback     | () => void           | Callback invoked when the window closes.|
E
ester.zhou 已提交
2040

2041 2042 2043 2044 2045 2046 2047
**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2048
    controller:WebController = new WebController()
2049 2050
    build() {
      Column() {
E
ester.zhou 已提交
2051 2052 2053
        Web({ src:'www.example.com', controller: this.controller })
        .onWindowExit(() => {
          console.log("onWindowExit...")
2054
        })
E
ester.zhou 已提交
2055
      }
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
    }
  }
  ```

## ConsoleMessage

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

### getLineNumber

getLineNumber(): number

Obtains the number of rows in this console message.

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

2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
| Type    | Description                  |
| ------ | -------------------- |
| number | Number of rows in the console message.|

### getMessage

getMessage(): string

Obtains the log information of this console message.

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

2084 2085
| Type    | Description                    |
| ------ | ---------------------- |
E
ester.zhou 已提交
2086
| string | Log information of the console message.|
2087 2088 2089 2090 2091 2092 2093 2094

### getMessageLevel

getMessageLevel(): MessageLevel

Obtains the level of this console message.

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

2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
| 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**
E
ester.zhou 已提交
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
| 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 details about 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>

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 已提交
2135

2136 2137 2138 2139
| Name   | Type  | Mandatory  | Default Value | Description       |
| ------ | ------ | ---- | ---- | ----------- |
| result | string | Yes   | -    | User input in the dialog box.|

E
ester.zhou 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
## FullScreenExitHandler<sup>9+</sup>

Defines a **FullScreenExitHandler** for listening for exiting full screen mode. For the sample code, see [onFullScreenEnter](#onfullscreenenter9).

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

exitFullScreen(): void

Exits full screen mode.

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

Defines a **WebController** for new web components. For the sample code, see [onWindowNew](#onwindownew9).

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

setWebController(controller: WebController): void

Sets a **WebController** object.

**Parameters**

| Name   | Type  | Mandatory  | Default Value | Description       |
| ------ | ------ | ---- | ---- | ----------- |
| controller | WebController | Yes   | -    | **WebController** object to set.|

2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
## WebResourceError

Implements the **WebResourceError** object. For details about the sample code, see [onErrorReceive](#onerrorreceive).

### getErrorCode

getErrorCode(): number

Obtains the error code for resource loading.

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

2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
| Type    | Description         |
| ------ | ----------- |
| number | Error code for resource loading.|

### getErrorInfo

getErrorInfo(): string

Obtains error information about resource loading.

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

2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
| Type    | Description          |
| ------ | ------------ |
| string | Error information about resource loading.|

## WebResourceRequest

Implements the **WebResourceRequest** object. For details about the sample code, see [onErrorReceive](#onerrorreceive).

### getRequestHeader

getResponseHeader() : Array\<Header\>

Obtains the information about the resource request header.

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

2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
| 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 已提交
2217

2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
| 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 已提交
2229

2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
| 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 已提交
2241

2242 2243
| Type     | Description              |
| ------- | ---------------- |
E
ester.zhou 已提交
2244
| boolean | Whether the resource request is redirected by the server.|
2245 2246 2247 2248 2249 2250 2251 2252

### isRequestGesture

isRequestGesture(): boolean

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

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

2254 2255
| Type     | Description                  |
| ------- | -------------------- |
E
ester.zhou 已提交
2256
| boolean | Whether the resource request is associated with a gesture (for example, a tap).|
2257 2258

## Header
E
ester.zhou 已提交
2259

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

2262 2263 2264 2265
| Name         | Type    | Description           |
| ----------- | ------ | ------------- |
| headerKey   | string | Key of the request/response header.  |
| headerValue | string | Value of the request/response header.|
E
ester.zhou 已提交
2266 2267


2268
## WebResourceResponse
E
ester.zhou 已提交
2269

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

2272
### getReasonMessage
E
ester.zhou 已提交
2273

2274
getReasonMessage(): string
E
ester.zhou 已提交
2275

2276
Obtains the status code description of the resource response.
E
ester.zhou 已提交
2277

2278
**Return value**
E
ester.zhou 已提交
2279

2280 2281 2282
| Type    | Description           |
| ------ | ------------- |
| string | Status code description of the resource response.|
E
ester.zhou 已提交
2283

2284
### getResponseCode
E
ester.zhou 已提交
2285

2286
getResponseCode(): number
E
ester.zhou 已提交
2287

2288
Obtains the status code of the resource response.
E
ester.zhou 已提交
2289

2290
**Return value**
E
ester.zhou 已提交
2291

2292 2293 2294
| Type    | Description         |
| ------ | ----------- |
| number | Status code of the resource response.|
E
ester.zhou 已提交
2295

2296
### getResponseData
E
ester.zhou 已提交
2297

2298
getResponseData(): string
E
ester.zhou 已提交
2299

2300
Obtains the data in the resource response.
E
ester.zhou 已提交
2301

2302
**Return value**
E
ester.zhou 已提交
2303

2304 2305 2306
| Type    | Description       |
| ------ | --------- |
| string | Data in the resource response.|
E
ester.zhou 已提交
2307

2308
### getResponseEncoding
E
ester.zhou 已提交
2309

2310 2311 2312 2313 2314
getResponseEncoding(): string

Obtains the encoding string of the resource response.

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

2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
| Type    | Description        |
| ------ | ---------- |
| string | Encoding string of the resource response.|

### getResponseHeader

getResponseHeader() : Array\<Header\>

Obtains the resource response header.

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

2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
| Type                        | Description      |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | Resource response header.|

### getResponseMimeType

getResponseMimeType(): string

Obtains the MIME type of the resource response.

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

2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
| Type    | Description                |
| ------ | ------------------ |
| string | MIME type of the resource response.|

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

setResponseData(data: string)

Sets the data in the resource response.

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

2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
| Name | Type  | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | ----------- |
| data | string | Yes   | -    | Resource response data to set.|

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

setResponseEncoding(encoding: string)

Sets the encoding string of the resource response.

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

2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
| 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 已提交
2375

2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
| 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 已提交
2387

2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
| 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 已提交
2399

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
| 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 已提交
2411

2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
| Name | Type  | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ------------- |
| code | number | Yes   | -    | Status code to set.|

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

Notifies the **\<Web>** component of the file selection result. For details about the sample code, see [onShowFileSelector](#onshowfileselector9).

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

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

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

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

2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
| Name     | Type           | Mandatory  | Default Value | Description        |
| -------- | --------------- | ---- | ---- | ------------ |
| fileList | Array\<string\> | Yes   | -    | List of files to operate.|

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

Implements the **FileSelectorParam** object. For details about the sample code, see [onShowFileSelector](#onshowfileselector9).

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

getTitle(): string

Obtains the title of the file selector.

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

2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
| 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 已提交
2455

2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode)| Mode of the file selector.|

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

getAcceptType(): Array\<string\>

Obtains the file filtering type.

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

2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
| Type             | Description       |
| --------------- | --------- |
| Array\<string\> | File filtering type.|

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

isCapture(): boolean

Checks whether multimedia capabilities are invoked.

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

2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
| Type     | Description          |
| ------- | ------------ |
| boolean | Whether multimedia capabilities are invoked.|

## HttpAuthHandler<sup>9+</sup>

Implements the **HttpAuthHandler** object. For details about the sample code, see [onHttpAuthRequest](#onhttpauthrequest9).

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

cancel(): void

Cancels HTTP authentication as requested by the user.

### confirm<sup>9+</sup>

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

Performs HTTP authentication with the user name and password provided by the user.

**Parameters**

| Name     | Type  | Mandatory  | Default Value | Description      |
| -------- | ------ | ---- | ---- | ---------- |
| userName | string | Yes   | -    | HTTP authentication user name.|
| pwd      | string | Yes   | -    | HTTP authentication password. |

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

2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|

### isHttpAuthInfoSaved<sup>9+</sup>

isHttpAuthInfoSaved(): boolean

Uses the password cached on the server for authentication.

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

2521 2522 2523 2524
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|

E
ester.zhou 已提交
2525
## SslErrorHandler<sup>9+</sup>
2526

E
ester.zhou 已提交
2527
Implements an **SslErrorHandler** object. For details about the sample code, see [onSslErrorEventReceive Event](#onsslerroreventreceive9).
2528

E
ester.zhou 已提交
2529
### handleCancel<sup>9+</sup>
2530

E
ester.zhou 已提交
2531
handleCancel(): void
2532

E
ester.zhou 已提交
2533
Cancels this request.
2534

E
ester.zhou 已提交
2535
### handleConfirm<sup>9+</sup>
2536

E
ester.zhou 已提交
2537
handleConfirm(): void
2538

E
ester.zhou 已提交
2539
Continues using the SSL certificate.
E
ester.zhou 已提交
2540 2541 2542

## ClientAuthenticationHandler<sup>9+</sup>

E
ester.zhou 已提交
2543
Defines a **ClientAuthenticationHandler** object returned by the **\<Web>** component. For details about the sample code, see [onClientAuthenticationRequest](#onclientauthenticationrequest9).
E
ester.zhou 已提交
2544 2545 2546 2547 2548

### confirm<sup>9+</sup>

confirm(priKeyFile : string, certChainFile : string): void

E
ester.zhou 已提交
2549
Uses the specified private key and client certificate chain.
E
ester.zhou 已提交
2550 2551 2552 2553 2554

**Parameters**

| Name        | Type| Mandatory  | Description       |
| --------      | ------   | ----  | ----------     |
E
ester.zhou 已提交
2555 2556
| 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 已提交
2557 2558 2559 2560 2561

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

cancel(): void

E
ester.zhou 已提交
2562
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.
E
ester.zhou 已提交
2563 2564 2565 2566 2567

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

ignore(): void

E
ester.zhou 已提交
2568
Ignores this request.
E
ester.zhou 已提交
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584

## PermissionRequest<sup>9+</sup>

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

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

deny(): void

Denies the permission requested by the web page.

### getOrigin<sup>9+</sup>

getOrigin(): string

Obtains the origin of this web page.
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614

**Return value**

| Type     | Description                   |
| ------- | --------------------- |
| string  | Origin of the web page that requests the permission.|

### getAccessibleResource<sup>9+</sup>

getAccessibleResource(): Array\<string\>

Obtains the list of accessible resources requested for the web page. For details about the resource types, see [ProtectedResourceType](#protectedresourcetype9).

**Return value**

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

### grant<sup>9+</sup>

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

Grants the permission for resources requested by the web page.

**Parameters**

| Name    | Type       | Mandatory| Default Value| Description               |
| --------- | --------------- | ---- | ----- | ---------------------- |
| resources | Array\<string\> | Yes  | -     | List of accessible resources requested by the web page.|
E
ester.zhou 已提交
2615

E
ester.zhou 已提交
2616 2617 2618 2619 2620 2621 2622 2623
## WebContextMenuParam<sup>9+</sup>

Provides the information about the context menu that is displayed when a page element is long pressed. For details about the sample code, see [onContextMenuShow](#oncontextmenushow9).

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

x(): number

E
ester.zhou 已提交
2624
Obtains the X coordinate of the context menu.
E
ester.zhou 已提交
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635

**Return value**

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

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

y(): number

E
ester.zhou 已提交
2636
Obtains the Y coordinate of the context menu.
E
ester.zhou 已提交
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653

**Return value**

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

### getLinkUrl<sup>9+</sup>

getLinkUrl(): string

Obtains the URL of the destination link.

**Return value**

| Type           | Description                    |
| --------------- | ----------------------- |
E
ester.zhou 已提交
2654
| string | If it is a link that is being long pressed, the URL that has passed the security check is returned.|
E
ester.zhou 已提交
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

### getUnfilterendLinkUrl<sup>9+</sup>

getUnfilterendLinkUrl(): string

Obtains the URL of the destination link.

**Return value**

| Type           | Description                    |
| --------------- | ----------------------- |
E
ester.zhou 已提交
2666
| string | If it is a link that is being long pressed, the original URL is returned.|
E
ester.zhou 已提交
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 2696 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

### getSourceUrl<sup>9+</sup>

getSourceUrl(): string

Obtain the source URL.

**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.

**Return value**

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

## WebContextMenuResult<sup>9+</sup>

Defines the response event executed when a context menu is displayed. For details about the sample code, see [onContextMenuShow](#oncontextmenushow9).

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

closeContextMenu(): void

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

### copyImage<sup>9+</sup>

copyImage(): void

Copies the image specified in **WebContextMenuParam**.

## JsGeolocation

Implements the **PermissionRequest** object. For details about the sample code, see [onGeolocationShow Event](#ongeolocationshow).

### invoke

invoke(origin: string, allow: boolean, retain: boolean): void

Sets the geolocation permission status of a web page.

**Parameters**

| Name    | Type| Mandatory| Default Value| Description              |
| --------- | ------- | ---- | ----- | ---------------------- |
E
ester.zhou 已提交
2722
| origin    | string  | Yes  | -     | Index of the origin.    |
E
ester.zhou 已提交
2723 2724 2725
| allow     | boolean | Yes  | -     | Geolocation permission status.|
| retain    | boolean | Yes  | -     | Whether the geolocation permission status can be saved to the system. The **[GeolocationPermissions](#geolocationpermissions9)** API can be used to manage the geolocation permission status saved to the system.|

E
ester.zhou 已提交
2726 2727
## WebController

E
ester.zhou 已提交
2728
Defines 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.
E
ester.zhou 已提交
2729 2730 2731 2732 2733 2734 2735

### Creating an Object

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

2736 2737 2738 2739 2740 2741 2742
### requestFocus

requestFocus()

Requests focus for this web page.

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

2744 2745 2746 2747 2748
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2749
    controller: WebController = new WebController()
2750 2751 2752 2753 2754
  
    build() {
      Column() {
        Button('requestFocus')
          .onClick(() => {
E
ester.zhou 已提交
2755
            this.controller.requestFocus()
2756 2757 2758 2759 2760 2761 2762
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
2763 2764 2765 2766 2767 2768
### accessBackward

accessBackward(): boolean

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

2769
**Return value**
E
ester.zhou 已提交
2770

2771 2772 2773 2774 2775
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if going to the previous page can be performed on the current page; returns **false** otherwise.|

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

2777 2778 2779 2780 2781
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2782
    controller: WebController = new WebController()
2783 2784 2785 2786 2787
  
    build() {
      Column() {
        Button('accessBackward')
          .onClick(() => {
E
ester.zhou 已提交
2788 2789
            let result = this.controller.accessBackward()
            console.log('result:' + result)
2790 2791 2792 2793 2794 2795 2796
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
2797 2798 2799 2800 2801 2802
### accessForward

accessForward(): boolean

Checks whether going to the next page can be performed on the current page.

2803
**Return value**
E
ester.zhou 已提交
2804

2805 2806 2807 2808 2809
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if going to the next page can be performed on the current page; returns **false** otherwise.|

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

2811 2812 2813 2814 2815
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2816
    controller: WebController = new WebController()
2817 2818 2819 2820 2821
  
    build() {
      Column() {
        Button('accessForward')
          .onClick(() => {
E
ester.zhou 已提交
2822 2823
            let result = this.controller.accessForward()
            console.log('result:' + result)
2824 2825 2826 2827 2828 2829 2830
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
2831 2832 2833 2834
### accessStep

accessStep(step: number): boolean

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

2837 2838 2839 2840
**Parameters**

| Name | Type  | Mandatory  | Default Value | Description                 |
| ---- | ------ | ---- | ---- | --------------------- |
E
ester.zhou 已提交
2841
| step | number | Yes   | -    | Number of the steps to take. A positive number means to go forward, and a negative number means to go backward.|
2842 2843

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

2845 2846
| Type     | Description       |
| ------- | --------- |
E
ester.zhou 已提交
2847
| boolean | Whether going forward or backward from the current page is successful.|
2848 2849

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

2851 2852 2853 2854 2855
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2856 2857
    controller: WebController = new WebController()
    @State steps: number = 2
2858 2859 2860 2861 2862
  
    build() {
      Column() {
        Button('accessStep')
          .onClick(() => {
E
ester.zhou 已提交
2863 2864
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
2865 2866 2867 2868 2869 2870
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
2871 2872 2873 2874 2875 2876 2877

### backward

backward(): void

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

2878
**Example**
E
ester.zhou 已提交
2879

2880 2881 2882 2883 2884
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2885
    controller: WebController = new WebController()
2886 2887 2888 2889 2890
  
    build() {
      Column() {
        Button('backward')
          .onClick(() => {
E
ester.zhou 已提交
2891
            this.controller.backward()
2892 2893 2894 2895 2896 2897
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
2898 2899 2900 2901 2902 2903 2904

### forward

forward(): void

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

2905
**Example**
E
ester.zhou 已提交
2906

2907 2908 2909 2910 2911
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2912
    controller: WebController = new WebController()
2913 2914 2915 2916 2917
  
    build() {
      Column() {
        Button('forward')
          .onClick(() => {
E
ester.zhou 已提交
2918
            this.controller.forward()
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### backOrForward<sup>9+</sup>

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 已提交
2933

2934 2935 2936 2937 2938
| Name | Type  | Mandatory  | Default Value | Description       |
| ---- | ------ | ---- | ---- | ----------- |
| step | number | Yes   | -    | Number of the steps to take.|

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

2940 2941 2942 2943 2944
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2945 2946
    controller: WebController = new WebController()
    @State step: number = -2
E
ester.zhou 已提交
2947
  
2948 2949 2950
    build() {
      Column() {
        Button('backOrForward')
E
ester.zhou 已提交
2951
          .onClick(() => {
E
ester.zhou 已提交
2952
            this.controller.backOrForward(this.step)
E
ester.zhou 已提交
2953 2954 2955
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966
    }
  }
  ```

### deleteJavaScriptRegister

deleteJavaScriptRegister(name: string)

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.

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

2968 2969 2970 2971 2972
| 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.|

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

2974 2975 2976 2977 2978
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2979 2980
    controller: WebController = new WebController()
    @State name: string = 'Object'
2981 2982 2983 2984 2985
  
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
          .onClick(() => {
E
ester.zhou 已提交
2986
            this.controller.deleteJavaScriptRegister(this.name)
2987 2988 2989 2990 2991 2992 2993
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
2994 2995 2996 2997 2998 2999
### getHitTest

getHitTest(): HitTestType

Obtains the element type of the area being clicked.	

3000
**Return value**
E
ester.zhou 已提交
3001

3002 3003 3004 3005 3006
| Type                             | Description         |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype)| Element type of the area being clicked.|

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

3008 3009 3010 3011 3012
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3013
    controller: WebController = new WebController()
3014 3015 3016 3017 3018
  
    build() {
      Column() {
        Button('getHitTest')
          .onClick(() => {
E
ester.zhou 已提交
3019 3020
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getHitTestValue<sup>9+</sup>
getHitTestValue(): HitTestValue

Obtains the element information of the area being clicked.

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

3035 3036 3037 3038 3039
| Type                            | Description        |
| ------------------------------ | ---------- |
| [HitTestValue](#hittestvalue9) | Element information of the area being clicked.|

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

3041 3042 3043 3044 3045
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3046
    controller: WebController = new WebController()
3047 3048 3049 3050 3051
  
    build() {
      Column() {
        Button('getHitTestValue')
          .onClick(() => {
E
ester.zhou 已提交
3052 3053 3054
            let hitValue = this.controller.getHitTestValue()
            console.log("hitType: " + hitValue.getType())
            console.log("extra: " + hitValue.getExtra())
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getWebId<sup>9+</sup>
getWebId(): number

Obtains the index value of this **\<Web>** component, which can be used for **\<Web>** component management.

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

3069 3070 3071 3072 3073
| Type    | Description          |
| ------ | ------------ |
| number | Index value of the current **\<Web>** component.|

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

3075 3076 3077 3078 3079
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3080
    controller: WebController = new WebController()
3081 3082 3083 3084 3085
  
    build() {
      Column() {
        Button('getWebId')
          .onClick(() => {
E
ester.zhou 已提交
3086 3087
            let id = this.controller.getWebId()
            console.log("id: " + id)
3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

Obtains the title of the current web page.

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

3102 3103 3104 3105 3106
| Type    | Description      |
| ------ | -------- |
| string | Title of the current web page.|

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

3108 3109 3110 3111 3112
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3113
    controller: WebController = new WebController()
3114 3115 3116 3117 3118
  
    build() {
      Column() {
        Button('getTitle')
          .onClick(() => {
E
ester.zhou 已提交
3119 3120
            let title = this.controller.getTitle()
            console.log("title: " + title)
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getPageHeight<sup>9+</sup>
getPageHeight(): number

Obtains the height of the current web page.

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

3135 3136 3137 3138 3139
| Type    | Description        |
| ------ | ---------- |
| number | Height of the current web page.|

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

3141 3142 3143 3144 3145
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3146
    controller: WebController = new WebController()
3147 3148 3149 3150 3151
  
    build() {
      Column() {
        Button('getPageHeight')
          .onClick(() => {
E
ester.zhou 已提交
3152 3153
            let pageHeight = this.controller.getPageHeight()
            console.log("pageHeight: " + pageHeight)
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

Obtains the default user agent of the current web page.

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

3168 3169 3170 3171 3172
| Type    | Description     |
| ------ | ------- |
| string | Default user agent.|

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

3174 3175 3176 3177 3178
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3179
    controller: WebController = new WebController()
3180 3181 3182 3183 3184
  
    build() {
      Column() {
        Button('getDefaultUserAgent')
          .onClick(() => {
E
ester.zhou 已提交
3185 3186
            let userAgent = this.controller.getDefaultUserAgent()
            console.log("userAgent: " + userAgent)
3187 3188 3189 3190 3191 3192
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
3193 3194 3195

### loadData

3196
loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string })
E
ester.zhou 已提交
3197 3198 3199

Loads data. If **baseUrl** is empty, the specified character string will be loaded using the data protocol.

E
ester.zhou 已提交
3200
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 已提交
3201

E
ester.zhou 已提交
3202
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 已提交
3203

3204
**Parameters**
E
ester.zhou 已提交
3205

3206 3207 3208 3209 3210 3211 3212 3213 3214
| 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.|

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

3216 3217 3218 3219 3220
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3221
    controller: WebController = new WebController()
3222 3223 3224 3225 3226 3227 3228 3229 3230
  
    build() {
      Column() {
        Button('loadData')
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
E
ester.zhou 已提交
3231
            })
3232 3233 3234 3235 3236 3237
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
3238 3239 3240

### loadUrl

3241
loadUrl(options: { url: string | Resource, headers?: Array\<Header\> })
E
ester.zhou 已提交
3242 3243 3244 3245 3246

Loads a URL using the specified HTTP header.

The object injected through **loadUrl** is valid only in the current document. It will be invalid on a new page navigated to through **loadUrl**.

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

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

3251 3252 3253 3254 3255 3256
| Name    | Type                      | Mandatory  | Default Value | Description          |
| ------- | -------------------------- | ---- | ---- | -------------- |
| url     | string                     | Yes   | -    | URL to load.    |
| headers | Array\<[Header](#header)\> | No   | []   | Additional HTTP request header of the URL.|

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

3258 3259 3260 3261 3262
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3263
    controller: WebController = new WebController()
3264 3265 3266 3267 3268
  
    build() {
      Column() {
        Button('loadUrl')
          .onClick(() => {
E
ester.zhou 已提交
3269
            this.controller.loadUrl({ url: 'www.example.com' })
3270 3271 3272 3273 3274 3275
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
3276 3277 3278 3279 3280

### onActive

onActive(): void

E
ester.zhou 已提交
3281
Invoked when the **\<Web>** component enters the active state.
E
ester.zhou 已提交
3282

3283
**Example**
E
ester.zhou 已提交
3284

3285 3286 3287 3288 3289
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3290
    controller: WebController = new WebController()
3291 3292 3293 3294 3295
  
    build() {
      Column() {
        Button('onActive')
          .onClick(() => {
E
ester.zhou 已提交
3296
            this.controller.onActive()
3297 3298 3299 3300 3301 3302 3303
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
3304 3305 3306 3307
### onInactive

onInactive(): void

E
ester.zhou 已提交
3308
Invoked when the **\<Web>** component enters the inactive state.
E
ester.zhou 已提交
3309

3310
**Example**
E
ester.zhou 已提交
3311

3312 3313 3314 3315 3316
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3317
    controller: WebController = new WebController()
3318 3319 3320 3321 3322
  
    build() {
      Column() {
        Button('onInactive')
          .onClick(() => {
E
ester.zhou 已提交
3323
            this.controller.onInactive()
3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### zoom
zoom(factor: number): void

Sets a zoom factor for the current web page.

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

3338 3339 3340 3341 3342
| Name   | Type  | Mandatory  | Description                          |
| ------ | ------ | ---- | ------------------------------ |
| factor | number | Yes   | Zoom factor to set. A positive value indicates zoom-in, and a negative value indicates zoom-out.|

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

3344 3345 3346 3347 3348
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3349 3350
    controller: WebController = new WebController()
    @State factor: number = 1
3351 3352 3353 3354 3355
  
    build() {
      Column() {
        Button('zoom')
          .onClick(() => {
E
ester.zhou 已提交
3356
            this.controller.zoom(this.factor)
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### zoomIn<sup>9+</sup>
zoomIn(): boolean

Zooms in on the current web page by 20%.

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

3371 3372 3373 3374 3375
| Type     | Description         |
| ------- | ----------- |
| boolean | Operation result.|

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

3377 3378 3379 3380 3381
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3382
    controller: WebController = new WebController()
3383 3384 3385 3386 3387
  
    build() {
      Column() {
        Button('zoomIn')
          .onClick(() => {
E
ester.zhou 已提交
3388 3389
            let result = this.controller.zoomIn()
            console.log("result: " + result)
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### zoomOut<sup>9+</sup>
zoomOut(): boolean

E
ester.zhou 已提交
3400
Zooms out of the current web page by 20%.
3401 3402

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

3404 3405 3406 3407 3408
| Type     | Description         |
| ------- | ----------- |
| boolean | Operation result.|

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

3410 3411 3412 3413 3414
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3415
    controller: WebController = new WebController()
3416 3417 3418 3419 3420
  
    build() {
      Column() {
        Button('zoomOut')
          .onClick(() => {
E
ester.zhou 已提交
3421 3422
            let result = this.controller.zoomOut()
            console.log("result: " + result)
3423 3424 3425 3426 3427 3428 3429
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
3430 3431
### refresh

3432
refresh()
E
ester.zhou 已提交
3433

E
ester.zhou 已提交
3434
Invoked when the **\<Web>** component refreshes the web page.
E
ester.zhou 已提交
3435

3436
**Example**
E
ester.zhou 已提交
3437

3438 3439 3440 3441 3442
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3443
    controller: WebController = new WebController()
3444 3445 3446 3447 3448
  
    build() {
      Column() {
        Button('refresh')
          .onClick(() => {
E
ester.zhou 已提交
3449
            this.controller.refresh()
3450 3451 3452 3453 3454 3455
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
3456

3457
### registerJavaScriptProxy
E
ester.zhou 已提交
3458

3459 3460 3461 3462 3463
registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })

Registers a JavaScript object and invokes the methods of the object in the window. You must invoke the [refresh](#refresh) API for the registration to take effect.

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

3465 3466
| Name       | Type           | Mandatory  | Default Value | Description                                    |
| ---------- | --------------- | ---- | ---- | ---------------------------------------- |
E
ester.zhou 已提交
3467
| 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.|
3468 3469 3470 3471
| 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.                |

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

3473 3474 3475 3476 3477 3478 3479 3480
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
E
ester.zhou 已提交
3481
        return "ArkUI Web Component"
3482 3483
      },
      toString: () => {
E
ester.zhou 已提交
3484
        console.log('Web Component toString')
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
E
ester.zhou 已提交
3495
            })
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```

  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
E
ester.zhou 已提交
3515 3516
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
3517 3518 3519 3520 3521
      }
  </script>
  </html>
  
  ```
E
ester.zhou 已提交
3522 3523 3524

### runJavaScript

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

E
ester.zhou 已提交
3527
Asynchronously executes a JavaScript script. This API uses a callback to return the script execution result. **runJavaScript** can be invoked only after **loadUrl** is executed. For example, it can be executed in **onPageEnd**.
E
ester.zhou 已提交
3528

3529
**Parameters**
E
ester.zhou 已提交
3530

3531 3532 3533 3534 3535 3536
| 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.|

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

3538 3539 3540 3541 3542
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3543
    controller: WebController = new WebController()
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555
    @State webResult: string = ''
    build() {
      Column() {
        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}`)
E
ester.zhou 已提交
3556 3557
            }})
          console.info('url: ', e.url)
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
        })
      }
    }
  }
  ```

  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
E
ester.zhou 已提交
3574
        console.log('Ark WebComponent')
3575 3576 3577 3578
        return "This value is from index.html"
    }
    </script>
  </html>
E
ester.zhou 已提交
3579

3580
  ```
E
ester.zhou 已提交
3581 3582 3583

### stop

3584
stop()
E
ester.zhou 已提交
3585 3586 3587

Stops page loading.

3588
**Example**
E
ester.zhou 已提交
3589

3590 3591 3592 3593 3594
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3595
    controller: WebController = new WebController()
3596 3597 3598 3599 3600
  
    build() {
      Column() {
        Button('stop')
          .onClick(() => {
E
ester.zhou 已提交
3601
            this.controller.stop()
3602 3603 3604 3605 3606 3607 3608
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
3609 3610 3611 3612 3613 3614
### clearHistory

clearHistory(): void

Clears the browsing history.

3615
**Example**
E
ester.zhou 已提交
3616

3617 3618 3619 3620 3621
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3622
    controller: WebController = new WebController()
3623 3624 3625 3626 3627
  
    build() {
      Column() {
        Button('clearHistory')
          .onClick(() => {
E
ester.zhou 已提交
3628
            this.controller.clearHistory()
3629 3630 3631 3632 3633 3634 3635
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
3636 3637 3638 3639
### clearSslCache

clearSslCache(): void

E
ester.zhou 已提交
3640
Clears the user operation corresponding to the SSL certificate error event recorded by the **\<Web>** component.
E
ester.zhou 已提交
3641 3642 3643 3644 3645 3646 3647 3648

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3649
    controller: WebController = new WebController()
E
ester.zhou 已提交
3650 3651 3652 3653 3654

    build() {
      Column() {
        Button('clearSslCache')
          .onClick(() => {
E
ester.zhou 已提交
3655
            this.controller.clearSslCache()
E
ester.zhou 已提交
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### clearClientAuthenticationCache

clearClientAuthenticationCache(): void

E
ester.zhou 已提交
3667
Clears the user operation corresponding to the client certificate request event recorded by the \<Web> component.
E
ester.zhou 已提交
3668 3669 3670 3671 3672 3673 3674 3675

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3676
    controller: WebController = new WebController()
E
ester.zhou 已提交
3677 3678 3679 3680 3681

    build() {
      Column() {
        Button('clearClientAuthenticationCache')
          .onClick(() => {
E
ester.zhou 已提交
3682
            this.controller.clearClientAuthenticationCache()
E
ester.zhou 已提交
3683 3684 3685 3686 3687 3688 3689
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

3690
### getCookieManager<sup>9+</sup>
E
ester.zhou 已提交
3691 3692 3693 3694

getCookieManager(): WebCookie

Obtains the cookie management object of the **\<Web>** component.
3695 3696

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

3698 3699 3700 3701 3702
| Type       | Description                                      |
| --------- | ---------------------------------------- |
| WebCookie | Cookie management object. For details, see [WebCookie](#webcookie).|

**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 3713 3714
  
    build() {
      Column() {
        Button('getCookieManager')
          .onClick(() => {
E
ester.zhou 已提交
3715
            let cookieManager = this.controller.getCookieManager()
3716 3717 3718 3719 3720 3721 3722
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
3723
### createWebMessagePorts<sup>9+</sup>
3724

E
ester.zhou 已提交
3725
createWebMessagePorts(): Array\<WebMessagePort\>
3726

E
ester.zhou 已提交
3727
Creates web message ports.
3728 3729 3730 3731

**Return value**


E
ester.zhou 已提交
3732 3733
| Type                             | Description           |
| ------------------------------- | ------------- |
E
ester.zhou 已提交
3734
| Array\<[WebMessagePort](#webmessageport9)\> | List of web message ports.|
3735

E
ester.zhou 已提交
3736
**Example**
3737

E
ester.zhou 已提交
3738 3739 3740 3741 3742
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3743 3744
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
3745 3746 3747 3748
    build() {
      Column() {
        Button('createWebMessagePorts')
          .onClick(() => {
E
ester.zhou 已提交
3749
            this.ports = this.controller.createWebMessagePorts()
E
ester.zhou 已提交
3750 3751 3752 3753 3754 3755 3756
            console.log("createWebMessagePorts size:" + this.ports.length)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
3757

E
ester.zhou 已提交
3758
### postMessage<sup>9+</sup>
3759

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

E
ester.zhou 已提交
3762
Sends a web message to an HTML5 window.
3763 3764 3765

**Parameters**

E
ester.zhou 已提交
3766 3767
| Name       | Type           | Mandatory  | Default Value | Description                     |
| ---------- | --------------- | ---- | ---- | ------------------------- |
E
ester.zhou 已提交
3768 3769
| message     | [WebMessageEvent](#webmessageevent9)          | Yes   | -    |Message to send, including the data and message port.|
| uri       | string          | Yes   | -    | URI for receiving the message.|
3770 3771

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

3773
  ```ts
E
ester.zhou 已提交
3774
  // index.ets
3775 3776 3777
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3778 3779 3780 3781 3782
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
    @State sendFromEts: string = 'Send this message from ets to HTML'
    @State receivedFromHtml: string = 'Display received message send from HTML'

3783 3784
    build() {
      Column() {
E
ester.zhou 已提交
3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
        // 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')
3795
          .onClick(() => {
E
ester.zhou 已提交
3796 3797
            this.ports = this.controller.createWebMessagePorts()
            console.log("createWebMessagePorts size:" + this.ports.length)
3798
          })
E
ester.zhou 已提交
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828

        // 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')
          .onClick(() => {
            var msg = new WebMessageEvent()
            msg.setData(this.sendFromEts)
            this.ports[0].postMessageEvent(msg)
          })
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .javaScriptAccess(true)
          .fileAccess(true)
3829 3830 3831
      }
    }
  }
E
ester.zhou 已提交
3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847

  // 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>

  // index.js
E
ester.zhou 已提交
3848
  var h5Port;
E
ester.zhou 已提交
3849 3850 3851
  var output = document.querySelector('.output');
  window.addEventListener('message', function(event) {
    if (event.data == '__init_port__') {
E
ester.zhou 已提交
3852
      if(event.ports[0] != null) {
E
ester.zhou 已提交
3853
        h5Port = event.ports[0]; // 1. Save the port number sent from the eTS side.
E
ester.zhou 已提交
3854
        h5Port.onmessage = function(event) {
E
ester.zhou 已提交
3855 3856 3857
          // 2. Receive the message sent from the eTS side.
          var msg = 'Got message from ets:' + event.data;
          output.innerHTML = msg;
E
ester.zhou 已提交
3858 3859 3860 3861
        }
      }
    }
  })
E
ester.zhou 已提交
3862 3863 3864 3865 3866

  // 3. Use h5Port to send messages to the eTS side.
  function PostMsgToEts(data) {
    h5Port.postMessage(data)
  }
3867
  ```
E
ester.zhou 已提交
3868

E
ester.zhou 已提交
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887
### getUrl<sup>9+</sup>

getUrl(): string

Obtains the URL of the current page.

**Return value**

| Type                             | Description           |
| ------------------------------- | ------------- |
| string | URL of the current page.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3888
    controller: WebController = new WebController()
E
ester.zhou 已提交
3889 3890 3891 3892
    build() {
      Column() {
        Button('getUrl')
          .onClick(() => {
E
ester.zhou 已提交
3893
            console.log("url: " + this.controller.getUrl())
E
ester.zhou 已提交
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

## HitTestValue<sup>9+</sup>
Implements the **HitTestValue** object. For details about the sample code, see [getHitTestValue](#gethittestvalue9).

### getType<sup>9+</sup>
getType(): HitTestType

Obtains the element type of the area being clicked.

**Return value**

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

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

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.

**Return value**

| Type    | Description          |
| ------ | ------------ |
| string | Extra information of the area being clicked.|


## WebCookie

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.

### setCookie<sup>9+</sup>
setCookie(url: string, value: string): boolean

Sets the cookie. This API returns the result synchronously. Returns **true** if the operation is successful; returns **false** otherwise.

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | Yes   | -    | URL of the cookie to set.|
| value | string | Yes   | -    | Value of the cookie to set.        |

**Return value**

| Type     | Description           |
| ------- | ------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3956
    controller: WebController = new WebController()
E
ester.zhou 已提交
3957 3958 3959 3960 3961
  
    build() {
      Column() {
        Button('setCookie')
          .onClick(() => {
E
ester.zhou 已提交
3962 3963
            let result = this.controller.getCookieManager().setCookie("www.example.com", "a=b")
            console.log("result: " + result)
E
ester.zhou 已提交
3964 3965 3966 3967 3968 3969 3970 3971 3972
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### saveCookieSync<sup>9+</sup>
saveCookieSync(): boolean
E
ester.zhou 已提交
3973

E
ester.zhou 已提交
3974
Saves the cookies in the memory to the drive. This API returns the result synchronously.
3975 3976

**Return value**
E
ester.zhou 已提交
3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988

| Type     | Description                  |
| ------- | -------------------- |
| boolean | Operation result.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
3989
    controller: WebController = new WebController()
E
ester.zhou 已提交
3990 3991 3992 3993 3994
  
    build() {
      Column() {
        Button('saveCookieSync')
          .onClick(() => {
E
ester.zhou 已提交
3995 3996
            let result = this.controller.getCookieManager().saveCookieSync()
            console.log("result: " + result)
E
ester.zhou 已提交
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getCookie<sup>9+</sup>
getCookie(url: string): string

Obtains the cookie value corresponding to the specified URL.

**Parameters**

| 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.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4029
    controller: WebController = new WebController()
E
ester.zhou 已提交
4030 4031 4032 4033 4034
  
    build() {
      Column() {
        Button('getCookie')
          .onClick(() => {
E
ester.zhou 已提交
4035 4036
            let value = webview.WebCookieManager.getCookie('www.example.com')
            console.log("value: " + value)
E
ester.zhou 已提交
4037 4038 4039 4040 4041 4042 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 4068 4069
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### setCookie<sup>9+</sup>
setCookie(url: string, value: string): boolean

Sets a cookie value for the specified URL.

**Parameters**

| 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.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4070
    controller: WebController = new WebController()
E
ester.zhou 已提交
4071 4072 4073 4074 4075
  
    build() {
      Column() {
        Button('setCookie')
          .onClick(() => {
E
ester.zhou 已提交
4076 4077
            let result = web_webview.WebCookieManager.setCookie('www.example.com', 'a=b')
            console.log("result: " + result)
E
ester.zhou 已提交
4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### saveCookieSync<sup>9+</sup>
saveCookieSync(): boolean

E
ester.zhou 已提交
4088
Saves the cookies in the memory to the drive. This API returns the result synchronously.
E
ester.zhou 已提交
4089 4090 4091

**Return value**

4092 4093 4094 4095 4096
| Type     | Description                  |
| ------- | -------------------- |
| boolean | Operation result.|

**Example**
E
ester.zhou 已提交
4097 4098 4099 4100 4101 4102 4103

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4104
    controller: WebController = new WebController()
E
ester.zhou 已提交
4105 4106 4107 4108 4109
  
    build() {
      Column() {
        Button('saveCookieSync')
          .onClick(() => {
E
ester.zhou 已提交
4110 4111
            let result = web_webview.WebCookieManager.saveCookieSync()
            console.log("result: " + result)
E
ester.zhou 已提交
4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### saveCookieAsync<sup>9+</sup>
saveCookieAsync(): Promise\<boolean>

Saves cookies in the memory to the drive. This API uses a promise to return the value.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| Promise\<boolean> | Promise used to return the result.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4138
    controller: WebController = new WebController()
E
ester.zhou 已提交
4139 4140 4141 4142 4143 4144 4145
  
    build() {
      Column() {
        Button('saveCookieAsync')
          .onClick(() => {
            web_webview.WebCookieManager.saveCookieAsync()
              .then (function(result) {
E
ester.zhou 已提交
4146
                console.log("result: " + result)
E
ester.zhou 已提交
4147 4148
              })
              .catch(function(error) {
E
ester.zhou 已提交
4149 4150
                console.error("error: " + error)
              })
E
ester.zhou 已提交
4151 4152 4153 4154 4155 4156 4157 4158 4159 4160
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### saveCookieAsync<sup>9+</sup>
saveCookieAsync(callback: AsyncCallback\<boolean>): void

E
ester.zhou 已提交
4161
Saves cookies in the memory to the drive. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| callback   | AsyncCallback\<boolean> | Yes   | -    | Callback used to return the operation result.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4177
    controller: WebController = new WebController()
E
ester.zhou 已提交
4178 4179 4180 4181 4182 4183
  
    build() {
      Column() {
        Button('saveCookieAsync')
          .onClick(() => {
            web_webview.WebCookieManager.saveCookieAsync(function(result) {
E
ester.zhou 已提交
4184 4185
              console.log("result: " + result)
            })
E
ester.zhou 已提交
4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### isCookieAllowed<sup>9+</sup>
isCookieAllowed(): boolean

Checks whether the **WebCookieManager** instance has the permission to send and receive cookies.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| boolean | Whether the **WebCookieManager** instance has the permission to send and receive cookies.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4212
    controller: WebController = new WebController()
E
ester.zhou 已提交
4213 4214 4215 4216 4217
  
    build() {
      Column() {
        Button('isCookieAllowed')
          .onClick(() => {
E
ester.zhou 已提交
4218 4219
            let result = web_webview.WebCookieManager.isCookieAllowed()
            console.log("result: " + result)
E
ester.zhou 已提交
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### putAcceptCookieEnabled<sup>9+</sup>
putAcceptCookieEnabled(accept: boolean): void

Sets whether the **WebCookieManager** instance has the permission to send and receive cookies.

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| accept   | boolean | Yes   | -    | Whether the **WebCookieManager** instance has the permission to send and receive cookies.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4246
    controller: WebController = new WebController()
E
ester.zhou 已提交
4247 4248 4249 4250 4251
  
    build() {
      Column() {
        Button('putAcceptCookieEnabled')
          .onClick(() => {
E
ester.zhou 已提交
4252
            web_webview.WebCookieManager.putAcceptCookieEnabled(false)
E
ester.zhou 已提交
4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### isThirdPartyCookieAllowed<sup>9+</sup>
isThirdCookieAllowed(): boolean

Checks whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| boolean | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4279
    controller: WebController = new WebController()
E
ester.zhou 已提交
4280 4281 4282 4283 4284
  
    build() {
      Column() {
        Button('isThirdPartyCookieAllowed')
          .onClick(() => {
E
ester.zhou 已提交
4285 4286
            let result = web_webview.WebCookieManager.isThirdPartyCookieAllowed()
            console.log("result: " + result)
E
ester.zhou 已提交
4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### putAcceptThirdPartyCookieEnabled<sup>9+</sup>
putAcceptThirdPartyCookieEnabled(accept: boolean): void

Sets whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| accept   | boolean | Yes   | -    | Whether the **WebCookieManager** instance has the permission to send and receive third-party cookies.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4313
    controller: WebController = new WebController()
E
ester.zhou 已提交
4314 4315 4316 4317 4318
  
    build() {
      Column() {
        Button('putAcceptThirdPartyCookieEnabled')
          .onClick(() => {
E
ester.zhou 已提交
4319
            web_webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(false)
E
ester.zhou 已提交
4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### existCookie<sup>9+</sup>
existCookie(): boolean

Checks whether cookies exist.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| boolean | Whether cookies exist.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4346
    controller: WebController = new WebController()
E
ester.zhou 已提交
4347 4348 4349 4350 4351
  
    build() {
      Column() {
        Button('existCookie')
          .onClick(() => {
E
ester.zhou 已提交
4352 4353
            let result = web_webview.WebCookieManager.existCookie()
            console.log("result: " + result)
E
ester.zhou 已提交
4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### deleteEntireCookie<sup>9+</sup>
deleteEntireCookie(): void

Deletes all cookies.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4374
    controller: WebController = new WebController()
E
ester.zhou 已提交
4375 4376 4377 4378 4379
  
    build() {
      Column() {
        Button('deleteEntireCookie')
          .onClick(() => {
E
ester.zhou 已提交
4380
            web_webview.WebCookieManager.deleteEntireCookie()
E
ester.zhou 已提交
4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### deleteSessionCookie<sup>9+</sup>
deleteSessionCookie(): void

Deletes all session cookies.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4401
    controller: WebController = new WebController()
E
ester.zhou 已提交
4402 4403 4404 4405 4406
  
    build() {
      Column() {
        Button('deleteSessionCookie')
          .onClick(() => {
E
ester.zhou 已提交
4407
            webview.WebCookieManager.deleteSessionCookie()
E
ester.zhou 已提交
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

## WebDataBase<sup>9+</sup>
Implements the **WebDataBase** object.

### existHttpAuthCredentials<sup>9+</sup>

static existHttpAuthCredentials(): boolean

Checks whether any saved HTTP authentication credentials exist. This API is synchronous.

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
| boolean | Whether any saved HTTP authentication credentials exist. Returns **true** if any saved HTTP authentication credentials exist exists; returns **false** otherwise.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4438
    controller: WebController = new WebController()
E
ester.zhou 已提交
4439 4440 4441 4442 4443
  
    build() {
      Column() {
        Button('existHttpAuthCredentials')
          .onClick(() => {
E
ester.zhou 已提交
4444 4445
            let result = web_webview.WebDataBase.existHttpAuthCredentials()
            console.log('result: ' + result)
E
ester.zhou 已提交
4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### deleteHttpAuthCredentials<sup>9+</sup>

static deleteHttpAuthCredentials(): void

Deletes all HTTP authentication credentials saved in the cache. This API returns the result synchronously.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4467
    controller: WebController = new WebController()
E
ester.zhou 已提交
4468 4469 4470 4471 4472
  
    build() {
      Column() {
        Button('deleteHttpAuthCredentials')
          .onClick(() => {
E
ester.zhou 已提交
4473
            web_webview.WebDataBase.deleteHttpAuthCredentials()
E
ester.zhou 已提交
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### getHttpAuthCredentials<sup>9+</sup>

static getHttpAuthCredentials(host: string, realm: string): Array\<string\>

Retrieves HTTP authentication credentials for a given host and domain. This API is synchronous.

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description            |
| ----- | ------ | ---- | ---- | ---------------- |
| host  | string | Yes   | -    | Host for which you want to obtain the HTTP authentication credentials.|
| realm | string | Yes   | -    | Realm for which you want to obtain the HTTP authentication credentials. |

**Return value**

| Type             | Description                    |
| --------------- | ---------------------- |
| Array\<string\> | Returns the array of the matching user names and passwords if the operation is successful; returns an empty array otherwise.|

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4508 4509 4510 4511
    controller: WebController = new WebController()
    host: string = "www.spincast.org"
    realm: string = "protected example"
    username_password: string[]
E
ester.zhou 已提交
4512 4513 4514 4515
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
E
ester.zhou 已提交
4516 4517
            this.username_password = web_webview.WebDataBase.getHttpAuthCredentials(this.host, this.realm)
            console.log('num: ' + this.username_password.length)
E
ester.zhou 已提交
4518
            ForEach(this.username_password, (item) => {
E
ester.zhou 已提交
4519
              console.log('username_password: ' + item)
E
ester.zhou 已提交
4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550
            }, item => item)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### saveHttpAuthCredentials<sup>9+</sup>

static saveHttpAuthCredentials(host: string, realm: string, username: string, password: string): void

Saves HTTP authentication credentials for a given host and realm. This API returns the result synchronously.

**Parameters**

| Name     | Type  | Mandatory  | Default Value | Description            |
| -------- | ------ | ---- | ---- | ---------------- |
| host     | string | Yes   | -    | Host for which you want to obtain the HTTP authentication credentials.|
| realm    | string | Yes   | -    | Realm to which HTTP authentication credentials apply. |
| username | string | Yes   | -    | User name.            |
| password | string | Yes   | -    | Password.             |

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4551 4552 4553
    controller: WebController = new WebController()
    host: string = "www.spincast.org"
    realm: string = "protected example"
E
ester.zhou 已提交
4554 4555 4556 4557
    build() {
      Column() {
        Button('saveHttpAuthCredentials')
          .onClick(() => {
E
ester.zhou 已提交
4558
            web_webview.WebDataBase.saveHttpAuthCredentials(this.host, this.realm, "Stromgol", "Laroche")
E
ester.zhou 已提交
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

## GeolocationPermissions<sup>9+</sup>

Defines a **GeolocationPermissions** object.

### allowGeolocation<sup>9+</sup>

static allowGeolocation(origin: string): void

Allows the specified source to use the geolocation information.

**Parameters**

| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
E
ester.zhou 已提交
4580
| origin   | string   | Yes  | -     | Index of the origin.|
E
ester.zhou 已提交
4581 4582 4583 4584 4585

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
4586
  import web_webview from '@ohos.web.webview'
E
ester.zhou 已提交
4587 4588 4589
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4590 4591
    controller: WebController = new WebController()
    origin: string = "file:///"
E
ester.zhou 已提交
4592 4593 4594 4595
    build() {
      Column() {
        Button('allowGeolocation')
          .onClick(() => {
E
ester.zhou 已提交
4596
            web_webview.GeolocationPermissions.allowGeolocation(this.origin)
E
ester.zhou 已提交
4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### deleteGeolocation<sup>9+</sup>

static deleteGeolocation(origin: string): void

Clears the geolocation permission status of a specified source.

**Parameters**

| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
E
ester.zhou 已提交
4614
| origin   | string   | Yes  | -     | Index of the origin.|
E
ester.zhou 已提交
4615 4616 4617 4618 4619

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
4620
  import web_webview from '@ohos.web.webview'
E
ester.zhou 已提交
4621 4622 4623
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4624 4625
    controller: WebController = new WebController()
    origin: string = "file:///"
E
ester.zhou 已提交
4626 4627 4628 4629
    build() {
      Column() {
        Button('deleteGeolocation')
          .onClick(() => {
E
ester.zhou 已提交
4630
            web_webview.GeolocationPermissions.deleteGeolocation(this.origin)
E
ester.zhou 已提交
4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### deleteAllGeolocation<sup>9+</sup>

static deleteAllGeolocation(): void

Clears the geolocation permission status of all sources.

**Example**

4646 4647
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4648
  import web_webview from '@ohos.web.webview'
4649 4650 4651
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4652
    controller: WebController = new WebController()
4653 4654
    build() {
      Column() {
E
ester.zhou 已提交
4655
        Button('deleteAllGeolocation')
4656
          .onClick(() => {
E
ester.zhou 已提交
4657
            web_webview.GeolocationPermissions.deleteAllGeolocation()
4658 4659 4660 4661 4662 4663 4664
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
4665
### getAccessibleGeolocation<sup>9+</sup>
4666

E
ester.zhou 已提交
4667
static getAccessibleGeolocation(origin: string, callback: AsyncCallback\<boolean\>): void
4668

E
ester.zhou 已提交
4669
Obtains the geolocation permission status of the specified source. This API uses an asynchronous callback to return the result.
4670

E
ester.zhou 已提交
4671
**Parameters**
4672

E
ester.zhou 已提交
4673 4674 4675 4676
| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
| origin   | string   | Yes  | -     | Index of the origin.|
| callback | AsyncCallback\<boolean\> | Yes| - | Callback used to return the geolocation permission status of the specified source. If the operation is successful, the value **true** means that the geolocation permission is granted, and **false** means the opposite. If the operation fails, the geolocation permission status of the specified source is not found.|
4677 4678

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

4680 4681
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4682
  import web_webview from '@ohos.web.webview'
4683 4684 4685
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4686 4687
    controller: WebController = new WebController()
    origin: string = "file:///"
4688 4689
    build() {
      Column() {
E
ester.zhou 已提交
4690
        Button('getAccessibleGeolocationAsync')
4691
          .onClick(() => {
E
ester.zhou 已提交
4692 4693
            web_webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => {
              if (error) {
E
ester.zhou 已提交
4694 4695
                console.log('getAccessibleGeolocationAsync error: ' + JSON.stringify(error))
                return
E
ester.zhou 已提交
4696
              }
E
ester.zhou 已提交
4697 4698
              console.log('getAccessibleGeolocationAsync result: ' + result)
            })
4699 4700 4701 4702 4703 4704 4705
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
4706
### getAccessibleGeolocation<sup>9+</sup>
4707

E
ester.zhou 已提交
4708
static getAccessibleGeolocation(origin: string): Promise\<boolean\>
4709

E
ester.zhou 已提交
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722
Obtains the geolocation permission status of the specified source. This API uses a promise to return the result.

**Parameters**

| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
| origin   | string   | Yes  | -     | Index of the origin.|

**Return value**

| Type              | Description                                 |
| ------------------ | ------------------------------------ |
| Promise\<boolean\> | Promise used to return the geolocation permission status of the specified source. If the operation is successful, the value **true** means that the geolocation permission is granted, and **false** means the opposite. If the operation fails, the geolocation permission status of the specified source is not found.|
4723 4724

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

4726 4727
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4728
  import web_webview from '@ohos.web.webview'
4729 4730 4731
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4732 4733
    controller: WebController = new WebController()
    origin: string = "file:///"
4734 4735
    build() {
      Column() {
E
ester.zhou 已提交
4736
        Button('getAccessibleGeolocationPromise')
4737
          .onClick(() => {
E
ester.zhou 已提交
4738
            web_webview.GeolocationPermissions.getAccessibleGeolocation(this.origin).then(result => {
E
ester.zhou 已提交
4739
              console.log('getAccessibleGeolocationPromise result: ' + result)
E
ester.zhou 已提交
4740
            }).catch(error => {
E
ester.zhou 已提交
4741 4742
              console.log('getAccessibleGeolocationPromise error: ' + JSON.stringify(error))
            })
4743 4744 4745 4746 4747 4748 4749
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
4750
### getStoredGeolocation<sup>9+</sup>
4751

E
ester.zhou 已提交
4752
static getStoredGeolocation(callback: AsyncCallback\<Array\<string\>\>): void
4753

E
ester.zhou 已提交
4754
Obtains the geolocation permission status of all sources. This API uses an asynchronous callback to return the result.
4755 4756 4757

**Parameters**

E
ester.zhou 已提交
4758 4759 4760
| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
| callback | AsyncCallback\<Array\<string\>\> | Yes| - | Callback used to return the geolocation permission status of all sources.|
4761 4762

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

4764 4765
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4766
  import web_webview from '@ohos.web.webview'
4767 4768 4769
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4770
    controller: WebController = new WebController()
4771 4772
    build() {
      Column() {
E
ester.zhou 已提交
4773
        Button('getStoredGeolocationAsync')
4774
          .onClick(() => {
E
ester.zhou 已提交
4775 4776
            web_webview.GeolocationPermissions.getStoredGeolocation((error, origins) => {
              if (error) {
E
ester.zhou 已提交
4777 4778
                console.log('getStoredGeolocationAsync error: ' + JSON.stringify(error))
                return
E
ester.zhou 已提交
4779
              }
E
ester.zhou 已提交
4780 4781 4782
              let origins_str: string = origins.join()
              console.log('getStoredGeolocationAsync origins: ' + origins_str)
            })
4783 4784 4785 4786 4787 4788 4789
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
4790
### getStoredGeolocation<sup>9+</sup>
4791

E
ester.zhou 已提交
4792
static getStoredGeolocation(): Promise\<Array\<string\>\>
4793

E
ester.zhou 已提交
4794
Obtains the geolocation permission status of all sources. This API uses a promise to return the result.
4795 4796

**Parameters**
E
ester.zhou 已提交
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806

| Name   | Type| Mandatory| Default Value| Description      |
| -------- | -------- | ---- | ----- | ------------- |
| callback | AsyncCallback\<Array\<string\>\> | Yes| - | Callback used to return the geolocation permission status of all sources.|

**Return value**

| Type                      | Description                                 |
| -------------------------- | ------------------------------------ |
| Promise\<Array\<string\>\> | Promise used to return the geolocation permission status of all sources.|
4807 4808

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

4810 4811
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4812
  import web_webview from '@ohos.web.webview'
4813 4814 4815
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4816
    controller: WebController = new WebController()
4817 4818
    build() {
      Column() {
E
ester.zhou 已提交
4819
        Button('getStoredGeolocationPromise')
4820
          .onClick(() => {
E
ester.zhou 已提交
4821
            web_webview.GeolocationPermissions.getStoredGeolocation().then(origins => {
E
ester.zhou 已提交
4822 4823
              let origins_str: string = origins.join()
              console.log('getStoredGeolocationPromise origins: ' + origins_str)
E
ester.zhou 已提交
4824
            }).catch(error => {
E
ester.zhou 已提交
4825 4826
                console.log('getStoredGeolocationPromise error: ' + JSON.stringify(error))
            })
4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

## WebStorage<sup>9+</sup>
Implements the **WebStorage** object, which can be used to manage the Web SQL and the HTML5 Web Storage API. All **\<Web>** components in an application share one **WebStorage**.
### deleteAllData<sup>9+</sup>
static deleteAllData(): void

Deletes all data in the Web SQL database.

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

4843 4844
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4845
  import web_webview from '@ohos.web.webview'
4846 4847 4848
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4849
    controller: WebController = new WebController()
4850 4851 4852 4853
    build() {
      Column() {
        Button('deleteAllData')
          .onClick(() => {
E
ester.zhou 已提交
4854
            web_webview.WebStorage.deleteAllData()
4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
          })
        Web({ src: 'www.example.com', controller: this.controller })
        .databaseAccess(true)
      }
    }
  }
  ```

### deleteOrigin<sup>9+</sup>
static deleteOrigin(origin : string): void

Deletes all data in the specified origin.

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

4870 4871 4872 4873 4874
| Name   | Type  | Mandatory  | Description        |
| ------ | ------ | ---- | ---------- |
| origin | string | Yes   | Index of the origin.|

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

4876 4877
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4878
  import web_webview from '@ohos.web.webview'
4879 4880 4881
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4882 4883
    controller: WebController = new WebController()
    origin: string = "origin"
4884 4885 4886 4887
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
E
ester.zhou 已提交
4888
            web_webview.WebStorage.deleteOrigin(this.origin)
4889 4890 4891 4892 4893 4894 4895 4896 4897
          })
        Web({ src: 'www.example.com', controller: this.controller })
        .databaseAccess(true)
      }
    }
  }
  ```

### getOrigins<sup>9+</sup>
E
ester.zhou 已提交
4898
static getOrigins(callback: AsyncCallback\<Array\<WebStorageOrigin>>) : void
4899 4900 4901 4902

Obtains information about all origins that are currently using the Web SQL database. This API uses an asynchronous callback to return the result.

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

4904 4905 4906 4907 4908
| Name     | Type                                    | Mandatory  | Description                                 |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<Array<[WebStorageOrigin](#webstorageorigin9)>> | Yes   | Callback used to return the information about the origins. For details, see **WebStorageOrigin**.|

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

4910 4911
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4912
  import web_webview from '@ohos.web.webview'
4913 4914 4915
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4916 4917
    controller: WebController = new WebController()
    origin: string = "origin"
4918 4919 4920 4921
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
E
ester.zhou 已提交
4922
            web_webview.WebStorage.getOrigins((error, origins) => {
4923
              if (error) {
E
ester.zhou 已提交
4924 4925
                console.log('error: ' + error)
                return
4926 4927
              }
              for (let i = 0; i < origins.length; i++) {
E
ester.zhou 已提交
4928 4929 4930
                console.log('origin: ' + origins[i].origin)
                console.log('usage: ' + origins[i].usage)
                console.log('quota: ' + origins[i].quota)
4931 4932 4933 4934 4935 4936 4937 4938 4939
              }
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
        .databaseAccess(true)
      }
    }
  }
  ```
E
ester.zhou 已提交
4940

4941
### getOrigins<sup>9+</sup>
E
ester.zhou 已提交
4942
static getOrigins() : Promise\<Array\<WebStorageOrigin>>
4943 4944 4945 4946 4947 4948 4949 4950 4951 4952

Obtains information about all origins that are currently using the Web SQL database. This API uses a promise to return the result.

**Return value**

| Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- |
| Promise<Array<[WebStorageOrigin](#webstorageorigin9)>> | Promise used to return the information about the origins. For details, see **WebStorageOrigin**.|

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

4954 4955
  ```ts
  // xxx.ets
E
ester.zhou 已提交
4956
  import web_webview from '@ohos.web.webview'
4957 4958 4959
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4960 4961
    controller: WebController = new WebController()
    origin: string = "origin"
4962 4963 4964 4965
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
E
ester.zhou 已提交
4966
            web_webview.WebStorage.getOrigins()
4967 4968
              .then(origins => {
                for (let i = 0; i < origins.length; i++) {
E
ester.zhou 已提交
4969 4970 4971
                  console.log('origin: ' + origins[i].origin)
                  console.log('usage: ' + origins[i].usage)
                  console.log('quota: ' + origins[i].quota)
4972 4973 4974
                }
              })
              .catch(error => {
E
ester.zhou 已提交
4975
                console.log('error: ' + error)
4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
      }
    }
  }
  ```

### getOriginQuota<sup>9+</sup>
static getOriginQuota(origin : string, callback : AsyncCallback\<number>) : void

Obtains the storage quota of an origin in the Web SQL database, in bytes. This API uses an asynchronous callback to return the result.

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

4992 4993 4994 4995 4996 4997
| Name     | Type                  | Mandatory  | Description       |
| -------- | ---------------------- | ---- | --------- |
| origin   | string                 | Yes   | Index of the origin.|
| callback | AsyncCallback\<number> | Yes   | Callback used to return the storage quota of the origin.|

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

4999 5000
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5001
  import web_webview from '@ohos.web.webview'
5002 5003 5004
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5005 5006
    controller: WebController = new WebController()
    origin: string = "origin"
5007 5008 5009 5010
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
E
ester.zhou 已提交
5011
            web_webview.WebStorage.getOriginQuota(this.origin, (error, quota) => {
5012
              if (error) {
E
ester.zhou 已提交
5013 5014
                console.log('error: ' + error)
                return
5015
              }
E
ester.zhou 已提交
5016
              console.log('quota: ' + quota)
5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
      }
    }
  }
  ```

### getOriginQuota<sup>9+</sup>
static getOriginQuota(origin : string) : Promise\<number>

Obtains the storage quota of an origin in the Web SQL database, in bytes. This API uses a promise to return the result.

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

5033 5034 5035 5036 5037
| Name   | Type  | Mandatory  | Description        |
| ------ | ------ | ---- | ---------- |
| origin | string | Yes   | Index of the origin.|

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

5039 5040 5041 5042 5043
| Type              | Description                     |
| ---------------- | ----------------------- |
| Promise\<number> | Promise used to return the storage quota of the origin.|

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

5045 5046
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5047
  import web_webview from '@ohos.web.webview'
5048 5049 5050 5051
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
E
ester.zhou 已提交
5052
    origin: string = "origin"
5053 5054 5055 5056
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
E
ester.zhou 已提交
5057
            web_webview.WebStorage.getOriginQuota(this.origin)
5058
              .then(quota => {
E
ester.zhou 已提交
5059
                console.log('quota: ' + quota)
5060 5061
              })
              .catch(error => {
E
ester.zhou 已提交
5062
                console.log('error: ' + error)
5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
      }
    }
  }
  ```

### getOriginUsage<sup>9+</sup>
static getOriginUsage(origin : string, callback : AsyncCallback\<number>) : void

Obtains the storage usage of an origin in the Web SQL database, in bytes. This API uses an asynchronous callback to return the result.

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

5079 5080 5081 5082 5083 5084
| Name     | Type                  | Mandatory  | Description        |
| -------- | ---------------------- | ---- | ---------- |
| origin   | string                 | Yes   | Index of the origin.|
| callback | AsyncCallback\<number> | Yes   | Callback used to return the storage usage of the origin.  |

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

5086 5087
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5088
  import web_webview from '@ohos.web.webview'
5089 5090 5091 5092
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
E
ester.zhou 已提交
5093
    origin: string = "origin"
5094 5095 5096 5097
    build() {
      Column() {
        Button('getOriginUsage')
          .onClick(() => {
E
ester.zhou 已提交
5098
            web_webview.WebStorage.getOriginUsage(this.origin, (error, usage) => {
5099
              if (error) {
E
ester.zhou 已提交
5100 5101
                console.log('error: ' + error)
                return
5102
              }
E
ester.zhou 已提交
5103
              console.log('usage: ' + usage)
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
      }
    }
  }
  ```

### getOriginUsage<sup>9+</sup>
static getOriginUsage(origin : string) : Promise\<number>

Obtains the storage usage of an origin in the Web SQL database, in bytes. This API uses a promise to return the result.

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

5120 5121 5122 5123 5124
| Name   | Type  | Mandatory  | Description        |
| ------ | ------ | ---- | ---------- |
| origin | string | Yes   | Index of the origin.|

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

5126 5127 5128 5129 5130
| Type              | Description                    |
| ---------------- | ---------------------- |
| Promise\<number> | Promise used to return the storage usage of the origin.|

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

5132 5133
  ```ts
  // xxx.ets
E
ester.zhou 已提交
5134
  import web_webview from '@ohos.web.webview'
5135 5136 5137 5138
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
E
ester.zhou 已提交
5139
    origin: string = "origin"
5140 5141 5142 5143
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
E
ester.zhou 已提交
5144
            web_webview.WebStorage.getOriginUsage(this.origin)
5145
              .then(usage => {
E
ester.zhou 已提交
5146
                console.log('usage: ' + usage)
5147 5148
              })
              .catch(error => {
E
ester.zhou 已提交
5149
                console.log('error: ' + error)
5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
      }
    }
  }
  ```
### searchAllAsync<sup>9+</sup>

searchAllAsync(searchString: string): void

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).

**Parameters**

| Name | Type  | Mandatory  | Default Value | Description                 |
| ---- | ------ | ---- | ---- | --------------------- |
| searchString | string | Yes   | -    | Search keyword.|

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

5172 5173 5174 5175 5176
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5177 5178
    controller: WebController = new WebController()
    @State searchString: string = "xxx"
5179 5180 5181 5182 5183

    build() {
      Column() {
        Button('searchString')
          .onClick(() => {
E
ester.zhou 已提交
5184
            this.controller.searchAllAsync(this.searchString)
5185 5186 5187
          })
        Button('clearMatches')
          .onClick(() => {
E
ester.zhou 已提交
5188
            this.controller.clearMatches()
5189 5190 5191
          })
        Button('searchNext')
          .onClick(() => {
E
ester.zhou 已提交
5192
            this.controller.searchNext(true)
5193 5194 5195 5196
          })
        Web({ src: 'www.example.com', controller: this.controller })
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
E
ester.zhou 已提交
5197
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting)
5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210
          })
      }
    }
  }
  ```

### clearMatches<sup>9+</sup>

clearMatches(): void

Clears the matches found through [searchAllAsync](#searchallasync9).

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

5212 5213 5214 5215 5216
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5217
    controller: WebController = new WebController()
5218 5219 5220 5221 5222

    build() {
      Column() {
        Button('clearMatches')
          .onClick(() => {
E
ester.zhou 已提交
5223
            this.controller.clearMatches()
5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### searchNext<sup>9+</sup>

searchNext(forward: boolean): void

Searches for and highlights the next match.

**Parameters**

| Name | Type  | Mandatory  | Default Value | Description                 |
| ---- | ------ | ---- | ---- | --------------------- |
| forward | boolean | Yes   | -    | Whether to search forward.|


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

5246 5247 5248 5249 5250
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5251
    controller: WebController = new WebController()
5252 5253 5254 5255 5256

    build() {
      Column() {
        Button('searchNext')
          .onClick(() => {
E
ester.zhou 已提交
5257
            this.controller.searchNext(true)
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

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

Invoked to notify the caller of the search result on the web page.

**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**
E
ester.zhou 已提交
5280

5281 5282 5283 5284 5285
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5286
    controller: WebController = new WebController()
5287 5288 5289 5290 5291 5292

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
E
ester.zhou 已提交
5293
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting)
5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304
          })
      }
    }
  }
  ```

## WebStorageOrigin<sup>9+</sup>

Provides usage information about the Web SQL database.

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

5306 5307 5308 5309 5310 5311
| Name   | Type  | Mandatory  | Description        |
| ------ | ------ | ---- | ---------- |
| origin | string | Yes   | Index of the origin.|
| usage  | number | Yes   | Storage usage of the origin.  |
| quota  | number | Yes   | Storage quota of the origin. |

E
ester.zhou 已提交
5312
## MessageLevel
5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323

| Name   | Description   |
| ----- | :---- |
| Debug | Debug level.|
| Error | Error level.|
| Info  | Information level.|
| Log   | Log level.|
| Warn  | Warning level. |

## RenderExitReason

E
ester.zhou 已提交
5324
Enumerates the reasons why the rendering process exits.
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369

| 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.                   |
| Unknown       | Unknown content.                   |

E
ester.zhou 已提交
5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380
## SslError<sup>9+</sup>

Enumerates the error codes returned by **onSslErrorEventReceive** API.

| 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.|

5381 5382 5383 5384
## ProtectedResourceType<sup>9+</sup>

| Name     | Description           | Remarks          |
| --------- | -------------- | -------------- |
E
ester.zhou 已提交
5385
| MidiSysex | MIDI SYSEX resource.| Currently, only permission events can be reported. MIDI devices are not yet supported.|
5386 5387 5388 5389 5390 5391

## WebAsyncController

Implements the **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
E
ester.zhou 已提交
5392

E
ester.zhou 已提交
5393
```
5394 5395
webController: WebController = new WebController();
webAsyncController: WebAsyncController = new WebAsyncController(webController);
E
ester.zhou 已提交
5396 5397
```

5398 5399
### storeWebArchive<sup>9+</sup>

E
ester.zhou 已提交
5400
storeWebArchive(baseName: string, autoName: boolean, callback: AsyncCallback\<string>): void
5401

E
ester.zhou 已提交
5402
Stores this web page. This API uses an asynchronous callback to return the result.
5403 5404 5405 5406 5407

**Parameters**

| Name     | Type                                    | Mandatory  | Description                                 |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
E
ester.zhou 已提交
5408 5409 5410
| 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.|
5411 5412 5413 5414 5415

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
5416
  import web_webview from '@ohos.web.webview'
5417 5418 5419
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5420
    controller: WebController = new WebController()
5421 5422 5423 5424
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
E
ester.zhou 已提交
5425
            let webAsyncController = new web_webview.WebAsyncController(this.controller)
5426 5427 5428 5429
            webAsyncController.storeWebArchive("/data/storage/el2/base/", true, (filename) => {
              if (filename != null) {
                console.info(`save web archive success: ${filename}`)
              }
E
ester.zhou 已提交
5430
            })
5431 5432 5433 5434 5435 5436 5437 5438 5439
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### storeWebArchive<sup>9+</sup>

E
ester.zhou 已提交
5440
storeWebArchive(baseName: string, autoName: boolean): Promise\<string>
5441

E
ester.zhou 已提交
5442
Stores this web page. This API uses a promise to return the result.
5443 5444 5445 5446 5447

**Parameters**

| Name     | Type                                    | Mandatory  | Description                                 |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
E
ester.zhou 已提交
5448 5449
| 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. |
5450 5451 5452

**Return value**

E
ester.zhou 已提交
5453 5454 5455
| Type             | Description                                                  |
| ---------------- | ------------------------------------------------------------ |
| Promise\<string> | Promise used to return the save path if the operation is successful and null otherwise. |
5456 5457 5458 5459 5460

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
5461
  import web_webview from '@ohos.web.webview'
5462 5463 5464 5465 5466 5467 5468 5469
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
E
ester.zhou 已提交
5470
            let webAsyncController = new web_webview.WebAsyncController(this.controller);
5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482
            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 已提交
5483 5484 5485

## WebMessagePort<sup>9+</sup>

E
ester.zhou 已提交
5486
Defines a **WebMessagePort** instance, which can be used to send and receive messages.
E
ester.zhou 已提交
5487 5488 5489 5490 5491 5492 5493 5494 5495

### close<sup>9+</sup>
close(): void

Disables this message port.

### postMessageEvent<sup>9+</sup>
postMessageEvent(message: WebMessageEvent): void

E
ester.zhou 已提交
5496
Sends messages. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| message   | [WebMessageEvent](#webmessageevent9) | Yes   | -    | Message to send.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5511 5512
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5513 5514 5515 5516 5517

    build() {
      Column() {
        Button('postMessageEvent')
          .onClick(() => {
E
ester.zhou 已提交
5518 5519 5520
            var msg = new WebMessageEvent()
            msg.setData("post message from ets to html5")
            this.ports[0].postMessageEvent(msg)
E
ester.zhou 已提交
5521 5522 5523 5524 5525 5526 5527 5528 5529 5530
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### onMessageEvent<sup>9+</sup>
onMessageEvent(callback: (result: string) => void): void

E
ester.zhou 已提交
5531
Registers a callback to receive messages from an HTML5 page. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| callback   | function | Yes   | -    | Callback for receiving messages.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5546 5547
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
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

    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**

| Type                             | Description           |
| ------------------------------- | ------------- |
| 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 已提交
5591 5592 5593
            msgEvent.setData("message event data")
            var messageData = msgEvent.getData()
            console.log("message is:" + messageData)
E
ester.zhou 已提交
5594 5595 5596 5597 5598 5599 5600 5601 5602
          })
      }
    }
  }
  ```

### setData<sup>9+</sup>
setData(data: string): void

E
ester.zhou 已提交
5603
Sets the message in this object. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| data   | string | Yes   | -    | Message to send.|

**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('setData')
          .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 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651
          })
        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**

| Type                             | Description           |
| ------------------------------- | ------------- |
| Array\<[WebMessagePort](#webmessageport9)\> | Message port stored in the object of this type.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5652
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5653 5654 5655 5656
    build() {
      Column() {
        Button('getPorts')
          .onClick(() => {
E
ester.zhou 已提交
5657 5658 5659 5660 5661
            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 已提交
5662 5663 5664 5665 5666 5667 5668 5669 5670
          })
      }
    }
  }
  ```

### setPorts<sup>9+</sup>
setPorts(ports: Array\<WebMessagePort\>): void

E
ester.zhou 已提交
5671
Sets the message port in this object. For the complete sample code, see [postMessage](#postmessage9).
E
ester.zhou 已提交
5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685

**Parameters**

| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
| ports   | Array\<[WebMessagePort](#webmessageport9)\> | Yes   | -    | Message port.|

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
5686 5687
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
E
ester.zhou 已提交
5688 5689 5690 5691 5692
  
    build() {
      Column() {
        Button('setPorts')
          .onClick(() => {
E
ester.zhou 已提交
5693 5694 5695 5696 5697
            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 已提交
5698 5699 5700 5701 5702 5703
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```