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

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

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

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

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

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

## APIs

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

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

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

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

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

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

E
ester.zhou 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
  @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 已提交
54 55
  ```ts
  // xxx.ets
E
ester.zhou 已提交
56 57
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
58 59 60
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
61
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
62 63 64 65 66 67 68 69
    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
70
  Example of loading local resource files in the sandbox:
E
ester.zhou 已提交
71

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

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

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

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

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

## Attributes
119

120
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).
121 122 123 124 125 126 127 128

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

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

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

130 131
| Name             | Type   | Mandatory  | Default Value  | Description                                |
| ---------------- | ------- | ---- | ----- | ------------------------------------ |
132 133 134
| domStorageAccess | boolean | Yes   | false | Whether to enable the DOM Storage API.|

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

136 137
  ```ts
  // xxx.ets
E
ester.zhou 已提交
138 139
  import web_webview from '@ohos.web.webview'

140 141 142
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
143
    controller: web_webview.WebviewController = new web_webview.WebviewController()
144 145
    build() {
      Column() {
E
ester.zhou 已提交
146 147
        Web({ src: 'www.example.com', controller: this.controller })
          .domStorageAccess(true)
148 149 150 151 152 153 154 155 156
      }
    }
  }
  ```

### fileAccess

fileAccess(fileAccess: boolean)

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

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

161 162
| Name       | Type   | Mandatory  | Default Value | Description                  |
| ---------- | ------- | ---- | ---- | ---------------------- |
E
ester.zhou 已提交
163
| fileAccess | boolean | Yes   | true | Whether to enable access to the file system in the application. By default, this feature is enabled.|
164 165

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

167 168
  ```ts
  // xxx.ets
E
ester.zhou 已提交
169 170
  import web_webview from '@ohos.web.webview'

171 172 173
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
174
    controller: web_webview.WebviewController = new web_webview.WebviewController()
175 176
    build() {
      Column() {
E
ester.zhou 已提交
177 178
        Web({ src: 'www.example.com', controller: this.controller })
          .fileAccess(true)
179 180 181 182 183 184 185 186 187 188 189 190
      }
    }
  }
  ```

### imageAccess

imageAccess(imageAccess: boolean)

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

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

192 193
| Name        | Type   | Mandatory  | Default Value | Description           |
| ----------- | ------- | ---- | ---- | --------------- |
194
| imageAccess | boolean | Yes   | true | Whether to enable automatic image loading.|
195 196 197 198

**Example**
  ```ts
  // xxx.ets
E
ester.zhou 已提交
199 200
  import web_webview from '@ohos.web.webview'

201 202 203
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
204
    controller: web_webview.WebviewController = new web_webview.WebviewController()
205 206
    build() {
      Column() {
E
ester.zhou 已提交
207 208
        Web({ src: 'www.example.com', controller: this.controller })
          .imageAccess(true)
209 210 211 212 213 214 215 216
      }
    }
  }
  ```

### javaScriptProxy

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

219
Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. The parameters cannot be updated.
220 221

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

223 224 225 226 227
| Name       | Type                                    | Mandatory  | Default Value | Description                     |
| ---------- | ---------------------------------------- | ---- | ---- | ------------------------- |
| object     | object                                   | Yes   | -    | Object to be registered. Methods can be declared, but attributes cannot.   |
| name       | string                                   | Yes   | -    | Name of the object to be registered, which is the same as that invoked in the window.|
| methodList | Array\<string\>                          | Yes   | -    | Methods of the JavaScript object to be registered at the application side. |
E
ester.zhou 已提交
228
| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | Yes   | -    | Controller. This API is deprecated since API version 9. You are advised to use **WebviewController** instead.|
229 230

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

E
ester.zhou 已提交
232 233 234 235 236 237 238 239
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
240 241
    testObj = {
      test: (data1, data2, data3) => {
E
ester.zhou 已提交
242 243 244 245
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
246 247
      },
      toString: () => {
E
ester.zhou 已提交
248
        console.log('toString' + "interface instead.")
249 250 251 252
      }
    }
    build() {
      Column() {
E
ester.zhou 已提交
253 254 255 256 257 258 259
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
260 261 262 263 264 265 266 267 268 269 270 271 272
        })
      }
    }
  }
  ```

### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

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

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

274 275 276 277 278
| Name             | Type   | Mandatory  | Default Value | Description               |
| ---------------- | ------- | ---- | ---- | ------------------- |
| javaScriptAccess | boolean | Yes   | true | Whether JavaScript scripts can be executed.|

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

280 281
  ```ts
  // xxx.ets
E
ester.zhou 已提交
282 283
  import web_webview from '@ohos.web.webview'

284 285 286
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
287
    controller: web_webview.WebviewController = new web_webview.WebviewController()
288 289
    build() {
      Column() {
E
ester.zhou 已提交
290 291
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
292 293 294 295 296 297 298 299 300 301 302 303
      }
    }
  }
  ```

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

305 306
| Name      | Type                       | Mandatory  | Default Value           | Description     |
| --------- | --------------------------- | ---- | -------------- | --------- |
E
ester.zhou 已提交
307
| mixedMode | [MixedMode](#mixedmode)| Yes   | MixedMode.None | Mixed content to load.|
308 309

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

311 312
  ```ts
  // xxx.ets
E
ester.zhou 已提交
313 314
  import web_webview from '@ohos.web.webview'

315 316 317
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
318
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
319
    @State mode: MixedMode = MixedMode.All
320 321
    build() {
      Column() {
E
ester.zhou 已提交
322 323
        Web({ src: 'www.example.com', controller: this.controller })
          .mixedMode(this.mode)
324 325 326 327 328 329 330 331 332 333 334 335
      }
    }
  }
  ```

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

337 338 339 340 341
| 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 已提交
342

343 344
  ```ts
  // xxx.ets
E
ester.zhou 已提交
345 346
  import web_webview from '@ohos.web.webview'

347 348 349
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
350
    controller: web_webview.WebviewController = new web_webview.WebviewController()
351 352
    build() {
      Column() {
E
ester.zhou 已提交
353 354
        Web({ src: 'www.example.com', controller: this.controller })
          .onlineImageAccess(true)
355 356 357 358 359 360 361 362 363 364 365 366
      }
    }
  }
  ```

### zoomAccess

zoomAccess(zoomAccess: boolean)

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

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

368 369 370 371 372
| Name       | Type   | Mandatory  | Default Value | Description         |
| ---------- | ------- | ---- | ---- | ------------- |
| zoomAccess | boolean | Yes   | true | Whether to enable zoom gestures.|

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

374 375
  ```ts
  // xxx.ets
E
ester.zhou 已提交
376 377
  import web_webview from '@ohos.web.webview'

378 379 380
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
381
    controller: web_webview.WebviewController = new web_webview.WebviewController()
382 383
    build() {
      Column() {
E
ester.zhou 已提交
384 385
        Web({ src: 'www.example.com', controller: this.controller })
          .zoomAccess(true)
386 387 388 389 390 391 392 393 394 395 396 397
      }
    }
  }
  ```

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

399 400
| Name               | Type   | Mandatory  | Default Value | Description           |
| ------------------ | ------- | ---- | ---- | --------------- |
E
ester.zhou 已提交
401
| overviewModeAccess | boolean | Yes   | true | Whether to load web pages by using the overview mode.|
402 403

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

405 406
  ```ts
  // xxx.ets
E
ester.zhou 已提交
407 408
  import web_webview from '@ohos.web.webview'

409 410 411
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
412
    controller: web_webview.WebviewController = new web_webview.WebviewController()
413 414
    build() {
      Column() {
E
ester.zhou 已提交
415 416
        Web({ src: 'www.example.com', controller: this.controller })
          .overviewModeAccess(true)
417 418 419 420 421 422 423 424 425 426 427 428
      }
    }
  }
  ```

### databaseAccess

databaseAccess(databaseAccess: boolean)

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

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

430 431
| Name           | Type   | Mandatory  | Default Value  | Description             |
| -------------- | ------- | ---- | ----- | ----------------- |
E
ester.zhou 已提交
432
| databaseAccess | boolean | Yes   | false | Whether to enable database access.|
433 434

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

436 437
  ```ts
  // xxx.ets
E
ester.zhou 已提交
438 439
  import web_webview from '@ohos.web.webview'

440 441 442
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
443
    controller: web_webview.WebviewController = new web_webview.WebviewController()
444 445
    build() {
      Column() {
E
ester.zhou 已提交
446 447
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
448 449 450 451 452 453 454 455 456 457 458 459
      }
    }
  }
  ```

### geolocationAccess

geolocationAccess(geolocationAccess: boolean)

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

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

461 462 463
| Name              | Type   | Mandatory  | Default Value | Description           |
| ----------------- | ------- | ---- | ---- | --------------- |
| geolocationAccess | boolean | Yes   | true | Whether to enable geolocation access.|
464 465

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

467 468
  ```ts
  // xxx.ets
E
ester.zhou 已提交
469 470
  import web_webview from '@ohos.web.webview'

471 472 473
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
474
    controller: web_webview.WebviewController = new web_webview.WebviewController()
475 476
    build() {
      Column() {
E
ester.zhou 已提交
477 478 479 480 481 482 483 484 485 486 487
        Web({ src: 'www.example.com', controller: this.controller })
          .geolocationAccess(true)
      }
    }
  }
  ```

### mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

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

**Parameters**

492 493 494
| Name   | Type   | Mandatory  | Default Value | Description             |
| ------ | ------- | ---- | ---- | ----------------- |
| access | boolean | Yes   | true | Whether video playback must be started by user gestures.|
E
ester.zhou 已提交
495 496 497 498 499

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
500 501
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
502 503 504
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
505
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
506
    @State access: boolean = true
E
ester.zhou 已提交
507 508 509 510
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .mediaPlayGestureAccess(this.access)
511 512 513 514 515
      }
    }
  }
  ```

E
ester.zhou 已提交
516 517 518 519 520
### multiWindowAccess<sup>9+</sup>

multiWindowAccess(multiWindow: boolean)

Sets whether to enable the multi-window permission.
E
ester.zhou 已提交
521
Enabling the multi-window permission requires implementation of the **onWindowNew** event. For the sample code, see [onWindowNew](#onwindownew9).
522

E
ester.zhou 已提交
523 524
**Parameters**

525 526
| Name        | Type   | Mandatory  | Default Value  | Description        |
| ----------- | ------- | ---- | ----- | ------------ |
E
ester.zhou 已提交
527 528 529 530 531 532
| multiWindow | boolean | Yes   | false | Whether to enable the multi-window permission.|

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
533 534
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
535 536 537
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
538
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
539 540 541
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
542
        .multiWindowAccess(false)
E
ester.zhou 已提交
543 544 545 546 547
      }
    }
  }
  ```

548 549 550 551
### horizontalScrollBarAccess<sup>9+</sup>

horizontalScrollBarAccess(horizontalScrollBar: boolean)

E
ester.zhou 已提交
552
Sets whether to display the horizontal scrollbar, including the default system scrollbar and custom scrollbar. By default, the horizontal scrollbar is displayed.
553 554 555 556 557 558 559 560 561 562 563

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
564 565
  import web_webview from '@ohos.web.webview'

566 567 568
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
569
    controller: web_webview.WebviewController = new web_webview.WebviewController()
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .horizontalScrollBarAccess(true)
      }
    }
  }
  ```

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

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

verticalScrollBarAccess(verticalScrollBar: boolean)

E
ester.zhou 已提交
605
Sets whether to display the vertical scrollbar, including the default system scrollbar and custom scrollbar. By default, the vertical scrollbar is displayed.
606 607 608 609 610 611 612 613 614 615 616

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
617 618
  import web_webview from '@ohos.web.webview'

619 620 621
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
622
    controller: web_webview.WebviewController = new web_webview.WebviewController()
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .verticalScrollBarAccess(true)
      }
    }
  }
  ```

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

654 655 656 657 658 659 660
### cacheMode

cacheMode(cacheMode: CacheMode)

Sets the cache mode.

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

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

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

668 669
  ```ts
  // xxx.ets
E
ester.zhou 已提交
670 671
  import web_webview from '@ohos.web.webview'

672 673 674
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
675
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
676
    @State mode: CacheMode = CacheMode.None
677 678
    build() {
      Column() {
E
ester.zhou 已提交
679 680
        Web({ src: 'www.example.com', controller: this.controller })
          .cacheMode(this.mode)
681 682 683 684 685
      }
    }
  }
  ```

E
ester.zhou 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
### textZoomAtio<sup>(deprecated)</sup>

textZoomAtio(textZoomAtio: number)

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

This API is deprecated since API version 9. You are advised to use [textZoomRatio<sup>9+</sup>](#textzoomratio9) instead.

**Parameters**

| Name          | Type  | Mandatory  | Default Value | Description           |
| ------------- | ------ | ---- | ---- | --------------- |
| textZoomAtio | number | Yes   | 100  | Text zoom ratio to set.|

**Example**

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

E
ester.zhou 已提交
718
### textZoomRatio<sup>9+</sup>
719 720 721 722 723 724

textZoomRatio(textZoomRatio: number)

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

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

726 727 728
| Name          | Type  | Mandatory  | Default Value | Description           |
| ------------- | ------ | ---- | ---- | --------------- |
| textZoomRatio | number | Yes   | 100  | Text zoom ratio to set.|
729 730

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

732 733
  ```ts
  // xxx.ets
E
ester.zhou 已提交
734 735
  import web_webview from '@ohos.web.webview'

736 737 738
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
739
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
740
    @State atio: number = 150
741 742
    build() {
      Column() {
E
ester.zhou 已提交
743 744
        Web({ src: 'www.example.com', controller: this.controller })
          .textZoomRatio(this.atio)
745 746 747 748 749
      }
    }
  }
  ```

E
ester.zhou 已提交
750 751 752 753 754 755 756 757
### initialScale<sup>9+</sup>

initialScale(percent: number)

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

**Parameters**

758 759 760
| Name    | Type  | Mandatory  | Default Value | Description           |
| ------- | ------ | ---- | ---- | --------------- |
| percent | number | Yes   | 100  | Scale factor of the entire page.|
E
ester.zhou 已提交
761 762 763 764 765

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
766 767
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
768 769 770
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
771
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
772 773 774 775 776 777 778 779 780 781
    @State percent: number = 100
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .initialScale(this.percent)
      }
    }
  }
  ```

782 783 784 785 786 787 788
### userAgent

userAgent(userAgent: string)

Sets the user agent.

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

790 791 792 793 794
| Name      | Type  | Mandatory  | Default Value | Description     |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | Yes   | -    | User agent to set.|

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

796 797
  ```ts
  // xxx.ets
E
ester.zhou 已提交
798 799
  import web_webview from '@ohos.web.webview'

800 801 802
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
803
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
804
    @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'
805 806
    build() {
      Column() {
E
ester.zhou 已提交
807 808
        Web({ src: 'www.example.com', controller: this.controller })
          .userAgent(this.userAgent)
809 810 811 812
      }
    }
  }
  ```
E
ester.zhou 已提交
813

814
### blockNetwork<sup>9+</sup>
E
ester.zhou 已提交
815

816
blockNetwork(block: boolean)
E
ester.zhou 已提交
817

818
Sets whether to block online downloads.
E
ester.zhou 已提交
819

820
**Parameters**
821

822 823 824
| Name| Type| Mandatory| Default Value| Description                           |
| ------ | -------- | ---- | ------ | ----------------------------------- |
| block  | boolean  | Yes  | false  | Whether to block online downloads.|
825

826
**Example**
827

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State block: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .blockNetwork(this.block)
      }
    }
  }
  ```
E
ester.zhou 已提交
844

845
### defaultFixedFontSize<sup>9+</sup>
846

847
defaultFixedFontSize(size: number)
E
ester.zhou 已提交
848

849 850 851 852 853 854 855
Sets the default fixed font size for the web page.

**Parameters**

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

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

859 860
  ```ts
  // xxx.ets
861
  import web_webview from '@ohos.web.webview'
862 863 864
  @Entry
  @Component
  struct WebComponent {
865 866
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 16
867 868
    build() {
      Column() {
E
ester.zhou 已提交
869
        Web({ src: 'www.example.com', controller: this.controller })
870
          .defaultFixedFontSize(this.fontSize)
871 872 873 874 875
      }
    }
  }
  ```

876
### defaultFontSize<sup>9+</sup>
877

878
defaultFontSize(size: number)
879

880
Sets the default font size for the web page.
881 882

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

884 885 886
| Name| Type| Mandatory| Default Value| Description                |
| ------ | -------- | ---- | ------ | ------------------------ |
| size   | number   | Yes  | 16     | Default font size to set, in px. The value ranges from -2^31 to 2^31-1. In actual rendering, values greater than 72 are handled as 72, and values less than 1 are handled as 1. |
887 888

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

890 891
  ```ts
  // xxx.ets
892
  import web_webview from '@ohos.web.webview'
893 894 895
  @Entry
  @Component
  struct WebComponent {
896 897
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
898 899 900
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
901
          .defaultFontSize(this.fontSize)
902 903 904 905 906
      }
    }
  }
  ```

907
### minFontSize<sup>9+</sup>
908

909
minFontSize(size: number)
910

911
Sets the minimum font size for the web page.
912 913

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

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

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

921 922
  ```ts
  // xxx.ets
923
  import web_webview from '@ohos.web.webview'
924 925 926
  @Entry
  @Component
  struct WebComponent {
927 928
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
929 930 931
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
932
          .minFontSize(this.fontSize)
933 934 935 936 937
      }
    }
  }
  ```

938
### minLogicalFontSize<sup>9+</sup>
939

940
minLogicalFontSize(size: number)
E
ester.zhou 已提交
941

942
Sets the minimum logical font size for the web page.
943

944
**Parameters**
E
ester.zhou 已提交
945

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

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

952 953
  ```ts
  // xxx.ets
954
  import web_webview from '@ohos.web.webview'
955 956 957
  @Entry
  @Component
  struct WebComponent {
958 959
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State fontSize: number = 13
960 961
    build() {
      Column() {
E
ester.zhou 已提交
962
        Web({ src: 'www.example.com', controller: this.controller })
963
          .minLogicalFontSize(this.fontSize)
E
ester.zhou 已提交
964
      }
965 966 967 968 969
    }
  }
  ```


970
### webFixedFont<sup>9+</sup>
971

972
webFixedFont(family: string)
E
ester.zhou 已提交
973

974
Sets the fixed font family for the web page.
975

976
**Parameters**
E
ester.zhou 已提交
977

978 979 980
| Name| Type| Mandatory| Default Value   | Description                    |
| ------ | -------- | ---- | --------- | ---------------------------- |
| family | string   | Yes  | monospace | Fixed font family to set.|
981 982

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

984 985
  ```ts
  // xxx.ets
986
  import web_webview from '@ohos.web.webview'
987 988 989
  @Entry
  @Component
  struct WebComponent {
990 991
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "monospace"
992 993 994
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
995
          .webFixedFont(this.family)
996 997 998 999 1000
      }
    }
  }
  ```

1001
### webSansSerifFont<sup>9+</sup>
1002

1003 1004 1005
webSansSerifFont(family: string)

Sets the sans serif font family for the web page.
1006 1007

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

1009 1010 1011
| Name| Type| Mandatory| Default Value    | Description                         |
| ------ | -------- | ---- | ---------- | --------------------------------- |
| family | string   | Yes  | sans-serif | Sans serif font family to set.|
1012 1013

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

1015 1016
  ```ts
  // xxx.ets
1017
  import web_webview from '@ohos.web.webview'
1018 1019 1020
  @Entry
  @Component
  struct WebComponent {
1021 1022
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
1023 1024 1025
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1026
          .webSansSerifFont(this.family)
1027 1028 1029 1030 1031
      }
    }
  }
  ```

1032
### webSerifFont<sup>9+</sup>
1033

1034
webSerifFont(family: string)
1035

1036
Sets the serif font family for the web page.
1037 1038

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

1040 1041 1042
| Name| Type| Mandatory| Default Value| Description                    |
| ------ | -------- | ---- | ------ | ---------------------------- |
| family | string   | Yes  | serif  | Serif font family to set.|
1043 1044

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

1046 1047
  ```ts
  // xxx.ets
1048
  import web_webview from '@ohos.web.webview'
1049 1050 1051
  @Entry
  @Component
  struct WebComponent {
1052 1053
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "serif"
1054 1055 1056
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1057
          .webSerifFont(this.family)
1058 1059 1060 1061 1062
      }
    }
  }
  ```

1063
### webStandardFont<sup>9+</sup>
1064

1065
webStandardFont(family: string)
1066

1067
Sets the standard font family for the web page.
1068 1069

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

1071 1072 1073
| Name| Type| Mandatory| Default Value    | Description                       |
| ------ | -------- | ---- | ---------- | ------------------------------- |
| family | string   | Yes  | sans serif | Standard font family to set.|
1074 1075

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

1077 1078
  ```ts
  // xxx.ets
1079
  import web_webview from '@ohos.web.webview'
1080 1081 1082
  @Entry
  @Component
  struct WebComponent {
1083 1084
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
1085 1086 1087
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1088
          .webStandardFont(this.family)
1089 1090 1091 1092 1093
      }
    }
  }
  ```

1094
### webFantasyFont<sup>9+</sup>
1095

1096
webFantasyFont(family: string)
1097

1098
Sets the fantasy font family for the web page.
1099 1100

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

1102 1103 1104
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | Yes  | fantasy | Fantasy font family to set.|
1105 1106

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

1108 1109
  ```ts
  // xxx.ets
1110
  import web_webview from '@ohos.web.webview'
1111 1112 1113
  @Entry
  @Component
  struct WebComponent {
1114 1115
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "fantasy"
1116 1117 1118
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1119
          .webFantasyFont(this.family)
1120 1121 1122 1123 1124
      }
    }
  }
  ```

1125
### webCursiveFont<sup>9+</sup>
1126

1127
webCursiveFont(family: string)
1128

1129
Sets the cursive font family for the web page.
1130 1131

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

1133 1134 1135
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | Yes  | cursive | Cursive font family to set.|
1136 1137

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

1139 1140
  ```ts
  // xxx.ets
1141
  import web_webview from '@ohos.web.webview'
1142 1143 1144
  @Entry
  @Component
  struct WebComponent {
1145 1146
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "cursive"
1147 1148 1149
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1150
          .webCursiveFont(this.family)
1151 1152 1153 1154 1155
      }
    }
  }
  ```

1156
### darkMode<sup>9+</sup>
1157

1158
darkMode(mode: WebDarkMode)
1159

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

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

1164 1165 1166
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | ----------- | ---- | --------------- | ------------------ |
|  mode  | [WebDarkMode](#webdarkmode9) | Yes  | WebDarkMode.Off | Web dark mode to set.|
1167

E
ester.zhou 已提交
1168 1169
**Example**

1170 1171
  ```ts
  // xxx.ets
1172
  import web_webview from '@ohos.web.webview'
1173 1174 1175
  @Entry
  @Component
  struct WebComponent {
1176 1177
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
1178 1179 1180
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1181
          .darkMode(this.mode)
1182 1183 1184 1185 1186
      }
    }
  }
  ```

1187
### forceDarkAccess<sup>9+</sup>
1188

1189
forceDarkAccess(access: boolean)
1190

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

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

1195 1196 1197
| Name| Type| Mandatory| Default Value | Description                      |
| ------ | ------- | ---- | ----- | ------------------ |
| access | boolean | Yes  | false | Whether to enable forcible dark mode for the web page.|
1198

E
ester.zhou 已提交
1199 1200
**Example**

1201 1202
  ```ts
  // xxx.ets
1203
  import web_webview from '@ohos.web.webview'
1204 1205 1206
  @Entry
  @Component
  struct WebComponent {
1207 1208 1209
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
    @State access: boolean = true
1210 1211 1212
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1213 1214
          .darkMode(this.mode)
          .forceDarkAccess(this.access)
1215 1216 1217 1218 1219
      }
    }
  }
  ```

1220
### pinchSmooth<sup>9+</sup>
1221

1222
pinchSmooth(isEnabled: boolean)
1223

1224
Sets whether to enable smooth pinch mode for the web page.
1225 1226

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

1228 1229 1230
| Name   | Type| Mandatory| Default Value| Description                  |
| --------- | -------- | ---- | ------ | -------------------------- |
| isEnabled | boolean  | Yes  | false  | Whether to enable smooth pinch mode for the web page.|
E
ester.zhou 已提交
1231 1232

**Example**
1233 1234

  ```ts
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'www.example.com', controller: this.controller })
        .pinchSmooth(true)
1245 1246
    }
  }
1247
}
1248 1249
  ```

E
ester.zhou 已提交
1250

1251
## Events
1252

1253
The universal events are not supported.
1254

1255 1256 1257 1258
### onAlert

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

E
ester.zhou 已提交
1259
Called when **alert()** is invoked to display an alert dialog box on the web page.
1260 1261

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

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |

**Return value**

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

E
ester.zhou 已提交
1275 1276
**Example**

1277 1278
  ```ts
  // xxx.ets
E
ester.zhou 已提交
1279 1280
  import web_webview from '@ohos.web.webview'

1281 1282 1283
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1284
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1285 1286
    build() {
      Column() {
E
ester.zhou 已提交
1287
        Web({ src: $rawfile("xxx.html"), controller: this.controller })
1288
          .onAlert((event) => {
E
ester.zhou 已提交
1289 1290
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
            AlertDialog.show({
              title: 'onAlert',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
1311 1312 1313 1314 1315 1316
          })
      }
    }
  }
  ```

E
ester.zhou 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
  ```
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
  </head>
  <body>
    <h1>WebView onAlert Demo</h1>
    <button onclick="myFunction()">Click here</button>
    <script>
      function myFunction() {
        alert("Hello World");
      }
    </script>
  </body>
  </html>
  ```

1336
### onBeforeUnload
1337

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

E
ester.zhou 已提交
1340
Called when this page is about to exit after the user refreshes or closes the page. This API takes effect only when the page has obtained focus.
1341 1342

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

1344 1345 1346 1347 1348
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |
1349

E
ester.zhou 已提交
1350 1351
**Return value**

1352 1353
| Type     | Description                                      |
| ------- | ---------------------------------------- |
E
ester.zhou 已提交
1354
| boolean | If the callback returns **true**, the application can use the system dialog box (allows the confirm and cancel operations) and invoke the **JsResult** API to instruct the **\<Web>** component to exit the current page based on the user operation. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
1355 1356 1357 1358

**Example**

  ```ts
1359
  // xxx.ets
E
ester.zhou 已提交
1360 1361
  import web_webview from '@ohos.web.webview'

1362 1363 1364
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1365
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1366

1367 1368
    build() {
      Column() {
E
ester.zhou 已提交
1369
        Web({ src: $rawfile("xxx.html"), controller: this.controller })
1370 1371 1372
          .onBeforeUnload((event) => {
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
1373
            AlertDialog.show({
1374 1375 1376 1377
              title: 'onBeforeUnload',
              message: 'text',
              primaryButton: {
                value: 'cancel',
1378
                action: () => {
1379 1380 1381 1382 1383 1384 1385
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
1386 1387 1388
                }
              },
              cancel: () => {
1389
                event.result.handleCancel()
1390 1391
              }
            })
E
ester.zhou 已提交
1392 1393 1394 1395 1396 1397 1398
            return true
          })
      }
    }
  }
  ```

E
ester.zhou 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
  ```
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
  </head>
  <body onbeforeunload="return myFunction()">
    <h1>WebView onBeforeUnload Demo</h1>
    <a href="https://www.example.com">Click here</a>
    <script>
      function myFunction() {
        return "onBeforeUnload Event";
      }
    </script>
  </body>
  </html>
  ```

1418
### onConfirm
E
ester.zhou 已提交
1419

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

E
ester.zhou 已提交
1422
Called when **confirm()** is invoked by the web page.
E
ester.zhou 已提交
1423 1424 1425

**Parameters**

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

**Return value**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1442 1443
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1444 1445 1446
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1447
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1448

E
ester.zhou 已提交
1449 1450
    build() {
      Column() {
E
ester.zhou 已提交
1451
        Web({ src: $rawfile("xxx.html"), controller: this.controller })
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
          .onConfirm((event) => {
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            AlertDialog.show({
              title: 'onConfirm',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
E
ester.zhou 已提交
1475 1476 1477 1478 1479 1480
          })
      }
    }
  }
  ```

E
ester.zhou 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
  ```
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
  </head>

  <body>
    <h1>WebView onConfirm Demo</h1>
    <button onclick="myFunction()">Click here</button>
    <p id="demo"></p>
    <script>
      function myFunction() {
        let x;
        let r = confirm("click button!");
        if (r == true) {
          x = "ok";
        } else {
          x = "cancel";
        }
        document.getElementById("demo").innerHTML = x;
      }
    </script>
  </body>
  </html>
  ```

1509
### onPrompt<sup>9+</sup>
E
ester.zhou 已提交
1510

1511
onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean)
E
ester.zhou 已提交
1512 1513 1514

**Parameters**

1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
| Name    | Type                 | Description           |
| ------- | --------------------- | --------------- |
| url     | string                | URL of the web page where the dialog box is displayed.|
| message | string                | Message displayed in the dialog box.      |
| result  | [JsResult](#jsresult) | User operation. |

**Return value**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1531 1532
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1533 1534 1535
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1536
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1537

E
ester.zhou 已提交
1538 1539
    build() {
      Column() {
E
ester.zhou 已提交
1540
        Web({ src: $rawfile("xxx.html"), controller: this.controller })
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
          .onPrompt((event) => {
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
            AlertDialog.show({
              title: 'onPrompt',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
E
ester.zhou 已提交
1557
                  event.result.handlePromptConfirm(event.value)
1558 1559 1560 1561 1562 1563 1564
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true
1565 1566 1567 1568 1569 1570
          })
      }
    }
  }
  ```

E
ester.zhou 已提交
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  ```
  <!--xxx.html-->
  <!DOCTYPE html>
  <html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
  </head>

  <body>
    <h1>WebView onPrompt Demo</h1>
    <button onclick="myFunction()">Click here</button>
    <p id="demo"></p>
    <script>
      function myFunction() {
        let message = prompt("Message info", "Hello World");
        if (message != null && message != "") {
          document.getElementById("demo").innerHTML = message;
        }
      }
    </script>
  </body>
  </html>
  ```

1595
### onConsole
1596

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

E
ester.zhou 已提交
1599
Called to notify the host application of a JavaScript console message.
1600 1601

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

1603 1604 1605
| Name    | Type                             | Description     |
| ------- | --------------------------------- | --------- |
| message | [ConsoleMessage](#consolemessage) | Console message.|
1606 1607

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

1609 1610 1611
| Type     | Description                                 |
| ------- | ----------------------------------- |
| boolean | Returns **true** if the message will not be printed to the console; returns **false** otherwise.|
1612 1613 1614 1615 1616

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1617 1618
  import web_webview from '@ohos.web.webview'

1619 1620 1621
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1622
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1623

1624 1625 1626
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1627 1628 1629 1630 1631 1632
          .onConsole((event) => {
            console.log('getMessage:' + event.message.getMessage())
            console.log('getSourceId:' + event.message.getSourceId())
            console.log('getLineNumber:' + event.message.getLineNumber())
            console.log('getMessageLevel:' + event.message.getMessageLevel())
            return false
1633 1634 1635 1636 1637 1638
          })
      }
    }
  }
  ```

1639
### onDownloadStart
1640

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

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

1645 1646 1647
| Name               | Type         | Description                               |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | URL for the download task.                          |
E
ester.zhou 已提交
1648
| userAgent          | string        | User agent used for download.                          |
1649 1650 1651
| contentDisposition | string        | Content-Disposition response header returned by the server, which may be empty.|
| mimetype           | string        | MIME type of the content returned by the server.               |
| contentLength      | contentLength | Length of the content returned by the server.                        |
E
ester.zhou 已提交
1652 1653

**Example**
1654 1655 1656

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1657 1658
  import web_webview from '@ohos.web.webview'

1659 1660 1661
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1662
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1663

1664 1665
    build() {
      Column() {
E
ester.zhou 已提交
1666
        Web({ src: 'www.example.com', controller: this.controller })
1667 1668 1669 1670 1671 1672
          .onDownloadStart((event) => {
            console.log('url:' + event.url)
            console.log('userAgent:' + event.userAgent)
            console.log('contentDisposition:' + event.contentDisposition)
            console.log('contentLength:' + event.contentLength)
            console.log('mimetype:' + event.mimetype)
1673 1674 1675 1676 1677 1678
          })
      }
    }
  }
  ```

1679
### onErrorReceive
1680

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

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

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

1687 1688 1689 1690
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| error   | [WebResourceError](#webresourceerror)    | Encapsulation of a web page resource loading error.|
1691 1692 1693 1694 1695

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1696 1697
  import web_webview from '@ohos.web.webview'

1698 1699 1700
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1701
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1702

1703 1704
    build() {
      Column() {
E
ester.zhou 已提交
1705
        Web({ src: 'www.example.com', controller: this.controller })
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
          .onErrorReceive((event) => {
            console.log('getErrorInfo:' + event.error.getErrorInfo())
            console.log('getErrorCode:' + event.error.getErrorCode())
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
            }
          })
      }
    }
E
ester.zhou 已提交
1722 1723 1724
  }
  ```

1725
### onHttpErrorReceive
E
ester.zhou 已提交
1726

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

E
ester.zhou 已提交
1729
Called when an HTTP error (the response code is greater than or equal to 400) occurs during web page resource loading.
E
ester.zhou 已提交
1730 1731 1732

**Parameters**

1733 1734 1735 1736
| Name    | Type                                    | Description           |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request.     |
| response | [WebResourceResponse](#webresourceresponse)    | Encapsulation of a resource response.|
E
ester.zhou 已提交
1737 1738 1739 1740 1741

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1742 1743
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1744 1745 1746
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1747
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1748

E
ester.zhou 已提交
1749 1750 1751
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
          .onHttpErrorReceive((event) => {
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getResponseData:' + event.response.getResponseData())
            console.log('getResponseEncoding:' + event.response.getResponseEncoding())
            console.log('getResponseMimeType:' + event.response.getResponseMimeType())
            console.log('getResponseCode:' + event.response.getResponseCode())
            console.log('getReasonMessage:' + event.response.getReasonMessage())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
            for (let i of resph) {
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
E
ester.zhou 已提交
1772 1773 1774 1775 1776 1777
          })
      }
    }
  }
  ```

1778
### onPageBegin
E
ester.zhou 已提交
1779

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

1782

E
ester.zhou 已提交
1783
Called when the web page starts to be loaded. This API is called only for the main frame content, and not for the iframe or frameset content.
E
ester.zhou 已提交
1784 1785 1786

**Parameters**

1787 1788 1789 1790 1791
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|

**Example**
E
ester.zhou 已提交
1792 1793 1794

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1795 1796
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1797 1798 1799
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1800
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1801

E
ester.zhou 已提交
1802 1803 1804
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1805 1806
          .onPageBegin((event) => {
            console.log('url:' + event.url)
E
ester.zhou 已提交
1807 1808
          })
      }
1809 1810 1811
    }
  }
  ```
E
ester.zhou 已提交
1812

1813
### onPageEnd
1814

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

1817

E
ester.zhou 已提交
1818
Called when the web page loading is complete. This API takes effect only for the main frame content.
1819 1820

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

1822 1823 1824
| Name | Type  | Description     |
| ---- | ------ | --------- |
| url  | string | URL of the page.|
1825

E
ester.zhou 已提交
1826 1827 1828 1829
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1830 1831
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1832 1833 1834
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1835
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1836

E
ester.zhou 已提交
1837 1838 1839
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1840 1841
          .onPageEnd((event) => {
            console.log('url:' + event.url)
E
ester.zhou 已提交
1842 1843 1844 1845 1846 1847
          })
      }
    }
  }
  ```

1848
### onProgressChange
E
ester.zhou 已提交
1849

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

E
ester.zhou 已提交
1852
Called when the web page loading progress changes.
E
ester.zhou 已提交
1853 1854 1855

**Parameters**

1856 1857 1858
| Name        | Type  | Description                 |
| ----------- | ------ | --------------------- |
| newProgress | number | New loading progress. The value is an integer ranging from 0 to 100.|
E
ester.zhou 已提交
1859 1860 1861 1862 1863

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1864 1865
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1866 1867 1868
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1869
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1870

E
ester.zhou 已提交
1871 1872 1873
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1874 1875 1876
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
E
ester.zhou 已提交
1877 1878 1879 1880 1881
      }
    }
  }
  ```

1882
### onTitleReceive
E
ester.zhou 已提交
1883

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

E
ester.zhou 已提交
1886
Called when the document title of the web page is changed.
E
ester.zhou 已提交
1887 1888 1889

**Parameters**

1890 1891 1892
| Name  | Type  | Description         |
| ----- | ------ | ------------- |
| title | string | Document title.|
E
ester.zhou 已提交
1893 1894 1895 1896 1897

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1898 1899
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1900 1901 1902
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1903
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1904

E
ester.zhou 已提交
1905 1906 1907
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1908 1909 1910
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
E
ester.zhou 已提交
1911 1912 1913 1914 1915
      }
    }
  }
  ```

1916
### onRefreshAccessedHistory
E
ester.zhou 已提交
1917

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

E
ester.zhou 已提交
1920
Called when loading of the web page is complete. This API is used by an application to update the historical link it accessed.
E
ester.zhou 已提交
1921 1922 1923

**Parameters**

1924 1925 1926
| Name        | Type   | Description                                    |
| ----------- | ------- | ---------------------------------------- |
| url         | string  | URL to be accessed.                                 |
E
ester.zhou 已提交
1927
| isRefreshed | boolean | Whether the page is reloaded. The value **true** means that the page is reloaded by invoking the [refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh) API, and **false** means the opposite.|
E
ester.zhou 已提交
1928 1929 1930 1931 1932

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1933 1934
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1935 1936 1937
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1938
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1939

E
ester.zhou 已提交
1940 1941
    build() {
      Column() {
1942 1943 1944
        Web({ src: 'www.example.com', controller: this.controller })
          .onRefreshAccessedHistory((event) => {
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
E
ester.zhou 已提交
1945 1946 1947 1948 1949 1950
          })
      }
    }
  }
  ```

1951
### onRenderExited<sup>9+</sup>
E
ester.zhou 已提交
1952

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

E
ester.zhou 已提交
1955
Called when the rendering process exits abnormally.
E
ester.zhou 已提交
1956 1957 1958

**Parameters**

1959 1960 1961
| Name             | Type                                    | Description            |
| ---------------- | ---------------------------------------- | ---------------- |
| renderExitReason | [RenderExitReason](#renderexitreason)| Cause for the abnormal exit of the rendering process.|
E
ester.zhou 已提交
1962 1963 1964 1965 1966

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
1967 1968
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
1969 1970 1971
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
1972
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
1973

E
ester.zhou 已提交
1974 1975
    build() {
      Column() {
1976 1977 1978 1979
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
            console.log('reason:' + event.renderExitReason)
          })
E
ester.zhou 已提交
1980 1981 1982 1983 1984
      }
    }
  }
  ```

1985
### onShowFileSelector<sup>9+</sup>
E
ester.zhou 已提交
1986

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

E
ester.zhou 已提交
1989
Called to process an HTML form whose input type is **file**, in response to the tapping of the **Select File** button.
E
ester.zhou 已提交
1990 1991 1992

**Parameters**

1993 1994 1995 1996 1997 1998 1999 2000 2001
| Name         | Type                                    | Description             |
| ------------ | ---------------------------------------- | ----------------- |
| result       | [FileSelectorResult](#fileselectorresult9) | File selection result to be sent to the **\<Web>** component.|
| fileSelector | [FileSelectorParam](#fileselectorparam9) | Information about the file selector.      |

**Return value**

| Type     | Description                                      |
| ------- | ---------------------------------------- |
E
ester.zhou 已提交
2002
| boolean | The value **true** means that the pop-up window provided by the system is displayed. If the callback returns **false**, the **\<Web>** component cannot trigger the system dialog box.|
E
ester.zhou 已提交
2003 2004 2005 2006 2007

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2008 2009
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
2010 2011 2012
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2013
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2014

E
ester.zhou 已提交
2015 2016
    build() {
      Column() {
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
        Web({ src: 'www.example.com', controller: this.controller })
          .onShowFileSelector((event) => {
            AlertDialog.show({
              title: event.fileSelector.getTitle(),
              message: 'isCapture:' + event.fileSelector.isCapture() + " mode:" + event.fileSelector.getMode() + 'acceptType:' + event.fileSelector.getAcceptType(),
              confirm: {
                value: 'upload',
                action: () => {
                  let fileList: Array<string> = [
                    '/data/storage/el2/base/test',
                  ]
                  event.result.handleFileList(fileList)
                }
              },
              cancel: () => {
                let fileList: Array<string> = []
                event.result.handleFileList(fileList)
              }
            })
            return true
          })
E
ester.zhou 已提交
2038 2039 2040 2041 2042
      }
    }
  }
  ```

2043
### onResourceLoad<sup>9+</sup>
E
ester.zhou 已提交
2044

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

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

E
ester.zhou 已提交
2049
**Parameters**
E
ester.zhou 已提交
2050

2051 2052 2053
| Name | Type  | Description          |
| ---- | ------ | -------------- |
| url  | string | URL of the loaded resource file.|
E
ester.zhou 已提交
2054 2055 2056 2057 2058

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2059 2060
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
2061 2062 2063
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2064
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
2065

E
ester.zhou 已提交
2066 2067
    build() {
      Column() {
2068 2069 2070 2071
        Web({ src: 'www.example.com', controller: this.controller })
          .onResourceLoad((event) => {
            console.log('onResourceLoad: ' + event.url)
          })
E
ester.zhou 已提交
2072 2073 2074 2075 2076
      }
    }
  }
  ```

2077
### onScaleChange<sup>9+</sup>
E
ester.zhou 已提交
2078

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

E
ester.zhou 已提交
2081
Called when the display ratio of this page changes.
E
ester.zhou 已提交
2082 2083 2084

**Parameters**

2085 2086 2087 2088
| Name     | Type  | Description        |
| -------- | ------ | ------------ |
| oldScale | number | Display ratio of the page before the change.|
| newScale | number | Display ratio of the page after the change.|
E
ester.zhou 已提交
2089 2090 2091 2092 2093

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2094 2095
  import web_webview from '@ohos.web.webview'

E
ester.zhou 已提交
2096 2097 2098
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2099
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
2100

E
ester.zhou 已提交
2101 2102
    build() {
      Column() {
2103 2104 2105 2106
        Web({ src: 'www.example.com', controller: this.controller })
          .onScaleChange((event) => {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
          })
E
ester.zhou 已提交
2107 2108 2109 2110 2111
      }
    }
  }
  ```

E
ester.zhou 已提交
2112
### onUrlLoadIntercept
E
ester.zhou 已提交
2113

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

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

**Parameters**

2120 2121 2122 2123 2124 2125 2126 2127 2128
| Name | Type                                    | Description     |
| ---- | ---------------------------------------- | --------- |
| data | string / [WebResourceRequest](#webresourcerequest) | URL information.|

**Return value**

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

2130 2131 2132 2133
**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2134 2135
  import web_webview from '@ohos.web.webview'

2136 2137 2138
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2139
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
2140

2141 2142
    build() {
      Column() {
2143 2144 2145 2146 2147
        Web({ src: 'www.example.com', controller: this.controller })
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
            return true
          })
E
ester.zhou 已提交
2148
      }
2149 2150 2151 2152
    }
  }
  ```

2153
### onInterceptRequest<sup>9+</sup>
E
ester.zhou 已提交
2154

2155
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
2156

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

2159
**Parameters**
2160

2161 2162 2163
| Name    | Type                                    | Description       |
| ------- | ---------------------------------------- | ----------- |
| request | [WebResourceRequest](#webresourcerequest) | Information about the URL request.|
2164 2165

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

2167 2168 2169
| Type                                      | Description                                      |
| ---------------------------------------- | ---------------------------------------- |
| [WebResourceResponse](#webresourceresponse) | If response data is returned, the data is loaded based on the response data. If no response data is returned, null is returned, indicating that the data is loaded in the original mode.|
2170

2171
**Example**
2172

2173 2174
  ```ts
  // xxx.ets
E
ester.zhou 已提交
2175 2176
  import web_webview from '@ohos.web.webview'

2177 2178 2179
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2180
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
    @State webdata: string = "<!DOCTYPE html>\n" +
    "<html>\n"+
    "<head>\n"+
    "<title>intercept test</title>\n"+
    "</head>\n"+
    "<body>\n"+
    "<h1>intercept test</h1>\n"+
    "</body>\n"+
    "</html>"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onInterceptRequest((event) => {
            console.log('url:' + event.request.getRequestUrl())
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
            var length = this.heads.push(head1)
            length = this.heads.push(head2)
            this.responseweb.setResponseHeader(this.heads)
            this.responseweb.setResponseData(this.webdata)
            this.responseweb.setResponseEncoding('utf-8')
            this.responseweb.setResponseMimeType('text/html')
            this.responseweb.setResponseCode(200)
            this.responseweb.setReasonMessage('OK')
            return this.responseweb
          })
      }
    }
  }
  ```
2219

2220
### onHttpAuthRequest<sup>9+</sup>
E
ester.zhou 已提交
2221

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

E
ester.zhou 已提交
2224
Called when an HTTP authentication request is received.
2225

2226
**Parameters**
2227

2228 2229 2230 2231 2232
| Name    | Type                                | Description            |
| ------- | ------------------------------------ | ---------------- |
| handler | [HttpAuthHandler](#httpauthhandler9) | User operation.  |
| host    | string                               | Host to which HTTP authentication credentials apply.|
| realm   | string                               | Realm to which HTTP authentication credentials apply. |
2233 2234

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

2236 2237 2238
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
2239

2240
**Example**
2241

2242 2243 2244 2245 2246 2247
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2248
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2249
    httpAuth: boolean = false
E
ester.zhou 已提交
2250

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
              title: 'onHttpAuthRequest',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.handler.cancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
                  if (this.httpAuth == false) {
                    web_webview.WebDataBase.saveHttpAuthCredentials(
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
                    event.handler.cancel()
                  }
                }
              },
              cancel: () => {
                event.handler.cancel()
              }
            })
            return true
          })
      }
    }
  }
  ```
### onSslErrorEventReceive<sup>9+</sup>
2290

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

E
ester.zhou 已提交
2293
Called when an SSL error occurs during resource loading.
2294

2295
**Parameters**
2296

2297 2298 2299 2300
| Name    | Type                                | Description          |
| ------- | ------------------------------------ | -------------- |
| handler | [SslErrorHandler](#sslerrorhandler9) | User operation.|
| error   | [SslError](#sslerror9)          | Error code.          |
2301

2302
**Example**
2303

2304 2305 2306 2307 2308 2309
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2310
    controller: web_webview.WebviewController = new web_webview.WebviewController()
E
ester.zhou 已提交
2311

2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onSslErrorEventReceive((event) => {
            AlertDialog.show({
              title: 'onSslErrorEventReceive',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
                  event.handler.handleConfirm()
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
                  event.handler.handleCancel()
                }
              },
              cancel: () => {
                event.handler.handleCancel()
              }
            })
            return true
          })
      }
    }
  }
  ```

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

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

E
ester.zhou 已提交
2346
Called when an SSL client certificate request is received.
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

**Parameters**

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

  **Example**
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2365
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400

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

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

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

E
ester.zhou 已提交
2401
Called when a permission request is received.
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2413 2414
  import web_webview from '@ohos.web.webview'

2415 2416 2417
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2418
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              primaryButton: {
                value: 'deny',
                action: () => {
                  event.request.deny()
                }
              },
              secondaryButton: {
                value: 'onConfirm',
                action: () => {
                  event.request.grant(event.request.getAccessibleResource())
                }
              },
              cancel: () => {
                event.request.deny()
              }
            })
          })
      }
    }
  }
  ```

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

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

E
ester.zhou 已提交
2452
Called when a context menu is displayed after the user clicks the right mouse button or long presses a specific element, such as an image or a link.
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

**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
E
ester.zhou 已提交
2471 2472
  import web_webview from '@ohos.web.webview'

2473 2474 2475
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2476
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
    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)

E
ester.zhou 已提交
2494
Called when the scrollbar of the page scrolls.
2495 2496 2497 2498 2499

**Parameters**

| Name    | Type  | Description        |
| ------- | ------ | ------------ |
E
ester.zhou 已提交
2500 2501
| xOffset | number | Position of the scrollbar on the x-axis relative to the leftmost of the web page.|
| yOffset | number | Position of the scrollbar on the y-axis relative to the top of the web page.|
2502 2503 2504 2505 2506

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2507 2508
  import web_webview from '@ohos.web.webview'

2509 2510 2511
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2512
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
    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)

E
ester.zhou 已提交
2529
Called when a request to obtain the geolocation information is received.
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2542 2543
  import web_webview from '@ohos.web.webview'

2544 2545 2546
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2547
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
    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)

E
ester.zhou 已提交
2576
Called to notify the user that the request for obtaining the geolocation information received when **[onGeolocationShow](#ongeolocationshow)** is called has been canceled.
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2588 2589
  import web_webview from '@ohos.web.webview'

2590 2591 2592
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2593
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
    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)

E
ester.zhou 已提交
2610
Called when the component enters full screen mode.
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2622 2623
  import web_webview from '@ohos.web.webview'

2624 2625 2626
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2627
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
    handler: FullScreenExitHandler = null
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenEnter((event) => {
          console.log("onFullScreenEnter...")
          this.handler = event.handler
        })
      }
    }
  }
  ```

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

onFullScreenExit(callback: () => void)

E
ester.zhou 已提交
2645
Called when the component exits full screen mode.
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2657 2658
  import web_webview from '@ohos.web.webview'

2659 2660 2661
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2662
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
    handler: FullScreenExitHandler = null
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenExit(() => {
          console.log("onFullScreenExit...")
          this.handler.exitFullScreen()
        })
        .onFullScreenEnter((event) => {
          this.handler = event.handler
        })
      }
    }
  }
  ```

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

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

2683 2684 2685
Called when a new window is created. This API takes effect when **multiWindowAccess** is enabled.
If the **event.handler.setWebController** API is not called, the render process will be blocked.
If opening a new window is not needed, set the parameter to **null** when calling the **event.handler.setWebController** API.
2686 2687 2688 2689 2690 2691 2692 2693

**Parameters**

| Name          | Type                                    | Description                      |
| ------------- | ---------------------------------------- | -------------------------- |
| isAlert       | boolean                                  | Whether to open the target URL in a new window. The value **true** means to open the target URL in a new window, and **false** means to open the target URL in a new tab.|
| isUserTrigger | boolean                                  | Whether the creation is triggered by the user. The value **true** means that the creation is triggered by the user, and **false** means the opposite.  |
| targetUrl     | string                                   | Target URL.                    |
E
ester.zhou 已提交
2694
| handler       | [ControllerHandler](#controllerhandler9) | **WebviewController** instance for setting the new window. |
2695 2696 2697 2698 2699 2700

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
  
  // There are two <Web> components on the same page. When the WebComponent object opens a new window, the NewWebViewComp object is displayed. 
  @CustomDialog
  struct NewWebViewComp {
  controller: CustomDialogController
  webviewController1: web_webview.WebviewController
  build() {
      Column() {
        Web({ src: "", controller: this.webviewController1 })
          .javaScriptAccess(true)
          .multiWindowAccess(false)
          .onWindowExit(()=> {
            console.info("NewWebViewComp onWindowExit")
            this.controller.close()
          })
        }
    }
  }

2720 2721 2722 2723
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2724
    dialogController: CustomDialogController = null
2725 2726
    build() {
      Column() {
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          // MultiWindowAccess needs to be enabled.
          .multiWindowAccess(true)
          .onWindowNew((event) => {
            if (this.dialogController) {
              this.dialogController.close()
            }
            let popController:web_webview.WebviewController = new web_webview.WebviewController()
            this.dialogController = new CustomDialogController({
              builder: NewWebViewComp({webviewController1: popController})
            })
            this.dialogController.open()
            // Return the WebviewController object corresponding to the new window to the <Web> kernel.
            // If opening a new window is not needed, set the parameter to null when calling the event.handler.setWebController API.
            // If the event.handler.setWebController API is not called, the render process will be blocked.
            event.handler.setWebController(popController)
          })
2745 2746 2747 2748 2749 2750 2751 2752 2753
      }
    }
  }
  ```

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

onWindowExit(callback: () => void)

2754
Called when this window is closed.
2755 2756 2757 2758 2759

**Parameters**

| Name     | Type      | Description        |
| -------- | ---------- | ------------ |
2760
| callback | () => void | Callback invoked when the window is closed.|
2761 2762 2763 2764 2765

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2766 2767
  import web_webview from '@ohos.web.webview'

2768 2769 2770
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2771
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
        .onWindowExit(() => {
          console.log("onWindowExit...")
        })
      }
    }
  }
  ```

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

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

E
ester.zhou 已提交
2787
Called to notify the caller of the search result on the web page.
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
E
ester.zhou 已提交
2801 2802
  import web_webview from '@ohos.web.webview'

2803 2804 2805
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
2806
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823

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

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

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

E
ester.zhou 已提交
2824
Called when the web form data is resubmitted.
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856

**Parameters**

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

**Example**

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

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

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

E
ester.zhou 已提交
2857
Called when the old page is not displayed and the new page is about to be visible.
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888

**Parameters**

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

**Example**

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

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

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

2889
Called when the key event is intercepted and before it is consumed by the webview.
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900

**Parameters**

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

**Return value**

| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
2901
| boolean | Whether to continue to transfer the key event to the webview kernel.|
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onInterceptKeyEvent((event) => {
E
ester.zhou 已提交
2916
          if (event.keyCode == 2017 || event.keyCode == 2018) {
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
            console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`)
            return true;
          }
          return false;
        })
      }
    }
  }
  ```

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

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

E
ester.zhou 已提交
2931
Called when an apple-touch-icon URL is received.
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963

**Parameters**

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

**Example**

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

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

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

E
ester.zhou 已提交
2964
Called when this web page receives a new favicon.
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986

**Parameters**

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

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  import image from "@ohos.multimedia.image"
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State icon: image.PixelMap = undefined;
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onFaviconReceived((event) => {
E
ester.zhou 已提交
2987
          console.log('onFaviconReceived');
2988 2989 2990 2991 2992 2993 2994
          this.icon = event.favicon;
        })
      }
    }
  }
  ```

E
ester.zhou 已提交
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
### onRequestSelected

onRequestSelected(callback: () => void)

Called when the **\<Web>** component obtains the focus.

**Example**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onRequestSelected(() => {
            console.log('onRequestSelected')
          })
      }
    }
  }
  ```

3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
## ConsoleMessage

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

### getLineNumber

getLineNumber(): number

Obtains the number of rows in this console message.

**Return value**

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

### getMessage

getMessage(): string

Obtains the log information of this console message.

**Return value**

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

### getMessageLevel

getMessageLevel(): MessageLevel

Obtains the level of this console message.

**Return value**

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

### getSourceId

getSourceId(): string

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

**Return value**

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

## JsResult

E
ester.zhou 已提交
3077
Implements the **JsResult** object, which indicates the result returned to the **\<Web>** component to indicate the user operation performed in the dialog box. For the sample code, see [onAlert](#onalert).
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091

### 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>
3092 3093 3094 3095 3096 3097

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

3099 3100 3101 3102
| Name   | Type  | Mandatory  | Default Value | Description       |
| ------ | ------ | ---- | ---- | ----------- |
| result | string | Yes   | -    | User input in the dialog box.|

E
ester.zhou 已提交
3103 3104
## FullScreenExitHandler<sup>9+</sup>

3105
Implements a **FullScreenExitHandler** object for listening for exiting full screen mode. For the sample code, see [onFullScreenEnter](#onfullscreenenter9).
E
ester.zhou 已提交
3106 3107 3108 3109 3110 3111 3112 3113 3114

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

exitFullScreen(): void

Exits full screen mode.

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

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

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

3119
setWebController(controller: WebviewController): void
E
ester.zhou 已提交
3120

3121
Sets a **WebviewController** object. If opening a new window is not needed, set the parameter to **null**.
E
ester.zhou 已提交
3122 3123 3124

**Parameters**

3125 3126
| Name       | Type         | Mandatory  | Default Value | Description                     |
| ---------- | ------------- | ---- | ---- | ------------------------- |
3127
| controller | [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | Yes   | -    | **WebviewController** object of the **\<Web>** component. If opening a new window is not needed, set it to **null**.|
E
ester.zhou 已提交
3128

3129 3130
## WebResourceError

3131
Implements the **WebResourceError** object. For the sample code, see [onErrorReceive](#onerrorreceive).
3132 3133 3134 3135 3136 3137 3138 3139

### getErrorCode

getErrorCode(): number

Obtains the error code for resource loading.

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

3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
| Type    | Description         |
| ------ | ----------- |
| number | Error code for resource loading.|

### getErrorInfo

getErrorInfo(): string

Obtains error information about resource loading.

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

3153 3154 3155 3156 3157 3158
| Type    | Description          |
| ------ | ------------ |
| string | Error information about resource loading.|

## WebResourceRequest

3159
Implements the **WebResourceRequest** object. For the sample code, see [onErrorReceive](#onerrorreceive).
3160 3161 3162 3163 3164 3165 3166 3167

### getRequestHeader

getResponseHeader() : Array\<Header\>

Obtains the information about the resource request header.

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

3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
| 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 已提交
3180

3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
| 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 已提交
3192

3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
| 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 已提交
3204

3205 3206
| Type     | Description              |
| ------- | ---------------- |
E
ester.zhou 已提交
3207
| boolean | Whether the resource request is redirected by the server.|
3208 3209 3210 3211 3212 3213 3214 3215

### isRequestGesture

isRequestGesture(): boolean

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

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

3217 3218
| Type     | Description                  |
| ------- | -------------------- |
E
ester.zhou 已提交
3219
| boolean | Whether the resource request is associated with a gesture (for example, a tap).|
3220

3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
### getRequestMethod<sup>9+</sup>

getRequestMethod(): string

Obtains the request method.

**Return value**

| Type     | Description                  |
| ------- | -------------------- |
| string | Request method.|

3233
## Header
E
ester.zhou 已提交
3234

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

3237 3238 3239 3240
| Name         | Type    | Description           |
| ----------- | ------ | ------------- |
| headerKey   | string | Key of the request/response header.  |
| headerValue | string | Value of the request/response header.|
E
ester.zhou 已提交
3241 3242


3243
## WebResourceResponse
E
ester.zhou 已提交
3244

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

3247
### getReasonMessage
E
ester.zhou 已提交
3248

3249
getReasonMessage(): string
E
ester.zhou 已提交
3250

3251
Obtains the status code description of the resource response.
E
ester.zhou 已提交
3252

3253
**Return value**
E
ester.zhou 已提交
3254

3255 3256 3257
| Type    | Description           |
| ------ | ------------- |
| string | Status code description of the resource response.|
E
ester.zhou 已提交
3258

3259
### getResponseCode
E
ester.zhou 已提交
3260

3261
getResponseCode(): number
E
ester.zhou 已提交
3262

3263
Obtains the status code of the resource response.
E
ester.zhou 已提交
3264

3265
**Return value**
E
ester.zhou 已提交
3266

3267 3268 3269
| Type    | Description         |
| ------ | ----------- |
| number | Status code of the resource response.|
E
ester.zhou 已提交
3270

3271
### getResponseData
E
ester.zhou 已提交
3272

3273
getResponseData(): string
E
ester.zhou 已提交
3274

3275
Obtains the data in the resource response.
E
ester.zhou 已提交
3276

3277
**Return value**
E
ester.zhou 已提交
3278

3279 3280 3281
| Type    | Description       |
| ------ | --------- |
| string | Data in the resource response.|
E
ester.zhou 已提交
3282

3283
### getResponseEncoding
E
ester.zhou 已提交
3284

3285 3286 3287 3288 3289
getResponseEncoding(): string

Obtains the encoding string of the resource response.

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

3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
| Type    | Description        |
| ------ | ---------- |
| string | Encoding string of the resource response.|

### getResponseHeader

getResponseHeader() : Array\<Header\>

Obtains the resource response header.

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

3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
| Type                        | Description      |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | Resource response header.|

### getResponseMimeType

getResponseMimeType(): string

Obtains the MIME type of the resource response.

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

3315 3316 3317 3318 3319 3320
| Type    | Description                |
| ------ | ------------------ |
| string | MIME type of the resource response.|

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

3321
setResponseData(data: string | number)
3322 3323 3324 3325

Sets the data in the resource response.

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

3327 3328 3329
| Name| Type        | Mandatory| Default Value| Description                                                    |
| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ |
| data   | string \| number | Yes  | -      | Resource response data to set. When set to a number, the value indicates a file handle.|
3330 3331 3332 3333 3334 3335 3336 3337

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

setResponseEncoding(encoding: string)

Sets the encoding string of the resource response.

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

3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
| 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 已提交
3350

3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
| 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 已提交
3362

3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
| 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 已提交
3374

3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
| 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 已提交
3386

3387 3388 3389 3390
| Name | Type  | Mandatory  | Default Value | Description         |
| ---- | ------ | ---- | ---- | ------------- |
| code | number | Yes   | -    | Status code to set.|

3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
### setResponseIsReady<sup>9+</sup>

setResponseIsReady(IsReady: boolean)

Sets whether the resource response data is ready.

**Parameters**

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

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

3405
Notifies the **\<Web>** component of the file selection result. For the sample code, see [onShowFileSelector](#onshowfileselector9).
3406 3407 3408 3409 3410 3411 3412 3413

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

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

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

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

3415 3416 3417 3418 3419 3420
| Name     | Type           | Mandatory  | Default Value | Description        |
| -------- | --------------- | ---- | ---- | ------------ |
| fileList | Array\<string\> | Yes   | -    | List of files to operate.|

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

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

E
ester.zhou 已提交
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
### getTitle<sup>9+</sup>

getTitle(): string

Obtains the title of this file selector.

**Return value**

| Type    | Description      |
| ------ | -------- |
| string | Title of the file selector.|

3435 3436 3437 3438 3439 3440 3441
### getMode<sup>9+</sup>

getMode(): FileSelectorMode

Obtains the mode of the file selector.

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

3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode)| Mode of the file selector.|

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

getAcceptType(): Array\<string\>

Obtains the file filtering type.

3453
**Return value**
E
ester.zhou 已提交
3454

3455 3456 3457
| Type             | Description       |
| --------------- | --------- |
| Array\<string\> | File filtering type.|
E
ester.zhou 已提交
3458

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

3461
isCapture(): boolean
3462

3463
Checks whether multimedia capabilities are invoked.
3464

3465
**Return value**
E
ester.zhou 已提交
3466

3467 3468 3469
| Type     | Description          |
| ------- | ------------ |
| boolean | Whether multimedia capabilities are invoked.|
3470

3471
## HttpAuthHandler<sup>9+</sup>
E
ester.zhou 已提交
3472

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

3475
### cancel<sup>9+</sup>
E
ester.zhou 已提交
3476

3477
cancel(): void
E
ester.zhou 已提交
3478

3479
Cancels HTTP authentication as requested by the user.
3480

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

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

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

3487
**Parameters**
E
ester.zhou 已提交
3488

3489 3490 3491 3492
| Name     | Type  | Mandatory  | Default Value | Description      |
| -------- | ------ | ---- | ---- | ---------- |
| userName | string | Yes   | -    | HTTP authentication user name.|
| pwd      | string | Yes   | -    | HTTP authentication password. |
E
ester.zhou 已提交
3493

3494
**Return value**
E
ester.zhou 已提交
3495

3496 3497
| Type     | Description                   |
| ------- | --------------------- |
3498
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
3499

3500
### isHttpAuthInfoSaved<sup>9+</sup>
E
ester.zhou 已提交
3501

3502
isHttpAuthInfoSaved(): boolean
3503

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

3506
**Return value**
E
ester.zhou 已提交
3507

3508 3509 3510
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.|
E
ester.zhou 已提交
3511

3512
## SslErrorHandler<sup>9+</sup>
3513

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

3516
### handleCancel<sup>9+</sup>
E
ester.zhou 已提交
3517

3518
handleCancel(): void
3519

3520
Cancels this request.
E
ester.zhou 已提交
3521

3522
### handleConfirm<sup>9+</sup>
E
ester.zhou 已提交
3523

3524
handleConfirm(): void
E
ester.zhou 已提交
3525

3526
Continues using the SSL certificate.
E
ester.zhou 已提交
3527

3528
## ClientAuthenticationHandler<sup>9+</sup>
E
ester.zhou 已提交
3529

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

3532
### confirm<sup>9+</sup>
E
ester.zhou 已提交
3533

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

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

3538
**Parameters**
E
ester.zhou 已提交
3539

3540 3541 3542 3543
| Name          | Type  | Mandatory  | Description              |
| ------------- | ------ | ---- | ------------------ |
| priKeyFile    | string | Yes   | File that stores the private key, which is a directory including the file name. |
| certChainFile | string | Yes   | File that stores the certificate chain, which is a directory including the file name.|
E
ester.zhou 已提交
3544

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

3547
cancel(): void
3548

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

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

3553
ignore(): void
E
ester.zhou 已提交
3554

3555
Ignores this request.
3556

3557
## PermissionRequest<sup>9+</sup>
E
ester.zhou 已提交
3558

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

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

3563
deny(): void
3564

3565
Denies the permission requested by the web page.
3566

3567
### getOrigin<sup>9+</sup>
E
ester.zhou 已提交
3568

3569
getOrigin(): string
3570

3571
Obtains the origin of this web page.
E
ester.zhou 已提交
3572

3573
**Return value**
3574

3575 3576 3577
| Type    | Description          |
| ------ | ------------ |
| string | Origin of the web page that requests the permission.|
E
ester.zhou 已提交
3578

3579
### getAccessibleResource<sup>9+</sup>
E
ester.zhou 已提交
3580

3581 3582 3583
getAccessibleResource(): Array\<string\>

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

3585
**Return value**
E
ester.zhou 已提交
3586

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

3591
### grant<sup>9+</sup>
E
ester.zhou 已提交
3592

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

3595
Grants the permission for resources requested by the web page.
3596

3597
**Parameters**
3598

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

3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
## ContextMenuSourceType<sup>9+</sup>
| Name                  | Description        |
| -------------------- | ---------- |
| None        | Other event sources. |
| Mouse       | Mouse event. |
| LongPress   | Long press event. |

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

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

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

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

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

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

3638
## WebContextMenuParam<sup>9+</sup>
E
ester.zhou 已提交
3639

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

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

3644
x(): number
3645

3646
Obtains the X coordinate of the context menu.
E
ester.zhou 已提交
3647

3648
**Return value**
3649

3650 3651 3652
| Type    | Description                |
| ------ | ------------------ |
| number | If the display is normal, a non-negative integer is returned. Otherwise, **-1** is returned.|
E
ester.zhou 已提交
3653

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

3656
y(): number
3657

3658
Obtains the Y coordinate of the context menu.
3659 3660

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

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

3666
### getLinkUrl<sup>9+</sup>
E
ester.zhou 已提交
3667

3668
getLinkUrl(): string
3669

3670
Obtains the URL of the destination link.
3671

3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
**Return value**

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

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

getUnfilteredLinkUrl(): string

Obtains the URL of the destination link.
3683 3684

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

3686 3687 3688
| Type    | Description                   |
| ------ | --------------------- |
| string | If it is a link that is being long pressed, the original URL is returned.|
3689

3690
### getSourceUrl<sup>9+</sup>
E
ester.zhou 已提交
3691

3692
getSourceUrl(): string
3693

3694
Obtain the source URL.
3695

3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
**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.
3707 3708

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

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

3714
### getMediaType<sup>9+</sup>
E
ester.zhou 已提交
3715

3716
getMediaType(): ContextMenuMediaType
E
ester.zhou 已提交
3717

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

3720
**Return value**
E
ester.zhou 已提交
3721

3722 3723 3724
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuMediaType](#contextmenumediatype9) | Media type of the web page element.|
E
ester.zhou 已提交
3725

3726
### getSelectionText<sup>9+</sup>
E
ester.zhou 已提交
3727

3728
getSelectionText(): string
E
ester.zhou 已提交
3729

3730
Obtains the selected text.
E
ester.zhou 已提交
3731

3732
**Return value**
3733

3734 3735 3736
| Type     | Description                       |
| ------- | ------------------------- |
| string | Selected text for the context menu. If no text is selected, null is returned.|
E
ester.zhou 已提交
3737

3738 3739 3740 3741 3742 3743 3744
### getSourceType<sup>9+</sup>

getSourceType(): ContextMenuSourceType

Obtains the event source of the context menu.

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

3746 3747 3748
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuSourceType](#contextmenusourcetype9) | Event source of the context menu.|
E
ester.zhou 已提交
3749

3750
### getInputFieldType<sup>9+</sup>
E
ester.zhou 已提交
3751

3752
getInputFieldType(): ContextMenuInputFieldType
E
ester.zhou 已提交
3753

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

3756
**Return value**
3757

3758 3759 3760
| Type                                      | Description         |
| ---------------------------------------- | ----------- |
| [ContextMenuInputFieldType](#contextmenuinputfieldtype9) | Input field type.|
E
ester.zhou 已提交
3761

3762
### isEditable<sup>9+</sup>
3763

3764
isEditable(): boolean
E
ester.zhou 已提交
3765

3766
Checks whether this web page element is editable.
E
ester.zhou 已提交
3767

3768
**Return value**
E
ester.zhou 已提交
3769

3770 3771 3772
| Type     | Description                       |
| ------- | ------------------------- |
| boolean | Returns **true** if the web page element is editable; returns **false** otherwise.|
E
ester.zhou 已提交
3773

3774
### getEditStateFlags<sup>9+</sup>
E
ester.zhou 已提交
3775

3776
getEditStateFlags(): number
E
ester.zhou 已提交
3777

3778
Obtains the edit state flag of this web page element.
3779

3780
**Return value**
E
ester.zhou 已提交
3781

3782 3783 3784
| Type     | Description                       |
| ------- | ------------------------- |
| number | Edit state flag of the web page element. For details, see [ContextMenuEditStateFlags](#contextmenueditstateflags9).|
E
ester.zhou 已提交
3785

3786
## WebContextMenuResult<sup>9+</sup>
E
ester.zhou 已提交
3787

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

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

3792
closeContextMenu(): void
3793

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

3796
### copyImage<sup>9+</sup>
E
ester.zhou 已提交
3797

3798
copyImage(): void
3799

3800
Copies the image specified in **WebContextMenuParam**.
E
ester.zhou 已提交
3801

3802
### copy<sup>9+</sup>
3803

3804
copy(): void
3805

3806
Performs the copy operation related to this context menu.
3807

3808
### paste<sup>9+</sup>
E
ester.zhou 已提交
3809

3810
paste(): void
3811

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

3814
### cut<sup>9+</sup>
3815

3816
cut(): void
3817

3818
Performs the cut operation related to this context menu.
3819

3820
### selectAll<sup>9+</sup>
E
ester.zhou 已提交
3821

3822
selectAll(): void
3823

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

3826
## JsGeolocation
3827

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

3830
### invoke
E
ester.zhou 已提交
3831

3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843
invoke(origin: string, allow: boolean, retain: boolean): void

Sets the geolocation permission status of a web page.

**Parameters**

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

E
ester.zhou 已提交
3844
## MessageLevel
3845

E
ester.zhou 已提交
3846 3847 3848 3849 3850 3851 3852
| Name   | Description   |
| ----- | :---- |
| Debug | Debug level.|
| Error | Error level.|
| Info  | Information level.|
| Log   | Log level.|
| Warn  | Warning level. |
3853

E
ester.zhou 已提交
3854
## RenderExitReason
3855

E
ester.zhou 已提交
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 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 3956 3957
Enumerates the reasons why the rendering process exits.

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

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

## ProtectedResourceType<sup>9+</sup>

| Name       | Description           | Remarks                        |
| --------- | ------------- | -------------------------- |
| MidiSysex | MIDI SYSEX resource.| Currently, only permission events can be reported. MIDI devices are not yet supported.|

## WebDarkMode<sup>9+</sup>
| Name     | Description                                  |
| ------- | ------------------------------------ |
| Off     | The web dark mode is disabled.                    |
| On      | The web dark mode is enabled.                    |
| Auto    | The web dark mode setting follows the system settings.                |

## DataResubmissionHandler<sup>9+</sup>

Implements the **DataResubmissionHandler** object for resubmitting or canceling the web form data.

### resend<sup>9+</sup>

resend(): void

Resends the web form data.

**Example**

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

E
ester.zhou 已提交
3958
### cancel<sup>9+</sup>
E
ester.zhou 已提交
3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988

cancel(): void

Cancels the resending of web form data.

**Example**

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

  ## WebController

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

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

### Creating an Object
3992 3993 3994 3995 3996

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

E
ester.zhou 已提交
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006
### getCookieManager<sup>9+</sup>

getCookieManager(): WebCookie

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

**Return value**

| Type       | Description                                      |
| --------- | ---------------------------------------- |
E
ester.zhou 已提交
4007
| WebCookie | Cookie management object. For details, see [WebCookie](#webcookiedeprecated).|
E
ester.zhou 已提交
4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029

**Example**

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

    build() {
      Column() {
        Button('getCookieManager')
          .onClick(() => {
            let cookieManager = this.controller.getCookieManager()
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4030 4031 4032 4033 4034 4035
### requestFocus<sup>(deprecated)</sup>

requestFocus()

Requests focus for this web page.

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

4038
**Example**
E
ester.zhou 已提交
4039

4040 4041 4042 4043 4044
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4045
    controller: WebController = new WebController()
E
ester.zhou 已提交
4046

4047 4048
    build() {
      Column() {
4049
        Button('requestFocus')
4050
          .onClick(() => {
4051
            this.controller.requestFocus()
4052 4053 4054 4055 4056 4057
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
4058

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

4061
accessBackward(): boolean
4062

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

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

4067 4068 4069 4070 4071
**Return value**

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

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

4075 4076 4077 4078
  ```ts
  // xxx.ets
  @Entry
  @Component
4079
  struct WebComponent {
4080
    controller: WebController = new WebController()
E
ester.zhou 已提交
4081

4082 4083
    build() {
      Column() {
4084 4085 4086 4087
        Button('accessBackward')
          .onClick(() => {
            let result = this.controller.accessBackward()
            console.log('result:' + result)
4088
          })
4089
        Web({ src: 'www.example.com', controller: this.controller })
4090 4091 4092 4093 4094
      }
    }
  }
  ```

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

4097
accessForward(): boolean
E
ester.zhou 已提交
4098

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

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

4103
**Return value**
E
ester.zhou 已提交
4104

4105 4106 4107
| Type     | Description                   |
| ------- | --------------------- |
| boolean | Returns **true** if going to the next page can be performed on the current page; returns **false** otherwise.|
4108 4109

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

4111 4112 4113 4114 4115
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4116
    controller: WebController = new WebController()
E
ester.zhou 已提交
4117

4118 4119
    build() {
      Column() {
4120 4121 4122 4123 4124 4125
        Button('accessForward')
          .onClick(() => {
            let result = this.controller.accessForward()
            console.log('result:' + result)
          })
        Web({ src: 'www.example.com', controller: this.controller })
4126 4127 4128 4129 4130
      }
    }
  }
  ```

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

4133
accessStep(step: number): boolean
E
ester.zhou 已提交
4134

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

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

4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
**Parameters**

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

**Return value**

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

4151
**Example**
E
ester.zhou 已提交
4152

4153 4154 4155 4156 4157
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4158
    controller: WebController = new WebController()
4159
    @State steps: number = 2
E
ester.zhou 已提交
4160

4161 4162
    build() {
      Column() {
4163
        Button('accessStep')
4164
          .onClick(() => {
4165 4166
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
4167 4168 4169 4170 4171 4172 4173
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4176
backward(): void
E
ester.zhou 已提交
4177

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

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

4182
**Example**
E
ester.zhou 已提交
4183

4184 4185 4186 4187 4188
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4189
    controller: WebController = new WebController()
E
ester.zhou 已提交
4190

4191 4192
    build() {
      Column() {
4193
        Button('backward')
4194
          .onClick(() => {
4195
            this.controller.backward()
4196 4197 4198 4199 4200 4201 4202
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4205
forward(): void
E
ester.zhou 已提交
4206

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

E
ester.zhou 已提交
4209
This API is deprecated since API version 9. You are advised to use [forward<sup>9+</sup>](../apis/js-apis-webview.md#forward) instead.
E
ester.zhou 已提交
4210 4211 4212 4213 4214 4215 4216 4217

**Example**

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

E
ester.zhou 已提交
4220 4221
    build() {
      Column() {
4222
        Button('forward')
E
ester.zhou 已提交
4223
          .onClick(() => {
4224
            this.controller.forward()
E
ester.zhou 已提交
4225 4226 4227 4228 4229 4230 4231
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4234
deleteJavaScriptRegister(name: string)
E
ester.zhou 已提交
4235

E
ester.zhou 已提交
4236
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](#refreshdeprecated) API.
4237

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

4240 4241 4242 4243 4244
**Parameters**

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

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

4248 4249 4250 4251 4252
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4253
    controller: WebController = new WebController()
4254
    @State name: string = 'Object'
E
ester.zhou 已提交
4255

4256 4257
    build() {
      Column() {
4258
        Button('deleteJavaScriptRegister')
4259
          .onClick(() => {
4260
            this.controller.deleteJavaScriptRegister(this.name)
4261 4262 4263 4264 4265 4266 4267
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4268
### getHitTest<sup>(deprecated)</sup>
4269

4270
getHitTest(): HitTestType
4271

E
ester.zhou 已提交
4272
Obtains the element type of the area being clicked.
4273

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

4276
**Return value**
4277

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

E
ester.zhou 已提交
4282
**Example**
4283

E
ester.zhou 已提交
4284 4285 4286 4287 4288
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4289
    controller: WebController = new WebController()
E
ester.zhou 已提交
4290

E
ester.zhou 已提交
4291 4292
    build() {
      Column() {
4293
        Button('getHitTest')
E
ester.zhou 已提交
4294
          .onClick(() => {
4295 4296
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
E
ester.zhou 已提交
4297 4298 4299 4300 4301 4302
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
4303

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

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

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

4310
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 已提交
4311

4312
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 已提交
4313

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

4316 4317 4318 4319 4320 4321 4322 4323 4324
**Parameters**

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

**Example**

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

E
ester.zhou 已提交
4335 4336
    build() {
      Column() {
4337
        Button('loadData')
E
ester.zhou 已提交
4338
          .onClick(() => {
4339 4340 4341 4342 4343
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
            })
E
ester.zhou 已提交
4344 4345 4346 4347 4348 4349 4350
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

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

4355
Loads a URL using the specified HTTP header.
E
ester.zhou 已提交
4356

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

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

E
ester.zhou 已提交
4361
This API is deprecated since API version 9. You are advised to use [loadUrl<sup>9+</sup>](../apis/js-apis-webview.md#loadurl) instead.
4362 4363 4364 4365 4366 4367 4368

**Parameters**

| Name    | Type                      | Mandatory  | Default Value | Description          |
| ------- | -------------------------- | ---- | ---- | -------------- |
| url     | string                     | Yes   | -    | URL to load.    |
| headers | Array\<[Header](#header)\> | No   | []   | Additional HTTP request header of the URL.|
E
ester.zhou 已提交
4369 4370 4371 4372 4373 4374 4375 4376

**Example**

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

E
ester.zhou 已提交
4379 4380
    build() {
      Column() {
4381
        Button('loadUrl')
E
ester.zhou 已提交
4382
          .onClick(() => {
4383
            this.controller.loadUrl({ url: 'www.example.com' })
E
ester.zhou 已提交
4384 4385 4386 4387 4388 4389 4390
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4393
onActive(): void
E
ester.zhou 已提交
4394

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

E
ester.zhou 已提交
4397
This API is deprecated since API version 9. You are advised to use [onActive<sup>9+</sup>](../apis/js-apis-webview.md#onactive) instead.
E
ester.zhou 已提交
4398 4399 4400 4401 4402 4403 4404 4405

**Example**

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

E
ester.zhou 已提交
4408 4409
    build() {
      Column() {
4410
        Button('onActive')
E
ester.zhou 已提交
4411
          .onClick(() => {
4412
            this.controller.onActive()
E
ester.zhou 已提交
4413 4414 4415 4416 4417 4418 4419
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4422
onInactive(): void
E
ester.zhou 已提交
4423

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

E
ester.zhou 已提交
4426
This API is deprecated since API version 9. You are advised to use [onInactive<sup>9+</sup>](../apis/js-apis-webview.md#oninactive) instead.
E
ester.zhou 已提交
4427 4428 4429 4430 4431 4432 4433 4434

**Example**

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

E
ester.zhou 已提交
4437 4438
    build() {
      Column() {
4439
        Button('onInactive')
E
ester.zhou 已提交
4440
          .onClick(() => {
4441
            this.controller.onInactive()
E
ester.zhou 已提交
4442 4443 4444 4445 4446 4447 4448
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

4449 4450
### zoom<sup>(deprecated)</sup>
zoom(factor: number): void
E
ester.zhou 已提交
4451

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

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

4456 4457 4458 4459 4460
**Parameters**

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

**Example**
E
ester.zhou 已提交
4463 4464 4465 4466 4467 4468

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4469
    controller: WebController = new WebController()
4470
    @State factor: number = 1
E
ester.zhou 已提交
4471

E
ester.zhou 已提交
4472 4473
    build() {
      Column() {
4474
        Button('zoom')
E
ester.zhou 已提交
4475
          .onClick(() => {
4476
            this.controller.zoom(this.factor)
E
ester.zhou 已提交
4477 4478 4479 4480 4481 4482 4483
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4486
refresh()
E
ester.zhou 已提交
4487

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

E
ester.zhou 已提交
4490
This API is deprecated since API version 9. You are advised to use [refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh) instead.
E
ester.zhou 已提交
4491 4492 4493 4494 4495 4496 4497 4498

**Example**

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

E
ester.zhou 已提交
4501 4502
    build() {
      Column() {
4503
        Button('refresh')
E
ester.zhou 已提交
4504
          .onClick(() => {
4505
            this.controller.refresh()
E
ester.zhou 已提交
4506 4507 4508 4509 4510 4511 4512
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4515 4516
registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })

E
ester.zhou 已提交
4517
Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. You must invoke the [refresh](#refreshdeprecated) API for the registration to take effect.
4518

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

**Parameters**

4523 4524 4525 4526 4527
| Name       | Type           | Mandatory  | Default Value | Description                                    |
| ---------- | --------------- | ---- | ---- | ---------------------------------------- |
| object     | object          | Yes   | -    | Application-side JavaScript object to be registered. Methods can be declared, but attributes cannot. The parameters and return value can only be of the string, number, or Boolean type.|
| name       | string          | Yes   | -    | Name of the object to be registered, which is the same as that invoked in the window. After registration, the window can use this name to access the JavaScript object at the application side.|
| methodList | Array\<string\> | Yes   | -    | Methods of the JavaScript object to be registered at the application side.                |
E
ester.zhou 已提交
4528 4529 4530 4531 4532 4533 4534

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
4535
  struct Index {
E
ester.zhou 已提交
4536
    controller: WebController = new WebController()
4537 4538 4539 4540 4541 4542 4543 4544
    testObj = {
      test: (data) => {
        return "ArkUI Web Component"
      },
      toString: () => {
        console.log('Web Component toString')
      }
    }
E
ester.zhou 已提交
4545 4546
    build() {
      Column() {
4547 4548 4549 4550 4551 4552 4553
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
            })
E
ester.zhou 已提交
4554
          })
4555 4556 4557
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
E
ester.zhou 已提交
4558 4559 4560 4561 4562
      }
    }
  }
  ```

4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
      }
  </script>
  </html>
E
ester.zhou 已提交
4578

4579
  ```
E
ester.zhou 已提交
4580

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

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

4585 4586
Executes a JavaScript script. This API uses an asynchronous callback to return the script execution result. **runJavaScript** can be invoked only after **loadUrl** is executed. For example, it can be invoked in **onPageEnd**.

E
ester.zhou 已提交
4587
This API is deprecated since API version 9. You are advised to use [runJavaScript<sup>9+</sup>](../apis/js-apis-webview.md#runjavascript) instead.
4588 4589 4590 4591 4592 4593 4594

**Parameters**

| Name     | Type                    | Mandatory  | Default Value | Description                                    |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | Yes   | -    | JavaScript script.                           |
| callback | (result: string) => void | No   | -    | Callback used to return the result. Returns **null** if the JavaScript script fails to be executed or no value is returned.|
E
ester.zhou 已提交
4595 4596 4597 4598 4599 4600 4601 4602

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4603
    controller: WebController = new WebController()
4604
    @State webResult: string = ''
E
ester.zhou 已提交
4605 4606
    build() {
      Column() {
4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
        Text(this.webResult).fontSize(20)
        Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          this.controller.runJavaScript({
            script: 'test()',
            callback: (result: string)=> {
              this.webResult = result
              console.info(`The test() return value is: ${result}`)
            }})
          console.info('url: ', e.url)
        })
E
ester.zhou 已提交
4619 4620 4621 4622 4623
      }
    }
  }
  ```

4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638
  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
        console.log('Ark WebComponent')
        return "This value is from index.html"
    }
    </script>
  </html>
E
ester.zhou 已提交
4639

4640
  ```
E
ester.zhou 已提交
4641

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

4644 4645 4646 4647
stop()

Stops page loading.

E
ester.zhou 已提交
4648
This API is deprecated since API version 9. You are advised to use [stop<sup>9+</sup>](../apis/js-apis-webview.md#stop) instead.
E
ester.zhou 已提交
4649 4650 4651 4652 4653 4654 4655 4656

**Example**

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

E
ester.zhou 已提交
4659 4660
    build() {
      Column() {
4661
        Button('stop')
E
ester.zhou 已提交
4662
          .onClick(() => {
4663
            this.controller.stop()
E
ester.zhou 已提交
4664 4665 4666 4667 4668 4669 4670
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

4673
clearHistory(): void
E
ester.zhou 已提交
4674

4675
Clears the browsing history.
E
ester.zhou 已提交
4676

E
ester.zhou 已提交
4677
This API is deprecated since API version 9. You are advised to use [clearHistory<sup>9+</sup>](../apis/js-apis-webview.md#clearhistory) instead.
E
ester.zhou 已提交
4678 4679 4680 4681 4682 4683 4684 4685

**Example**

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

E
ester.zhou 已提交
4688 4689
    build() {
      Column() {
4690
        Button('clearHistory')
E
ester.zhou 已提交
4691
          .onClick(() => {
4692
            this.controller.clearHistory()
E
ester.zhou 已提交
4693 4694 4695 4696 4697 4698 4699
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
ester.zhou 已提交
4700
## WebCookie<sup>(deprecated)</sup>
E
ester.zhou 已提交
4701

E
ester.zhou 已提交
4702
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.
4703

E
ester.zhou 已提交
4704
### setCookie<sup>(deprecated)</sup>
E
ester.zhou 已提交
4705
setCookie(url: string, value: string): boolean
E
ester.zhou 已提交
4706

E
ester.zhou 已提交
4707
Sets the cookie. This API returns the result synchronously. Returns **true** if the operation is successful; returns **false** otherwise.
E
ester.zhou 已提交
4708
This API is deprecated since API version 9. You are advised to use [setCookie<sup>9+</sup>](../apis/js-apis-webview.md#setcookie) instead.
4709

E
ester.zhou 已提交
4710
**Parameters**
4711

E
ester.zhou 已提交
4712 4713
| Name  | Type  | Mandatory  | Default Value | Description             |
| ----- | ------ | ---- | ---- | ----------------- |
E
ester.zhou 已提交
4714
| url   | string | Yes   | -    | URL of the cookie to set. A complete URL is recommended.|
E
ester.zhou 已提交
4715
| value | string | Yes   | -    | Value of the cookie to set.        |
4716

4717 4718
**Return value**

E
ester.zhou 已提交
4719 4720 4721
| Type     | Description           |
| ------- | ------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
4722 4723

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

4725 4726 4727 4728 4729
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4730
    controller: WebController = new WebController()
E
ester.zhou 已提交
4731

4732 4733
    build() {
      Column() {
E
ester.zhou 已提交
4734
        Button('setCookie')
4735
          .onClick(() => {
E
ester.zhou 已提交
4736
            let result = this.controller.getCookieManager().setCookie("https://www.example.com", "a=b")
4737
            console.log("result: " + result)
4738 4739 4740 4741 4742 4743
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
ester.zhou 已提交
4744 4745
### saveCookie<sup>(deprecated)</sup>
saveCookie(): boolean
4746

E
ester.zhou 已提交
4747
Saves the cookies in the memory to the drive. This API returns the result synchronously.
E
ester.zhou 已提交
4748
This API is deprecated since API version 9. You are advised to use [saveCookieAsync<sup>9+</sup>](../apis/js-apis-webview.md#savecookieasync) instead.
4749

E
ester.zhou 已提交
4750
**Return value**
4751

E
ester.zhou 已提交
4752 4753 4754
| Type     | Description                  |
| ------- | -------------------- |
| boolean | Operation result.|
4755 4756

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

4758 4759 4760 4761 4762
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
E
ester.zhou 已提交
4763
    controller: WebController = new WebController()
E
ester.zhou 已提交
4764

4765 4766
    build() {
      Column() {
E
ester.zhou 已提交
4767
        Button('saveCookie')
4768
          .onClick(() => {
E
ester.zhou 已提交
4769
            let result = this.controller.getCookieManager().saveCookie()
E
ester.zhou 已提交
4770
            console.log("result: " + result)
4771
          })
4772
        Web({ src: 'www.example.com', controller: this.controller })
4773 4774 4775 4776
      }
    }
  }
  ```