1# Web 2 3The **<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> **NOTE** 6> 7> - This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8> - You can preview how this component looks on a real device. The preview is not yet available in the DevEco Studio Previewer. 9 10## Required Permissions 11To 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). 12 13## Child Components 14 15Not supported 16 17## APIs 18 19Web(options: { src: ResourceStr, controller: WebviewController | WebController}) 20 21> **NOTE** 22> 23> Transition animation is not supported. 24> 25> **\<Web>** components on a page must be bound to different **WebviewController**s. 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| ---------- | ---------------------------------------- | ---- | ------- | 31| src | [ResourceStr](ts-types.md) | Yes | Address of a web page resource. To access local resource files, use the **$rawfile** or **resource** protocol. To load a local resource file in the sandbox outside of the application package, use **file://** to specify the path of the sandbox.| 32| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | Yes | Controller. **WebController** is deprecated since API version 9. You are advised to use **WebviewController** instead.| 33 34**Example** 35 36 Example of loading online web pages: 37 ```ts 38 // xxx.ets 39 import web_webview from '@ohos.web.webview' 40 41 @Entry 42 @Component 43 struct WebComponent { 44 controller: web_webview.WebviewController = new web_webview.WebviewController() 45 build() { 46 Column() { 47 Web({ src: 'www.example.com', controller: this.controller }) 48 } 49 } 50 } 51 ``` 52 53 Example of loading local web pages: 54 ```ts 55 // xxx.ets 56 import web_webview from '@ohos.web.webview' 57 58 @Entry 59 @Component 60 struct WebComponent { 61 controller: web_webview.WebviewController = new web_webview.WebviewController() 62 build() { 63 Column() { 64 Web({ src: $rawfile("index.html"), controller: this.controller }) 65 } 66 } 67 } 68 ``` 69 70 Example of loading local resource files in the sandbox: 71 1. Use[globalthis](../../application-models/uiability-data-sync-with-ui.md#using-globalthis-between-uiability-and-page) to obtain the path of the sandbox. 72 ```ts 73 // xxx.ets 74 import web_webview from '@ohos.web.webview' 75 let url = 'file://' + globalThis.filesDir + '/xxx.html' 76 77 @Entry 78 @Component 79 struct WebComponent { 80 controller: web_webview.WebviewController = new web_webview.WebviewController() 81 build() { 82 Column() { 83 // Load the files in the sandbox. 84 Web({ src: url, controller: this.controller }) 85 } 86 } 87 } 88 ``` 89 90 2. Modify the **MainAbility.ts** file. 91 92 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). 93 ```ts 94 // xxx.ts 95 import UIAbility from '@ohos.app.ability.UIAbility'; 96 import web_webview from '@ohos.web.webview'; 97 98 export default class EntryAbility extends UIAbility { 99 onCreate(want, launchParam) { 100 // Bind filesDir to the globalThis object to implement data synchronization between the UIAbility component and the UI. 101 globalThis.filesDir = this.context.filesDir 102 console.log("Sandbox path is " + globalThis.filesDir) 103 } 104 } 105 ``` 106 107 ```html 108 <!-- index.html --> 109 <!DOCTYPE html> 110 <html> 111 <body> 112 <p>Hello World</p> 113 </body> 114 </html> 115 ``` 116 117## Attributes 118 119Only 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). 120 121### domStorageAccess 122 123domStorageAccess(domStorageAccess: boolean) 124 125Sets whether to enable the DOM Storage API. By default, this feature is disabled. 126 127**Parameters** 128 129| Name | Type | Mandatory | Default Value | Description | 130| ---------------- | ------- | ---- | ----- | ------------------------------------ | 131| domStorageAccess | boolean | Yes | false | Whether to enable the DOM Storage API.| 132 133**Example** 134 135 ```ts 136 // xxx.ets 137 import web_webview from '@ohos.web.webview' 138 139 @Entry 140 @Component 141 struct WebComponent { 142 controller: web_webview.WebviewController = new web_webview.WebviewController() 143 build() { 144 Column() { 145 Web({ src: 'www.example.com', controller: this.controller }) 146 .domStorageAccess(true) 147 } 148 } 149 } 150 ``` 151 152### fileAccess 153 154fileAccess(fileAccess: boolean) 155 156Sets 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). 157 158**Parameters** 159 160| Name | Type | Mandatory | Default Value | Description | 161| ---------- | ------- | ---- | ---- | ---------------------- | 162| fileAccess | boolean | Yes | true | Whether to enable access to the file system in the application. By default, this feature is enabled.| 163 164**Example** 165 166 ```ts 167 // xxx.ets 168 import web_webview from '@ohos.web.webview' 169 170 @Entry 171 @Component 172 struct WebComponent { 173 controller: web_webview.WebviewController = new web_webview.WebviewController() 174 build() { 175 Column() { 176 Web({ src: 'www.example.com', controller: this.controller }) 177 .fileAccess(true) 178 } 179 } 180 } 181 ``` 182 183### imageAccess 184 185imageAccess(imageAccess: boolean) 186 187Sets whether to enable automatic image loading. By default, this feature is enabled. 188 189**Parameters** 190 191| Name | Type | Mandatory | Default Value | Description | 192| ----------- | ------- | ---- | ---- | --------------- | 193| imageAccess | boolean | Yes | true | Whether to enable automatic image loading.| 194 195**Example** 196 ```ts 197 // xxx.ets 198 import web_webview from '@ohos.web.webview' 199 200 @Entry 201 @Component 202 struct WebComponent { 203 controller: web_webview.WebviewController = new web_webview.WebviewController() 204 build() { 205 Column() { 206 Web({ src: 'www.example.com', controller: this.controller }) 207 .imageAccess(true) 208 } 209 } 210 } 211 ``` 212 213### javaScriptProxy 214 215javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>, 216 controller: WebviewController | WebController}) 217 218Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. The parameters cannot be updated. 219 220**Parameters** 221 222| Name | Type | Mandatory | Default Value | Description | 223| ---------- | ---------------------------------------- | ---- | ---- | ------------------------- | 224| object | object | Yes | - | Object to be registered. Methods can be declared, but attributes cannot. | 225| name | string | Yes | - | Name of the object to be registered, which is the same as that invoked in the window.| 226| methodList | Array\<string\> | Yes | - | Methods of the JavaScript object to be registered at the application side. | 227| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | Yes | - | Controller. **WebController** is deprecated since API version 9. You are advised to use **WebviewController** instead.| 228 229**Example** 230 231 ```ts 232 // xxx.ets 233 import web_webview from '@ohos.web.webview' 234 235 @Entry 236 @Component 237 struct WebComponent { 238 controller: web_webview.WebviewController = new web_webview.WebviewController() 239 testObj = { 240 test: (data1, data2, data3) => { 241 console.log("data1:" + data1) 242 console.log("data2:" + data2) 243 console.log("data3:" + data3) 244 return "AceString" 245 }, 246 toString: () => { 247 console.log('toString' + "interface instead.") 248 } 249 } 250 build() { 251 Column() { 252 Web({ src: 'www.example.com', controller: this.controller }) 253 .javaScriptAccess(true) 254 .javaScriptProxy({ 255 object: this.testObj, 256 name: "objName", 257 methodList: ["test", "toString"], 258 controller: this.controller, 259 }) 260 } 261 } 262 } 263 ``` 264 265### javaScriptAccess 266 267javaScriptAccess(javaScriptAccess: boolean) 268 269Sets whether JavaScript scripts can be executed. By default, JavaScript scripts can be executed. 270 271**Parameters** 272 273| Name | Type | Mandatory | Default Value | Description | 274| ---------------- | ------- | ---- | ---- | ------------------- | 275| javaScriptAccess | boolean | Yes | true | Whether JavaScript scripts can be executed.| 276 277**Example** 278 279 ```ts 280 // xxx.ets 281 import web_webview from '@ohos.web.webview' 282 283 @Entry 284 @Component 285 struct WebComponent { 286 controller: web_webview.WebviewController = new web_webview.WebviewController() 287 build() { 288 Column() { 289 Web({ src: 'www.example.com', controller: this.controller }) 290 .javaScriptAccess(true) 291 } 292 } 293 } 294 ``` 295 296### mixedMode 297 298mixedMode(mixedMode: MixedMode) 299 300Sets whether to enable loading of HTTP and HTTPS hybrid content can be loaded. By default, this feature is disabled. 301 302**Parameters** 303 304| Name | Type | Mandatory | Default Value | Description | 305| --------- | --------------------------- | ---- | -------------- | --------- | 306| mixedMode | [MixedMode](#mixedmode)| Yes | MixedMode.None | Mixed content to load.| 307 308**Example** 309 310 ```ts 311 // xxx.ets 312 import web_webview from '@ohos.web.webview' 313 314 @Entry 315 @Component 316 struct WebComponent { 317 controller: web_webview.WebviewController = new web_webview.WebviewController() 318 @State mode: MixedMode = MixedMode.All 319 build() { 320 Column() { 321 Web({ src: 'www.example.com', controller: this.controller }) 322 .mixedMode(this.mode) 323 } 324 } 325 } 326 ``` 327 328### onlineImageAccess 329 330onlineImageAccess(onlineImageAccess: boolean) 331 332Sets whether to enable access to online images through HTTP and HTTPS. By default, this feature is enabled. 333 334**Parameters** 335 336| Name | Type | Mandatory | Default Value | Description | 337| ----------------- | ------- | ---- | ---- | ---------------- | 338| onlineImageAccess | boolean | Yes | true | Whether to enable access to online images through HTTP and HTTPS.| 339 340**Example** 341 342 ```ts 343 // xxx.ets 344 import web_webview from '@ohos.web.webview' 345 346 @Entry 347 @Component 348 struct WebComponent { 349 controller: web_webview.WebviewController = new web_webview.WebviewController() 350 build() { 351 Column() { 352 Web({ src: 'www.example.com', controller: this.controller }) 353 .onlineImageAccess(true) 354 } 355 } 356 } 357 ``` 358 359### zoomAccess 360 361zoomAccess(zoomAccess: boolean) 362 363Sets whether to enable zoom gestures. By default, this feature is enabled. 364 365**Parameters** 366 367| Name | Type | Mandatory | Default Value | Description | 368| ---------- | ------- | ---- | ---- | ------------- | 369| zoomAccess | boolean | Yes | true | Whether to enable zoom gestures.| 370 371**Example** 372 373 ```ts 374 // xxx.ets 375 import web_webview from '@ohos.web.webview' 376 377 @Entry 378 @Component 379 struct WebComponent { 380 controller: web_webview.WebviewController = new web_webview.WebviewController() 381 build() { 382 Column() { 383 Web({ src: 'www.example.com', controller: this.controller }) 384 .zoomAccess(true) 385 } 386 } 387 } 388 ``` 389 390### overviewModeAccess 391 392overviewModeAccess(overviewModeAccess: boolean) 393 394Sets whether to load web pages by using the overview mode. By default, this feature is enabled. 395 396**Parameters** 397 398| Name | Type | Mandatory | Default Value | Description | 399| ------------------ | ------- | ---- | ---- | --------------- | 400| overviewModeAccess | boolean | Yes | true | Whether to load web pages by using the overview mode.| 401 402**Example** 403 404 ```ts 405 // xxx.ets 406 import web_webview from '@ohos.web.webview' 407 408 @Entry 409 @Component 410 struct WebComponent { 411 controller: web_webview.WebviewController = new web_webview.WebviewController() 412 build() { 413 Column() { 414 Web({ src: 'www.example.com', controller: this.controller }) 415 .overviewModeAccess(true) 416 } 417 } 418 } 419 ``` 420 421### databaseAccess 422 423databaseAccess(databaseAccess: boolean) 424 425Sets whether to enable database access. By default, this feature is disabled. 426 427**Parameters** 428 429| Name | Type | Mandatory | Default Value | Description | 430| -------------- | ------- | ---- | ----- | ----------------- | 431| databaseAccess | boolean | Yes | false | Whether to enable database access.| 432 433**Example** 434 435 ```ts 436 // xxx.ets 437 import web_webview from '@ohos.web.webview' 438 439 @Entry 440 @Component 441 struct WebComponent { 442 controller: web_webview.WebviewController = new web_webview.WebviewController() 443 build() { 444 Column() { 445 Web({ src: 'www.example.com', controller: this.controller }) 446 .databaseAccess(true) 447 } 448 } 449 } 450 ``` 451 452### geolocationAccess 453 454geolocationAccess(geolocationAccess: boolean) 455 456Sets whether to enable geolocation access. By default, this feature is enabled. 457 458**Parameters** 459 460| Name | Type | Mandatory | Default Value | Description | 461| ----------------- | ------- | ---- | ---- | --------------- | 462| geolocationAccess | boolean | Yes | true | Whether to enable geolocation access.| 463 464**Example** 465 466 ```ts 467 // xxx.ets 468 import web_webview from '@ohos.web.webview' 469 470 @Entry 471 @Component 472 struct WebComponent { 473 controller: web_webview.WebviewController = new web_webview.WebviewController() 474 build() { 475 Column() { 476 Web({ src: 'www.example.com', controller: this.controller }) 477 .geolocationAccess(true) 478 } 479 } 480 } 481 ``` 482 483### mediaPlayGestureAccess 484 485mediaPlayGestureAccess(access: boolean) 486 487Sets 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. 488 489**Parameters** 490 491| Name | Type | Mandatory | Default Value | Description | 492| ------ | ------- | ---- | ---- | ----------------- | 493| access | boolean | Yes | true | Whether video playback must be started by user gestures.| 494 495**Example** 496 497 ```ts 498 // xxx.ets 499 import web_webview from '@ohos.web.webview' 500 501 @Entry 502 @Component 503 struct WebComponent { 504 controller: web_webview.WebviewController = new web_webview.WebviewController() 505 @State access: boolean = true 506 build() { 507 Column() { 508 Web({ src: 'www.example.com', controller: this.controller }) 509 .mediaPlayGestureAccess(this.access) 510 } 511 } 512 } 513 ``` 514 515### multiWindowAccess<sup>9+</sup> 516 517multiWindowAccess(multiWindow: boolean) 518 519Sets whether to enable the multi-window permission. 520 521**Parameters** 522 523| Name | Type | Mandatory | Default Value | Description | 524| ----------- | ------- | ---- | ----- | ------------ | 525| multiWindow | boolean | Yes | false | Whether to enable the multi-window permission.| 526 527**Example** 528 529 ```ts 530 // xxx.ets 531 import web_webview from '@ohos.web.webview' 532 533 @Entry 534 @Component 535 struct WebComponent { 536 controller: web_webview.WebviewController = new web_webview.WebviewController() 537 build() { 538 Column() { 539 Web({ src: 'www.example.com', controller: this.controller }) 540 .multiWindowAccess(true) 541 } 542 } 543 } 544 ``` 545 546### horizontalScrollBarAccess<sup>9+</sup> 547 548horizontalScrollBarAccess(horizontalScrollBar: boolean) 549 550Sets whether to display the horizontal scrollbar, including the default system scrollbar and custom scrollbar. By default, the horizontal scrollbar is displayed. 551 552**Parameters** 553 554| Name | Type | Mandatory | Default Value | Description | 555| ----------- | ------- | ---- | ----- | ------------ | 556| horizontalScrollBar | boolean | Yes | true | Whether to display the horizontal scrollbar.| 557 558**Example** 559 560 ```ts 561 // xxx.ets 562 import web_webview from '@ohos.web.webview' 563 564 @Entry 565 @Component 566 struct WebComponent { 567 controller: web_webview.WebviewController = new web_webview.WebviewController() 568 build() { 569 Column() { 570 Web({ src: 'www.example.com', controller: this.controller }) 571 .horizontalScrollBarAccess(true) 572 } 573 } 574 } 575 ``` 576 577 ```html 578 <!--xxx.html--> 579 <!DOCTYPE html> 580 <html> 581 <head> 582 <title>Demo</title> 583 <style> 584 body { 585 width:3000px; 586 height:3000px; 587 padding-right:170px; 588 padding-left:170px; 589 border:5px solid blueviolet 590 } 591 </style> 592 </head> 593 <body> 594 Scroll Test 595 </body> 596 </html> 597 ``` 598 599### verticalScrollBarAccess<sup>9+</sup> 600 601verticalScrollBarAccess(verticalScrollBar: boolean) 602 603Sets whether to display the vertical scrollbar, including the default system scrollbar and custom scrollbar. By default, the vertical scrollbar is displayed. 604 605**Parameters** 606 607| Name | Type | Mandatory | Default Value | Description | 608| ----------- | ------- | ---- | ----- | ------------ | 609| verticalScrollBarAccess | boolean | Yes | true | Whether to display the vertical scrollbar.| 610 611**Example** 612 613 ```ts 614 // xxx.ets 615 import web_webview from '@ohos.web.webview' 616 617 @Entry 618 @Component 619 struct WebComponent { 620 controller: web_webview.WebviewController = new web_webview.WebviewController() 621 build() { 622 Column() { 623 Web({ src: 'www.example.com', controller: this.controller }) 624 .verticalScrollBarAccess(true) 625 } 626 } 627 } 628 ``` 629 630 ```html 631 <!--xxx.html--> 632 <!DOCTYPE html> 633 <html> 634 <head> 635 <title>Demo</title> 636 <style> 637 body { 638 width:3000px; 639 height:3000px; 640 padding-right:170px; 641 padding-left:170px; 642 border:5px solid blueviolet 643 } 644 </style> 645 </head> 646 <body> 647 Scroll Test 648 </body> 649 </html> 650 ``` 651 652 653### cacheMode 654 655cacheMode(cacheMode: CacheMode) 656 657Sets the cache mode. 658 659**Parameters** 660 661| Name | Type | Mandatory | Default Value | Description | 662| --------- | --------------------------- | ---- | ----------------- | --------- | 663| cacheMode | [CacheMode](#cachemode)| Yes | CacheMode.Default | Cache mode to set.| 664 665**Example** 666 667 ```ts 668 // xxx.ets 669 import web_webview from '@ohos.web.webview' 670 671 @Entry 672 @Component 673 struct WebComponent { 674 controller: web_webview.WebviewController = new web_webview.WebviewController() 675 @State mode: CacheMode = CacheMode.None 676 build() { 677 Column() { 678 Web({ src: 'www.example.com', controller: this.controller }) 679 .cacheMode(this.mode) 680 } 681 } 682 } 683 ``` 684 685### textZoomRatio<sup>9+</sup> 686 687textZoomRatio(textZoomRatio: number) 688 689Sets the text zoom ratio of the page. The default value is **100**, which indicates 100%. 690 691**Parameters** 692 693| Name | Type | Mandatory | Default Value | Description | 694| ------------- | ------ | ---- | ---- | --------------- | 695| textZoomRatio | number | Yes | 100 | Text zoom ratio to set.| 696 697**Example** 698 699 ```ts 700 // xxx.ets 701 import web_webview from '@ohos.web.webview' 702 703 @Entry 704 @Component 705 struct WebComponent { 706 controller: web_webview.WebviewController = new web_webview.WebviewController() 707 @State atio: number = 150 708 build() { 709 Column() { 710 Web({ src: 'www.example.com', controller: this.controller }) 711 .textZoomRatio(this.atio) 712 } 713 } 714 } 715 ``` 716 717### initialScale<sup>9+</sup> 718 719initialScale(percent: number) 720 721Sets the scale factor of the entire page. The default value is 100%. 722 723**Parameters** 724 725| Name | Type | Mandatory | Default Value | Description | 726| ------- | ------ | ---- | ---- | --------------- | 727| percent | number | Yes | 100 | Scale factor of the entire page.| 728 729**Example** 730 731 ```ts 732 // xxx.ets 733 import web_webview from '@ohos.web.webview' 734 735 @Entry 736 @Component 737 struct WebComponent { 738 controller: web_webview.WebviewController = new web_webview.WebviewController() 739 @State percent: number = 100 740 build() { 741 Column() { 742 Web({ src: 'www.example.com', controller: this.controller }) 743 .initialScale(this.percent) 744 } 745 } 746 } 747 ``` 748 749### userAgent 750 751userAgent(userAgent: string) 752 753Sets the user agent. 754 755**Parameters** 756 757| Name | Type | Mandatory | Default Value | Description | 758| --------- | ------ | ---- | ---- | --------- | 759| userAgent | string | Yes | - | User agent to set.| 760 761**Example** 762 763 ```ts 764 // xxx.ets 765 import web_webview from '@ohos.web.webview' 766 767 @Entry 768 @Component 769 struct WebComponent { 770 controller: web_webview.WebviewController = new web_webview.WebviewController() 771 @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' 772 build() { 773 Column() { 774 Web({ src: 'www.example.com', controller: this.controller }) 775 .userAgent(this.userAgent) 776 } 777 } 778 } 779 ``` 780 781### blockNetwork<sup>9+</sup> 782 783blockNetwork(block: boolean) 784 785Sets whether to block online downloads. 786 787**Parameters** 788 789| Name| Type| Mandatory| Default Value| Description | 790| ------ | -------- | ---- | ------ | ----------------------------------- | 791| block | boolean | Yes | false | Whether to block online downloads.| 792 793**Example** 794 795 ```ts 796 // xxx.ets 797 import web_webview from '@ohos.web.webview' 798 @Entry 799 @Component 800 struct WebComponent { 801 controller: web_webview.WebviewController = new web_webview.WebviewController() 802 @State block: boolean = true 803 build() { 804 Column() { 805 Web({ src: 'www.example.com', controller: this.controller }) 806 .blockNetwork(this.block) 807 } 808 } 809 } 810 ``` 811 812### defaultFixedFontSize<sup>9+</sup> 813 814defaultFixedFontSize(size: number) 815 816Sets the default fixed font size for the web page. 817 818**Parameters** 819 820| Name| Type| Mandatory| Default Value| Description | 821| ------ | -------- | ---- | ------ | ---------------------------- | 822| 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. | 823 824**Example** 825 826 ```ts 827 // xxx.ets 828 import web_webview from '@ohos.web.webview' 829 @Entry 830 @Component 831 struct WebComponent { 832 controller: web_webview.WebviewController = new web_webview.WebviewController() 833 @State fontSize: number = 16 834 build() { 835 Column() { 836 Web({ src: 'www.example.com', controller: this.controller }) 837 .defaultFixedFontSize(this.fontSize) 838 } 839 } 840 } 841 ``` 842 843### defaultFontSize<sup>9+</sup> 844 845defaultFontSize(size: number) 846 847Sets the default font size for the web page. 848 849**Parameters** 850 851| Name| Type| Mandatory| Default Value| Description | 852| ------ | -------- | ---- | ------ | ------------------------ | 853| 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. | 854 855**Example** 856 857 ```ts 858 // xxx.ets 859 import web_webview from '@ohos.web.webview' 860 @Entry 861 @Component 862 struct WebComponent { 863 controller: web_webview.WebviewController = new web_webview.WebviewController() 864 @State fontSize: number = 13 865 build() { 866 Column() { 867 Web({ src: 'www.example.com', controller: this.controller }) 868 .defaultFontSize(this.fontSize) 869 } 870 } 871 } 872 ``` 873 874### minFontSize<sup>9+</sup> 875 876minFontSize(size: number) 877 878Sets the minimum font size for the web page. 879 880**Parameters** 881 882| Name| Type| Mandatory| Default Value| Description | 883| ------ | -------- | ---- | ------ | ------------------------ | 884| 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. | 885 886**Example** 887 888 ```ts 889 // xxx.ets 890 import web_webview from '@ohos.web.webview' 891 @Entry 892 @Component 893 struct WebComponent { 894 controller: web_webview.WebviewController = new web_webview.WebviewController() 895 @State fontSize: number = 13 896 build() { 897 Column() { 898 Web({ src: 'www.example.com', controller: this.controller }) 899 .minFontSize(this.fontSize) 900 } 901 } 902 } 903 ``` 904 905### minLogicalFontSize<sup>9+</sup> 906 907minLogicalFontSize(size: number) 908 909Sets the minimum logical font size for the web page. 910 911**Parameters** 912 913| Name| Type| Mandatory| Default Value| Description | 914| ------ | -------- | ---- | ------ | ------------------------ | 915| 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. | 916 917**Example** 918 919 ```ts 920 // xxx.ets 921 import web_webview from '@ohos.web.webview' 922 @Entry 923 @Component 924 struct WebComponent { 925 controller: web_webview.WebviewController = new web_webview.WebviewController() 926 @State fontSize: number = 13 927 build() { 928 Column() { 929 Web({ src: 'www.example.com', controller: this.controller }) 930 .minLogicalFontSize(this.fontSize) 931 } 932 } 933 } 934 ``` 935 936 937### webFixedFont<sup>9+</sup> 938 939webFixedFont(family: string) 940 941Sets the fixed font family for the web page. 942 943**Parameters** 944 945| Name| Type| Mandatory| Default Value | Description | 946| ------ | -------- | ---- | --------- | ---------------------------- | 947| family | string | Yes | monospace | Fixed font family to set.| 948 949**Example** 950 951 ```ts 952 // xxx.ets 953 import web_webview from '@ohos.web.webview' 954 @Entry 955 @Component 956 struct WebComponent { 957 controller: web_webview.WebviewController = new web_webview.WebviewController() 958 @State family: string = "monospace" 959 build() { 960 Column() { 961 Web({ src: 'www.example.com', controller: this.controller }) 962 .webFixedFont(this.family) 963 } 964 } 965 } 966 ``` 967 968### webSansSerifFont<sup>9+</sup> 969 970webSansSerifFont(family: string) 971 972Sets the sans serif font family for the web page. 973 974**Parameters** 975 976| Name| Type| Mandatory| Default Value | Description | 977| ------ | -------- | ---- | ---------- | --------------------------------- | 978| family | string | Yes | sans-serif | Sans serif font family to set.| 979 980**Example** 981 982 ```ts 983 // xxx.ets 984 import web_webview from '@ohos.web.webview' 985 @Entry 986 @Component 987 struct WebComponent { 988 controller: web_webview.WebviewController = new web_webview.WebviewController() 989 @State family: string = "sans-serif" 990 build() { 991 Column() { 992 Web({ src: 'www.example.com', controller: this.controller }) 993 .webSansSerifFont(this.family) 994 } 995 } 996 } 997 ``` 998 999### webSerifFont<sup>9+</sup> 1000 1001webSerifFont(family: string) 1002 1003Sets the serif font family for the web page. 1004 1005**Parameters** 1006 1007| Name| Type| Mandatory| Default Value| Description | 1008| ------ | -------- | ---- | ------ | ---------------------------- | 1009| family | string | Yes | serif | Serif font family to set.| 1010 1011**Example** 1012 1013 ```ts 1014 // xxx.ets 1015 import web_webview from '@ohos.web.webview' 1016 @Entry 1017 @Component 1018 struct WebComponent { 1019 controller: web_webview.WebviewController = new web_webview.WebviewController() 1020 @State family: string = "serif" 1021 build() { 1022 Column() { 1023 Web({ src: 'www.example.com', controller: this.controller }) 1024 .webSerifFont(this.family) 1025 } 1026 } 1027 } 1028 ``` 1029 1030### webStandardFont<sup>9+</sup> 1031 1032webStandardFont(family: string) 1033 1034Sets the standard font family for the web page. 1035 1036**Parameters** 1037 1038| Name| Type| Mandatory| Default Value | Description | 1039| ------ | -------- | ---- | ---------- | ------------------------------- | 1040| family | string | Yes | sans serif | Standard font family to set.| 1041 1042**Example** 1043 1044 ```ts 1045 // xxx.ets 1046 import web_webview from '@ohos.web.webview' 1047 @Entry 1048 @Component 1049 struct WebComponent { 1050 controller: web_webview.WebviewController = new web_webview.WebviewController() 1051 @State family: string = "sans-serif" 1052 build() { 1053 Column() { 1054 Web({ src: 'www.example.com', controller: this.controller }) 1055 .webStandardFont(this.family) 1056 } 1057 } 1058 } 1059 ``` 1060 1061### webFantasyFont<sup>9+</sup> 1062 1063webFantasyFont(family: string) 1064 1065Sets the fantasy font family for the web page. 1066 1067**Parameters** 1068 1069| Name| Type| Mandatory| Default Value | Description | 1070| ------ | -------- | ---- | ------- | ------------------------------ | 1071| family | string | Yes | fantasy | Fantasy font family to set.| 1072 1073**Example** 1074 1075 ```ts 1076 // xxx.ets 1077 import web_webview from '@ohos.web.webview' 1078 @Entry 1079 @Component 1080 struct WebComponent { 1081 controller: web_webview.WebviewController = new web_webview.WebviewController() 1082 @State family: string = "fantasy" 1083 build() { 1084 Column() { 1085 Web({ src: 'www.example.com', controller: this.controller }) 1086 .webFantasyFont(this.family) 1087 } 1088 } 1089 } 1090 ``` 1091 1092### webCursiveFont<sup>9+</sup> 1093 1094webCursiveFont(family: string) 1095 1096Sets the cursive font family for the web page. 1097 1098**Parameters** 1099 1100| Name| Type| Mandatory| Default Value | Description | 1101| ------ | -------- | ---- | ------- | ------------------------------ | 1102| family | string | Yes | cursive | Cursive font family to set.| 1103 1104**Example** 1105 1106 ```ts 1107 // xxx.ets 1108 import web_webview from '@ohos.web.webview' 1109 @Entry 1110 @Component 1111 struct WebComponent { 1112 controller: web_webview.WebviewController = new web_webview.WebviewController() 1113 @State family: string = "cursive" 1114 build() { 1115 Column() { 1116 Web({ src: 'www.example.com', controller: this.controller }) 1117 .webCursiveFont(this.family) 1118 } 1119 } 1120 } 1121 ``` 1122 1123### darkMode<sup>9+</sup> 1124 1125darkMode(mode: WebDarkMode) 1126 1127Sets 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). 1128 1129**Parameters** 1130 1131| Name| Type| Mandatory| Default Value | Description | 1132| ------ | ----------- | ---- | --------------- | ------------------ | 1133| mode | [WebDarkMode](#webdarkmode9) | Yes | WebDarkMode.Off | Web dark mode to set.| 1134 1135**Example** 1136 1137 ```ts 1138 // xxx.ets 1139 import web_webview from '@ohos.web.webview' 1140 @Entry 1141 @Component 1142 struct WebComponent { 1143 controller: web_webview.WebviewController = new web_webview.WebviewController() 1144 @State mode: WebDarkMode = WebDarkMode.On 1145 build() { 1146 Column() { 1147 Web({ src: 'www.example.com', controller: this.controller }) 1148 .darkMode(this.mode) 1149 } 1150 } 1151 } 1152 ``` 1153 1154### forceDarkAccess<sup>9+</sup> 1155 1156forceDarkAccess(access: boolean) 1157 1158Sets 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). 1159 1160**Parameters** 1161 1162| Name| Type| Mandatory| Default Value | Description | 1163| ------ | ------- | ---- | ----- | ------------------ | 1164| access | boolean | Yes | false | Whether to enable forcible dark mode for the web page.| 1165 1166**Example** 1167 1168 ```ts 1169 // xxx.ets 1170 import web_webview from '@ohos.web.webview' 1171 @Entry 1172 @Component 1173 struct WebComponent { 1174 controller: web_webview.WebviewController = new web_webview.WebviewController() 1175 @State mode: WebDarkMode = WebDarkMode.On 1176 @State access: boolean = true 1177 build() { 1178 Column() { 1179 Web({ src: 'www.example.com', controller: this.controller }) 1180 .darkMode(this.mode) 1181 .forceDarkAccess(this.access) 1182 } 1183 } 1184 } 1185 ``` 1186 1187### pinchSmooth<sup>9+</sup> 1188 1189pinchSmooth(isEnabled: boolean) 1190 1191Sets whether to enable smooth pinch mode for the web page. 1192 1193**Parameters** 1194 1195| Name | Type| Mandatory| Default Value| Description | 1196| --------- | -------- | ---- | ------ | -------------------------- | 1197| isEnabled | boolean | Yes | false | Whether to enable smooth pinch mode for the web page.| 1198 1199**Example** 1200 1201 ```ts 1202// xxx.ets 1203import web_webview from '@ohos.web.webview' 1204@Entry 1205@Component 1206struct WebComponent { 1207 controller: web_webview.WebviewController = new web_webview.WebviewController() 1208 build() { 1209 Column() { 1210 Web({ src: 'www.example.com', controller: this.controller }) 1211 .pinchSmooth(true) 1212 } 1213 } 1214} 1215 ``` 1216 1217 1218## Events 1219 1220The universal events are not supported. 1221 1222### onAlert 1223 1224onAlert(callback: (event?: { url: string; message: string; result: JsResult }) => boolean) 1225 1226Called when **alert()** is invoked to display an alert dialog box on the web page. 1227 1228**Parameters** 1229 1230| Name | Type | Description | 1231| ------- | --------------------- | --------------- | 1232| url | string | URL of the web page where the dialog box is displayed.| 1233| message | string | Message displayed in the dialog box. | 1234| result | [JsResult](#jsresult) | User operation. | 1235 1236**Return value** 1237 1238| Type | Description | 1239| ------- | ---------------------------------------- | 1240| 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.| 1241 1242**Example** 1243 1244 ```ts 1245 // xxx.ets 1246 import web_webview from '@ohos.web.webview' 1247 1248 @Entry 1249 @Component 1250 struct WebComponent { 1251 controller: web_webview.WebviewController = new web_webview.WebviewController() 1252 build() { 1253 Column() { 1254 Web({ src: 'www.example.com', controller: this.controller }) 1255 .onAlert((event) => { 1256 AlertDialog.show({ 1257 title: 'onAlert', 1258 message: 'text', 1259 primaryButton: { 1260 value: 'cancel', 1261 action: () => { 1262 event.result.handleCancel() 1263 } 1264 }, 1265 secondaryButton: { 1266 value: 'ok', 1267 action: () => { 1268 event.result.handleConfirm() 1269 } 1270 }, 1271 cancel: () => { 1272 event.result.handleCancel() 1273 } 1274 }) 1275 return true 1276 }) 1277 } 1278 } 1279 } 1280 ``` 1281 1282### onBeforeUnload 1283 1284onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResult }) => boolean) 1285 1286Called 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. 1287 1288**Parameters** 1289 1290| Name | Type | Description | 1291| ------- | --------------------- | --------------- | 1292| url | string | URL of the web page where the dialog box is displayed.| 1293| message | string | Message displayed in the dialog box. | 1294| result | [JsResult](#jsresult) | User operation. | 1295 1296**Return value** 1297 1298| Type | Description | 1299| ------- | ---------------------------------------- | 1300| 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.| 1301 1302**Example** 1303 1304 ```ts 1305 // xxx.ets 1306 import web_webview from '@ohos.web.webview' 1307 1308 @Entry 1309 @Component 1310 struct WebComponent { 1311 controller: web_webview.WebviewController = new web_webview.WebviewController() 1312 1313 build() { 1314 Column() { 1315 Web({ src: 'www.example.com', controller: this.controller }) 1316 .onBeforeUnload((event) => { 1317 console.log("event.url:" + event.url) 1318 console.log("event.message:" + event.message) 1319 AlertDialog.show({ 1320 title: 'onBeforeUnload', 1321 message: 'text', 1322 primaryButton: { 1323 value: 'cancel', 1324 action: () => { 1325 event.result.handleCancel() 1326 } 1327 }, 1328 secondaryButton: { 1329 value: 'ok', 1330 action: () => { 1331 event.result.handleConfirm() 1332 } 1333 }, 1334 cancel: () => { 1335 event.result.handleCancel() 1336 } 1337 }) 1338 return true 1339 }) 1340 } 1341 } 1342 } 1343 ``` 1344 1345### onConfirm 1346 1347onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) => boolean) 1348 1349Called when **confirm()** is invoked by the web page. 1350 1351**Parameters** 1352 1353| Name | Type | Description | 1354| ------- | --------------------- | --------------- | 1355| url | string | URL of the web page where the dialog box is displayed.| 1356| message | string | Message displayed in the dialog box. | 1357| result | [JsResult](#jsresult) | User operation. | 1358 1359**Return value** 1360 1361| Type | Description | 1362| ------- | ---------------------------------------- | 1363| 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.| 1364 1365**Example** 1366 1367 ```ts 1368 // xxx.ets 1369 import web_webview from '@ohos.web.webview' 1370 1371 @Entry 1372 @Component 1373 struct WebComponent { 1374 controller: web_webview.WebviewController = new web_webview.WebviewController() 1375 1376 build() { 1377 Column() { 1378 Web({ src: 'www.example.com', controller: this.controller }) 1379 .onConfirm((event) => { 1380 console.log("event.url:" + event.url) 1381 console.log("event.message:" + event.message) 1382 console.log("event.result:" + event.result) 1383 AlertDialog.show({ 1384 title: 'onConfirm', 1385 message: 'text', 1386 primaryButton: { 1387 value: 'cancel', 1388 action: () => { 1389 event.result.handleCancel() 1390 } 1391 }, 1392 secondaryButton: { 1393 value: 'ok', 1394 action: () => { 1395 event.result.handleConfirm() 1396 } 1397 }, 1398 cancel: () => { 1399 event.result.handleCancel() 1400 } 1401 }) 1402 return true 1403 }) 1404 } 1405 } 1406 } 1407 ``` 1408 1409### onPrompt<sup>9+</sup> 1410 1411onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean) 1412 1413**Parameters** 1414 1415| Name | Type | Description | 1416| ------- | --------------------- | --------------- | 1417| url | string | URL of the web page where the dialog box is displayed.| 1418| message | string | Message displayed in the dialog box. | 1419| result | [JsResult](#jsresult) | User operation. | 1420 1421**Return value** 1422 1423| Type | Description | 1424| ------- | ---------------------------------------- | 1425| 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.| 1426 1427**Example** 1428 1429 ```ts 1430 // xxx.ets 1431 import web_webview from '@ohos.web.webview' 1432 1433 @Entry 1434 @Component 1435 struct WebComponent { 1436 controller: web_webview.WebviewController = new web_webview.WebviewController() 1437 1438 build() { 1439 Column() { 1440 Web({ src: 'www.example.com', controller: this.controller }) 1441 .onPrompt((event) => { 1442 console.log("url:" + event.url) 1443 console.log("message:" + event.message) 1444 console.log("value:" + event.value) 1445 AlertDialog.show({ 1446 title: 'onPrompt', 1447 message: 'text', 1448 primaryButton: { 1449 value: 'cancel', 1450 action: () => { 1451 event.result.handleCancel() 1452 } 1453 }, 1454 secondaryButton: { 1455 value: 'ok', 1456 action: () => { 1457 event.result.handleConfirm() 1458 } 1459 }, 1460 cancel: () => { 1461 event.result.handleCancel() 1462 } 1463 }) 1464 return true 1465 }) 1466 } 1467 } 1468 } 1469 ``` 1470 1471### onConsole 1472 1473onConsole(callback: (event?: { message: ConsoleMessage }) => boolean) 1474 1475Called to notify the host application of a JavaScript console message. 1476 1477**Parameters** 1478 1479| Name | Type | Description | 1480| ------- | --------------------------------- | --------- | 1481| message | [ConsoleMessage](#consolemessage) | Console message.| 1482 1483**Return value** 1484 1485| Type | Description | 1486| ------- | ----------------------------------- | 1487| boolean | Returns **true** if the message will not be printed to the console; returns **false** otherwise.| 1488 1489**Example** 1490 1491 ```ts 1492 // xxx.ets 1493 import web_webview from '@ohos.web.webview' 1494 1495 @Entry 1496 @Component 1497 struct WebComponent { 1498 controller: web_webview.WebviewController = new web_webview.WebviewController() 1499 1500 build() { 1501 Column() { 1502 Web({ src: 'www.example.com', controller: this.controller }) 1503 .onConsole((event) => { 1504 console.log('getMessage:' + event.message.getMessage()) 1505 console.log('getSourceId:' + event.message.getSourceId()) 1506 console.log('getLineNumber:' + event.message.getLineNumber()) 1507 console.log('getMessageLevel:' + event.message.getMessageLevel()) 1508 return false 1509 }) 1510 } 1511 } 1512 } 1513 ``` 1514 1515### onDownloadStart 1516 1517onDownloadStart(callback: (event?: { url: string, userAgent: string, contentDisposition: string, mimetype: string, contentLength: number }) => void) 1518 1519**Parameters** 1520 1521| Name | Type | Description | 1522| ------------------ | ------------- | ----------------------------------- | 1523| url | string | URL for the download task. | 1524| contentDisposition | string | Content-Disposition response header returned by the server, which may be empty.| 1525| mimetype | string | MIME type of the content returned by the server. | 1526| contentLength | contentLength | Length of the content returned by the server. | 1527 1528**Example** 1529 1530 ```ts 1531 // xxx.ets 1532 import web_webview from '@ohos.web.webview' 1533 1534 @Entry 1535 @Component 1536 struct WebComponent { 1537 controller: web_webview.WebviewController = new web_webview.WebviewController() 1538 1539 build() { 1540 Column() { 1541 Web({ src: 'www.example.com', controller: this.controller }) 1542 .onDownloadStart((event) => { 1543 console.log('url:' + event.url) 1544 console.log('userAgent:' + event.userAgent) 1545 console.log('contentDisposition:' + event.contentDisposition) 1546 console.log('contentLength:' + event.contentLength) 1547 console.log('mimetype:' + event.mimetype) 1548 }) 1549 } 1550 } 1551 } 1552 ``` 1553 1554### onErrorReceive 1555 1556onErrorReceive(callback: (event?: { request: WebResourceRequest, error: WebResourceError }) => void) 1557 1558Called 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. 1559 1560**Parameters** 1561 1562| Name | Type | Description | 1563| ------- | ---------------------------------------- | --------------- | 1564| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request. | 1565| error | [WebResourceError](#webresourceerror) | Encapsulation of a web page resource loading error.| 1566 1567**Example** 1568 1569 ```ts 1570 // xxx.ets 1571 import web_webview from '@ohos.web.webview' 1572 1573 @Entry 1574 @Component 1575 struct WebComponent { 1576 controller: web_webview.WebviewController = new web_webview.WebviewController() 1577 1578 build() { 1579 Column() { 1580 Web({ src: 'www.example.com', controller: this.controller }) 1581 .onErrorReceive((event) => { 1582 console.log('getErrorInfo:' + event.error.getErrorInfo()) 1583 console.log('getErrorCode:' + event.error.getErrorCode()) 1584 console.log('url:' + event.request.getRequestUrl()) 1585 console.log('isMainFrame:' + event.request.isMainFrame()) 1586 console.log('isRedirect:' + event.request.isRedirect()) 1587 console.log('isRequestGesture:' + event.request.isRequestGesture()) 1588 console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString()) 1589 let result = event.request.getRequestHeader() 1590 console.log('The request header result size is ' + result.length) 1591 for (let i of result) { 1592 console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue) 1593 } 1594 }) 1595 } 1596 } 1597 } 1598 ``` 1599 1600### onHttpErrorReceive 1601 1602onHttpErrorReceive(callback: (event?: { request: WebResourceRequest, response: WebResourceResponse }) => void) 1603 1604Called when an HTTP error (the response code is greater than or equal to 400) occurs during web page resource loading. 1605 1606**Parameters** 1607 1608| Name | Type | Description | 1609| ------- | ---------------------------------------- | --------------- | 1610| request | [WebResourceRequest](#webresourcerequest) | Encapsulation of a web page request. | 1611| response | [WebResourceResponse](#webresourceresponse) | Encapsulation of a resource response.| 1612 1613**Example** 1614 1615 ```ts 1616 // xxx.ets 1617 import web_webview from '@ohos.web.webview' 1618 1619 @Entry 1620 @Component 1621 struct WebComponent { 1622 controller: web_webview.WebviewController = new web_webview.WebviewController() 1623 1624 build() { 1625 Column() { 1626 Web({ src: 'www.example.com', controller: this.controller }) 1627 .onHttpErrorReceive((event) => { 1628 console.log('url:' + event.request.getRequestUrl()) 1629 console.log('isMainFrame:' + event.request.isMainFrame()) 1630 console.log('isRedirect:' + event.request.isRedirect()) 1631 console.log('isRequestGesture:' + event.request.isRequestGesture()) 1632 console.log('getResponseData:' + event.response.getResponseData()) 1633 console.log('getResponseEncoding:' + event.response.getResponseEncoding()) 1634 console.log('getResponseMimeType:' + event.response.getResponseMimeType()) 1635 console.log('getResponseCode:' + event.response.getResponseCode()) 1636 console.log('getReasonMessage:' + event.response.getReasonMessage()) 1637 let result = event.request.getRequestHeader() 1638 console.log('The request header result size is ' + result.length) 1639 for (let i of result) { 1640 console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue) 1641 } 1642 let resph = event.response.getResponseHeader() 1643 console.log('The response header result size is ' + resph.length) 1644 for (let i of resph) { 1645 console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue) 1646 } 1647 }) 1648 } 1649 } 1650 } 1651 ``` 1652 1653### onPageBegin 1654 1655onPageBegin(callback: (event?: { url: string }) => void) 1656 1657 1658Called 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. 1659 1660**Parameters** 1661 1662| Name | Type | Description | 1663| ---- | ------ | --------- | 1664| url | string | URL of the page.| 1665 1666**Example** 1667 1668 ```ts 1669 // xxx.ets 1670 import web_webview from '@ohos.web.webview' 1671 1672 @Entry 1673 @Component 1674 struct WebComponent { 1675 controller: web_webview.WebviewController = new web_webview.WebviewController() 1676 1677 build() { 1678 Column() { 1679 Web({ src: 'www.example.com', controller: this.controller }) 1680 .onPageBegin((event) => { 1681 console.log('url:' + event.url) 1682 }) 1683 } 1684 } 1685 } 1686 ``` 1687 1688### onPageEnd 1689 1690onPageEnd(callback: (event?: { url: string }) => void) 1691 1692 1693Called when the web page loading is complete. This API takes effect only for the main frame content. 1694 1695**Parameters** 1696 1697| Name | Type | Description | 1698| ---- | ------ | --------- | 1699| url | string | URL of the page.| 1700 1701**Example** 1702 1703 ```ts 1704 // xxx.ets 1705 import web_webview from '@ohos.web.webview' 1706 1707 @Entry 1708 @Component 1709 struct WebComponent { 1710 controller: web_webview.WebviewController = new web_webview.WebviewController() 1711 1712 build() { 1713 Column() { 1714 Web({ src: 'www.example.com', controller: this.controller }) 1715 .onPageEnd((event) => { 1716 console.log('url:' + event.url) 1717 }) 1718 } 1719 } 1720 } 1721 ``` 1722 1723### onProgressChange 1724 1725onProgressChange(callback: (event?: { newProgress: number }) => void) 1726 1727Called when the web page loading progress changes. 1728 1729**Parameters** 1730 1731| Name | Type | Description | 1732| ----------- | ------ | --------------------- | 1733| newProgress | number | New loading progress. The value is an integer ranging from 0 to 100.| 1734 1735**Example** 1736 1737 ```ts 1738 // xxx.ets 1739 import web_webview from '@ohos.web.webview' 1740 1741 @Entry 1742 @Component 1743 struct WebComponent { 1744 controller: web_webview.WebviewController = new web_webview.WebviewController() 1745 1746 build() { 1747 Column() { 1748 Web({ src: 'www.example.com', controller: this.controller }) 1749 .onProgressChange((event) => { 1750 console.log('newProgress:' + event.newProgress) 1751 }) 1752 } 1753 } 1754 } 1755 ``` 1756 1757### onTitleReceive 1758 1759onTitleReceive(callback: (event?: { title: string }) => void) 1760 1761Called when the document title of the web page is changed. 1762 1763**Parameters** 1764 1765| Name | Type | Description | 1766| ----- | ------ | ------------- | 1767| title | string | Document title.| 1768 1769**Example** 1770 1771 ```ts 1772 // xxx.ets 1773 import web_webview from '@ohos.web.webview' 1774 1775 @Entry 1776 @Component 1777 struct WebComponent { 1778 controller: web_webview.WebviewController = new web_webview.WebviewController() 1779 1780 build() { 1781 Column() { 1782 Web({ src: 'www.example.com', controller: this.controller }) 1783 .onTitleReceive((event) => { 1784 console.log('title:' + event.title) 1785 }) 1786 } 1787 } 1788 } 1789 ``` 1790 1791### onRefreshAccessedHistory 1792 1793onRefreshAccessedHistory(callback: (event?: { url: string, isRefreshed: boolean }) => void) 1794 1795Called when loading of the web page is complete. This API is used by an application to update the historical link it accessed. 1796 1797**Parameters** 1798 1799| Name | Type | Description | 1800| ----------- | ------- | ---------------------------------------- | 1801| url | string | URL to be accessed. | 1802| isRefreshed | boolean | Whether the page is reloaded. The value **true** means that the page is reloaded by invoking the [refresh](#refresh) API, and **false** means the opposite.| 1803 1804**Example** 1805 1806 ```ts 1807 // xxx.ets 1808 import web_webview from '@ohos.web.webview' 1809 1810 @Entry 1811 @Component 1812 struct WebComponent { 1813 controller: web_webview.WebviewController = new web_webview.WebviewController() 1814 1815 build() { 1816 Column() { 1817 Web({ src: 'www.example.com', controller: this.controller }) 1818 .onRefreshAccessedHistory((event) => { 1819 console.log('url:' + event.url + ' isReload:' + event.isRefreshed) 1820 }) 1821 } 1822 } 1823 } 1824 ``` 1825 1826### onRenderExited<sup>9+</sup> 1827 1828onRenderExited(callback: (event?: { renderExitReason: RenderExitReason }) => void) 1829 1830Called when the rendering process exits abnormally. 1831 1832**Parameters** 1833 1834| Name | Type | Description | 1835| ---------------- | ---------------------------------------- | ---------------- | 1836| renderExitReason | [RenderExitReason](#renderexitreason)| Cause for the abnormal exit of the rendering process.| 1837 1838**Example** 1839 1840 ```ts 1841 // xxx.ets 1842 import web_webview from '@ohos.web.webview' 1843 1844 @Entry 1845 @Component 1846 struct WebComponent { 1847 controller: web_webview.WebviewController = new web_webview.WebviewController() 1848 1849 build() { 1850 Column() { 1851 Web({ src: 'chrome://crash/', controller: this.controller }) 1852 .onRenderExited((event) => { 1853 console.log('reason:' + event.renderExitReason) 1854 }) 1855 } 1856 } 1857 } 1858 ``` 1859 1860### onShowFileSelector<sup>9+</sup> 1861 1862onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => boolean) 1863 1864Called to process an HTML form whose input type is **file**, in response to the tapping of the **Select File** button. 1865 1866**Parameters** 1867 1868| Name | Type | Description | 1869| ------------ | ---------------------------------------- | ----------------- | 1870| result | [FileSelectorResult](#fileselectorresult9) | File selection result to be sent to the **\<Web>** component.| 1871| fileSelector | [FileSelectorParam](#fileselectorparam9) | Information about the file selector. | 1872 1873**Return value** 1874 1875| Type | Description | 1876| ------- | ---------------------------------------- | 1877| 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.| 1878 1879**Example** 1880 1881 ```ts 1882 // xxx.ets 1883 import web_webview from '@ohos.web.webview' 1884 1885 @Entry 1886 @Component 1887 struct WebComponent { 1888 controller: web_webview.WebviewController = new web_webview.WebviewController() 1889 1890 build() { 1891 Column() { 1892 Web({ src: 'www.example.com', controller: this.controller }) 1893 .onShowFileSelector((event) => { 1894 AlertDialog.show({ 1895 title: event.fileSelector.getTitle(), 1896 message: 'isCapture:' + event.fileSelector.isCapture() + " mode:" + event.fileSelector.getMode() + 'acceptType:' + event.fileSelector.getAcceptType(), 1897 confirm: { 1898 value: 'upload', 1899 action: () => { 1900 let fileList: Array<string> = [ 1901 '/data/storage/el2/base/test', 1902 ] 1903 event.result.handleFileList(fileList) 1904 } 1905 }, 1906 cancel: () => { 1907 let fileList: Array<string> = [] 1908 event.result.handleFileList(fileList) 1909 } 1910 }) 1911 return true 1912 }) 1913 } 1914 } 1915 } 1916 ``` 1917 1918### onResourceLoad<sup>9+</sup> 1919 1920onResourceLoad(callback: (event: {url: string}) => void) 1921 1922Called to notify the **\<Web>** component of the URL of the loaded resource file. 1923 1924**Parameters** 1925 1926| Name | Type | Description | 1927| ---- | ------ | -------------- | 1928| url | string | URL of the loaded resource file.| 1929 1930**Example** 1931 1932 ```ts 1933 // xxx.ets 1934 import web_webview from '@ohos.web.webview' 1935 1936 @Entry 1937 @Component 1938 struct WebComponent { 1939 controller: web_webview.WebviewController = new web_webview.WebviewController() 1940 1941 build() { 1942 Column() { 1943 Web({ src: 'www.example.com', controller: this.controller }) 1944 .onResourceLoad((event) => { 1945 console.log('onResourceLoad: ' + event.url) 1946 }) 1947 } 1948 } 1949 } 1950 ``` 1951 1952### onScaleChange<sup>9+</sup> 1953 1954onScaleChange(callback: (event: {oldScale: number, newScale: number}) => void) 1955 1956Called when the display ratio of this page changes. 1957 1958**Parameters** 1959 1960| Name | Type | Description | 1961| -------- | ------ | ------------ | 1962| oldScale | number | Display ratio of the page before the change.| 1963| newScale | number | Display ratio of the page after the change.| 1964 1965**Example** 1966 1967 ```ts 1968 // xxx.ets 1969 import web_webview from '@ohos.web.webview' 1970 1971 @Entry 1972 @Component 1973 struct WebComponent { 1974 controller: web_webview.WebviewController = new web_webview.WebviewController() 1975 1976 build() { 1977 Column() { 1978 Web({ src: 'www.example.com', controller: this.controller }) 1979 .onScaleChange((event) => { 1980 console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale) 1981 }) 1982 } 1983 } 1984 } 1985 ``` 1986 1987### onUrlLoadIntercept 1988 1989onUrlLoadIntercept(callback: (event?: { data:string | WebResourceRequest }) => boolean) 1990 1991Called 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. 1992 1993**Parameters** 1994 1995| Name | Type | Description | 1996| ---- | ---------------------------------------- | --------- | 1997| data | string / [WebResourceRequest](#webresourcerequest) | URL information.| 1998 1999**Return value** 2000 2001| Type | Description | 2002| ------- | ------------------------ | 2003| boolean | Returns **true** if the access is blocked; returns **false** otherwise.| 2004 2005**Example** 2006 2007 ```ts 2008 // xxx.ets 2009 import web_webview from '@ohos.web.webview' 2010 2011 @Entry 2012 @Component 2013 struct WebComponent { 2014 controller: web_webview.WebviewController = new web_webview.WebviewController() 2015 2016 build() { 2017 Column() { 2018 Web({ src: 'www.example.com', controller: this.controller }) 2019 .onUrlLoadIntercept((event) => { 2020 console.log('onUrlLoadIntercept ' + event.data.toString()) 2021 return true 2022 }) 2023 } 2024 } 2025 } 2026 ``` 2027 2028### onInterceptRequest<sup>9+</sup> 2029 2030onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse) 2031 2032Called when the **\<Web>** component is about to access a URL. This API is used to block the URL and return the response data. 2033 2034**Parameters** 2035 2036| Name | Type | Description | 2037| ------- | ---------------------------------------- | ----------- | 2038| request | [WebResourceRequest](#webresourcerequest) | Information about the URL request.| 2039 2040**Return value** 2041 2042| Type | Description | 2043| ---------------------------------------- | ---------------------------------------- | 2044| [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.| 2045 2046**Example** 2047 2048 ```ts 2049 // xxx.ets 2050 import web_webview from '@ohos.web.webview' 2051 2052 @Entry 2053 @Component 2054 struct WebComponent { 2055 controller: web_webview.WebviewController = new web_webview.WebviewController() 2056 responseweb: WebResourceResponse = new WebResourceResponse() 2057 heads:Header[] = new Array() 2058 @State webdata: string = "<!DOCTYPE html>\n" + 2059 "<html>\n"+ 2060 "<head>\n"+ 2061 "<title>intercept test</title>\n"+ 2062 "</head>\n"+ 2063 "<body>\n"+ 2064 "<h1>intercept test</h1>\n"+ 2065 "</body>\n"+ 2066 "</html>" 2067 build() { 2068 Column() { 2069 Web({ src: 'www.example.com', controller: this.controller }) 2070 .onInterceptRequest((event) => { 2071 console.log('url:' + event.request.getRequestUrl()) 2072 var head1:Header = { 2073 headerKey:"Connection", 2074 headerValue:"keep-alive" 2075 } 2076 var head2:Header = { 2077 headerKey:"Cache-Control", 2078 headerValue:"no-cache" 2079 } 2080 var length = this.heads.push(head1) 2081 length = this.heads.push(head2) 2082 this.responseweb.setResponseHeader(this.heads) 2083 this.responseweb.setResponseData(this.webdata) 2084 this.responseweb.setResponseEncoding('utf-8') 2085 this.responseweb.setResponseMimeType('text/html') 2086 this.responseweb.setResponseCode(200) 2087 this.responseweb.setReasonMessage('OK') 2088 return this.responseweb 2089 }) 2090 } 2091 } 2092 } 2093 ``` 2094 2095### onHttpAuthRequest<sup>9+</sup> 2096 2097onHttpAuthRequest(callback: (event?: { handler: HttpAuthHandler, host: string, realm: string}) => boolean) 2098 2099Called when an HTTP authentication request is received. 2100 2101**Parameters** 2102 2103| Name | Type | Description | 2104| ------- | ------------------------------------ | ---------------- | 2105| handler | [HttpAuthHandler](#httpauthhandler9) | User operation. | 2106| host | string | Host to which HTTP authentication credentials apply.| 2107| realm | string | Realm to which HTTP authentication credentials apply. | 2108 2109**Return value** 2110 2111| Type | Description | 2112| ------- | --------------------- | 2113| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.| 2114 2115**Example** 2116 2117 ```ts 2118 // xxx.ets 2119 import web_webview from '@ohos.web.webview' 2120 @Entry 2121 @Component 2122 struct WebComponent { 2123 controller: web_webview.WebviewController = new web_webview.WebviewController() 2124 httpAuth: boolean = false 2125 2126 build() { 2127 Column() { 2128 Web({ src: 'www.example.com', controller: this.controller }) 2129 .onHttpAuthRequest((event) => { 2130 AlertDialog.show({ 2131 title: 'onHttpAuthRequest', 2132 message: 'text', 2133 primaryButton: { 2134 value: 'cancel', 2135 action: () => { 2136 event.handler.cancel() 2137 } 2138 }, 2139 secondaryButton: { 2140 value: 'ok', 2141 action: () => { 2142 this.httpAuth = event.handler.isHttpAuthInfoSaved() 2143 if (this.httpAuth == false) { 2144 web_webview.WebDataBase.saveHttpAuthCredentials( 2145 event.host, 2146 event.realm, 2147 "2222", 2148 "2222" 2149 ) 2150 event.handler.cancel() 2151 } 2152 } 2153 }, 2154 cancel: () => { 2155 event.handler.cancel() 2156 } 2157 }) 2158 return true 2159 }) 2160 } 2161 } 2162 } 2163 ``` 2164### onSslErrorEventReceive<sup>9+</sup> 2165 2166onSslErrorEventReceive(callback: (event: { handler: SslErrorHandler, error: SslError }) => void) 2167 2168Called when an SSL error occurs during resource loading. 2169 2170**Parameters** 2171 2172| Name | Type | Description | 2173| ------- | ------------------------------------ | -------------- | 2174| handler | [SslErrorHandler](#sslerrorhandler9) | User operation.| 2175| error | [SslError](#sslerror9) | Error code. | 2176 2177**Example** 2178 2179 ```ts 2180 // xxx.ets 2181 import web_webview from '@ohos.web.webview' 2182 @Entry 2183 @Component 2184 struct WebComponent { 2185 controller: web_webview.WebviewController = new web_webview.WebviewController() 2186 2187 build() { 2188 Column() { 2189 Web({ src: 'www.example.com', controller: this.controller }) 2190 .onSslErrorEventReceive((event) => { 2191 AlertDialog.show({ 2192 title: 'onSslErrorEventReceive', 2193 message: 'text', 2194 primaryButton: { 2195 value: 'confirm', 2196 action: () => { 2197 event.handler.handleConfirm() 2198 } 2199 }, 2200 secondaryButton: { 2201 value: 'cancel', 2202 action: () => { 2203 event.handler.handleCancel() 2204 } 2205 }, 2206 cancel: () => { 2207 event.handler.handleCancel() 2208 } 2209 }) 2210 return true 2211 }) 2212 } 2213 } 2214 } 2215 ``` 2216 2217### onClientAuthenticationRequest<sup>9+</sup> 2218 2219onClientAuthenticationRequest(callback: (event: {handler : ClientAuthenticationHandler, host : string, port : number, keyTypes : Array<string>, issuers : Array<string>}) => void) 2220 2221Called when an SSL client certificate request is received. 2222 2223**Parameters** 2224 2225| Name | Type | Description | 2226| -------- | ---------------------------------------- | --------------- | 2227| handler | [ClientAuthenticationHandler](#clientauthenticationhandler9) | User operation. | 2228| host | string | Host name of the server that requests a certificate. | 2229| port | number | Port number of the server that requests a certificate. | 2230| keyTypes | Array<string> | Acceptable asymmetric private key types. | 2231| issuers | Array<string> | Issuer of the certificate that matches the private key.| 2232 2233 **Example** 2234 ```ts 2235 // xxx.ets 2236 import web_webview from '@ohos.web.webview' 2237 @Entry 2238 @Component 2239 struct WebComponent { 2240 controller: web_webview.WebviewController = new web_webview.WebviewController() 2241 2242 build() { 2243 Column() { 2244 Web({ src: 'www.example.com', controller: this.controller }) 2245 .onClientAuthenticationRequest((event) => { 2246 AlertDialog.show({ 2247 title: 'onClientAuthenticationRequest', 2248 message: 'text', 2249 primaryButton: { 2250 value: 'confirm', 2251 action: () => { 2252 event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem") 2253 } 2254 }, 2255 secondaryButton: { 2256 value: 'cancel', 2257 action: () => { 2258 event.handler.cancel() 2259 } 2260 }, 2261 cancel: () => { 2262 event.handler.ignore() 2263 } 2264 }) 2265 return true 2266 }) 2267 } 2268 } 2269 } 2270 ``` 2271 2272### onPermissionRequest<sup>9+</sup> 2273 2274onPermissionRequest(callback: (event?: { request: PermissionRequest }) => void) 2275 2276Called when a permission request is received. 2277 2278**Parameters** 2279 2280| Name | Type | Description | 2281| ------- | ---------------------------------------- | -------------- | 2282| request | [PermissionRequest](#permissionrequest9) | User operation.| 2283 2284**Example** 2285 2286 ```ts 2287 // xxx.ets 2288 import web_webview from '@ohos.web.webview' 2289 2290 @Entry 2291 @Component 2292 struct WebComponent { 2293 controller: web_webview.WebviewController = new web_webview.WebviewController() 2294 build() { 2295 Column() { 2296 Web({ src: 'www.example.com', controller: this.controller }) 2297 .onPermissionRequest((event) => { 2298 AlertDialog.show({ 2299 title: 'title', 2300 message: 'text', 2301 primaryButton: { 2302 value: 'deny', 2303 action: () => { 2304 event.request.deny() 2305 } 2306 }, 2307 secondaryButton: { 2308 value: 'onConfirm', 2309 action: () => { 2310 event.request.grant(event.request.getAccessibleResource()) 2311 } 2312 }, 2313 cancel: () => { 2314 event.request.deny() 2315 } 2316 }) 2317 }) 2318 } 2319 } 2320 } 2321 ``` 2322 2323### onContextMenuShow<sup>9+</sup> 2324 2325onContextMenuShow(callback: (event?: { param: WebContextMenuParam, result: WebContextMenuResult }) => boolean) 2326 2327Shows a context menu after the user clicks the right mouse button or long presses a specific element, such as an image or a link. 2328 2329**Parameters** 2330 2331| Name | Type | Description | 2332| ------ | ---------------------------------------- | ----------- | 2333| param | [WebContextMenuParam](#webcontextmenuparam9) | Parameters related to the context menu. | 2334| result | [WebContextMenuResult](#webcontextmenuresult9) | Result of the context menu.| 2335 2336**Return value** 2337 2338| Type | Description | 2339| ------- | ------------------------ | 2340| boolean | The value **true** means a custom menu, and **false** means the default menu.| 2341 2342**Example** 2343 2344 ```ts 2345 // xxx.ets 2346 import web_webview from '@ohos.web.webview' 2347 2348 @Entry 2349 @Component 2350 struct WebComponent { 2351 controller: web_webview.WebviewController = new web_webview.WebviewController() 2352 build() { 2353 Column() { 2354 Web({ src: 'www.example.com', controller: this.controller }) 2355 .onContextMenuShow((event) => { 2356 console.info("x coord = " + event.param.x()) 2357 console.info("link url = " + event.param.getLinkUrl()) 2358 return true 2359 }) 2360 } 2361 } 2362 } 2363 ``` 2364 2365### onScroll<sup>9+</sup> 2366 2367onScroll(callback: (event: {xOffset: number, yOffset: number}) => void) 2368 2369Called when the scrollbar of the page scrolls. 2370 2371**Parameters** 2372 2373| Name | Type | Description | 2374| ------- | ------ | ------------ | 2375| xOffset | number | Position of the scrollbar on the x-axis relative to the leftmost of the web page.| 2376| yOffset | number | Position of the scrollbar on the y-axis relative to the top of the web page.| 2377 2378**Example** 2379 2380 ```ts 2381 // xxx.ets 2382 import web_webview from '@ohos.web.webview' 2383 2384 @Entry 2385 @Component 2386 struct WebComponent { 2387 controller: web_webview.WebviewController = new web_webview.WebviewController() 2388 build() { 2389 Column() { 2390 Web({ src: 'www.example.com', controller: this.controller }) 2391 .onScroll((event) => { 2392 console.info("x = " + event.xOffset) 2393 console.info("y = " + event.yOffset) 2394 }) 2395 } 2396 } 2397 } 2398 ``` 2399 2400### onGeolocationShow 2401 2402onGeolocationShow(callback: (event?: { origin: string, geolocation: JsGeolocation }) => void) 2403 2404Registers a callback for receiving a request to obtain the geolocation information. 2405 2406**Parameters** 2407 2408| Name | Type | Description | 2409| ----------- | ------------------------------- | -------------- | 2410| origin | string | Index of the origin. | 2411| geolocation | [JsGeolocation](#jsgeolocation) | User operation.| 2412 2413**Example** 2414 2415 ```ts 2416 // xxx.ets 2417 import web_webview from '@ohos.web.webview' 2418 2419 @Entry 2420 @Component 2421 struct WebComponent { 2422 controller: web_webview.WebviewController = new web_webview.WebviewController() 2423 build() { 2424 Column() { 2425 Web({ src:'www.example.com', controller:this.controller }) 2426 .geolocationAccess(true) 2427 .onGeolocationShow((event) => { 2428 AlertDialog.show({ 2429 title: 'title', 2430 message: 'text', 2431 confirm: { 2432 value: 'onConfirm', 2433 action: () => { 2434 event.geolocation.invoke(event.origin, true, true) 2435 } 2436 }, 2437 cancel: () => { 2438 event.geolocation.invoke(event.origin, false, true) 2439 } 2440 }) 2441 }) 2442 } 2443 } 2444 } 2445 ``` 2446 2447### onGeolocationHide 2448 2449onGeolocationHide(callback: () => void) 2450 2451Called to notify the user that the request for obtaining the geolocation information received when **[onGeolocationShow](#ongeolocationshow)** is called has been canceled. 2452 2453**Parameters** 2454 2455| Name | Type | Description | 2456| -------- | ---------- | -------------------- | 2457| callback | () => void | Callback invoked when the request for obtaining geolocation information has been canceled. | 2458 2459**Example** 2460 2461 ```ts 2462 // xxx.ets 2463 import web_webview from '@ohos.web.webview' 2464 2465 @Entry 2466 @Component 2467 struct WebComponent { 2468 controller: web_webview.WebviewController = new web_webview.WebviewController() 2469 build() { 2470 Column() { 2471 Web({ src:'www.example.com', controller:this.controller }) 2472 .geolocationAccess(true) 2473 .onGeolocationHide(() => { 2474 console.log("onGeolocationHide...") 2475 }) 2476 } 2477 } 2478 } 2479 ``` 2480 2481### onFullScreenEnter<sup>9+</sup> 2482 2483onFullScreenEnter(callback: (event: { handler: FullScreenExitHandler }) => void) 2484 2485Called when the component enters full screen mode. 2486 2487**Parameters** 2488 2489| Name | Type | Description | 2490| ------- | ---------------------------------------- | -------------- | 2491| handler | [FullScreenExitHandler](#fullscreenexithandler9) | Function handle for exiting full screen mode.| 2492 2493**Example** 2494 2495 ```ts 2496 // xxx.ets 2497 import web_webview from '@ohos.web.webview' 2498 2499 @Entry 2500 @Component 2501 struct WebComponent { 2502 controller: web_webview.WebviewController = new web_webview.WebviewController() 2503 handler: FullScreenExitHandler = null 2504 build() { 2505 Column() { 2506 Web({ src:'www.example.com', controller:this.controller }) 2507 .onFullScreenEnter((event) => { 2508 console.log("onFullScreenEnter...") 2509 this.handler = event.handler 2510 }) 2511 } 2512 } 2513 } 2514 ``` 2515 2516### onFullScreenExit<sup>9+</sup> 2517 2518onFullScreenExit(callback: () => void) 2519 2520Called when the component exits full screen mode. 2521 2522**Parameters** 2523 2524| Name | Type | Description | 2525| -------- | ---------- | ------------- | 2526| callback | () => void | Callback invoked when the component exits full screen mode.| 2527 2528**Example** 2529 2530 ```ts 2531 // xxx.ets 2532 import web_webview from '@ohos.web.webview' 2533 2534 @Entry 2535 @Component 2536 struct WebComponent { 2537 controller: web_webview.WebviewController = new web_webview.WebviewController() 2538 handler: FullScreenExitHandler = null 2539 build() { 2540 Column() { 2541 Web({ src:'www.example.com', controller:this.controller }) 2542 .onFullScreenExit(() => { 2543 console.log("onFullScreenExit...") 2544 this.handler.exitFullScreen() 2545 }) 2546 .onFullScreenEnter((event) => { 2547 this.handler = event.handler 2548 }) 2549 } 2550 } 2551 } 2552 ``` 2553 2554### onWindowNew<sup>9+</sup> 2555 2556onWindowNew(callback: (event: {isAlert: boolean, isUserTrigger: boolean, targetUrl: string, handler: ControllerHandler}) => void) 2557 2558Registers a callback for window creation. 2559 2560**Parameters** 2561 2562| Name | Type | Description | 2563| ------------- | ---------------------------------------- | -------------------------- | 2564| 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.| 2565| 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. | 2566| targetUrl | string | Target URL. | 2567| handler | [ControllerHandler](#controllerhandler9) | **WebviewController** instance for setting the new window. | 2568 2569**Example** 2570 2571 ```ts 2572 // xxx.ets 2573 import web_webview from '@ohos.web.webview' 2574 @Entry 2575 @Component 2576 struct WebComponent { 2577 controller: web_webview.WebviewController = new web_webview.WebviewController() 2578 build() { 2579 Column() { 2580 Web({ src:'www.example.com', controller: this.controller }) 2581 .multiWindowAccess(true) 2582 .onWindowNew((event) => { 2583 console.log("onWindowNew...") 2584 var popController: web_webview.WebviewController = new web_webview.WebviewController() 2585 event.handler.setWebController(popController) 2586 }) 2587 } 2588 } 2589 } 2590 ``` 2591 2592### onWindowExit<sup>9+</sup> 2593 2594onWindowExit(callback: () => void) 2595 2596Registers a callback for window closure. 2597 2598**Parameters** 2599 2600| Name | Type | Description | 2601| -------- | ---------- | ------------ | 2602| callback | () => void | Callback invoked when the window closes.| 2603 2604**Example** 2605 2606 ```ts 2607 // xxx.ets 2608 import web_webview from '@ohos.web.webview' 2609 2610 @Entry 2611 @Component 2612 struct WebComponent { 2613 controller: web_webview.WebviewController = new web_webview.WebviewController() 2614 build() { 2615 Column() { 2616 Web({ src:'www.example.com', controller: this.controller }) 2617 .onWindowExit(() => { 2618 console.log("onWindowExit...") 2619 }) 2620 } 2621 } 2622 } 2623 ``` 2624 2625### onSearchResultReceive<sup>9+</sup> 2626 2627onSearchResultReceive(callback: (event?: {activeMatchOrdinal: number, numberOfMatches: number, isDoneCounting: boolean}) => void): WebAttribute 2628 2629Called to notify the caller of the search result on the web page. 2630 2631**Parameters** 2632 2633| Name | Type | Description | 2634| ------------------ | ------- | ---------------------------------------- | 2635| activeMatchOrdinal | number | Sequence number of the current match, which starts from 0. | 2636| numberOfMatches | number | Total number of matches. | 2637| isDoneCounting | boolean | Whether the search operation on the current page is complete. This API may be called multiple times until **isDoneCounting** is **true**.| 2638 2639**Example** 2640 2641 ```ts 2642 // xxx.ets 2643 import web_webview from '@ohos.web.webview' 2644 2645 @Entry 2646 @Component 2647 struct WebComponent { 2648 controller: web_webview.WebviewController = new web_webview.WebviewController() 2649 2650 build() { 2651 Column() { 2652 Web({ src: 'www.example.com', controller: this.controller }) 2653 .onSearchResultReceive(ret => { 2654 console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal + 2655 "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting) 2656 }) 2657 } 2658 } 2659 } 2660 ``` 2661 2662### onDataResubmitted<sup>9+</sup> 2663 2664onDataResubmitted(callback: (event: {handler: DataResubmissionHandler}) => void) 2665 2666Called when the web form data is resubmitted. 2667 2668**Parameters** 2669 2670| Name | Type | Description | 2671| ------- | ---------------------------------------------------- | ---------------------- | 2672| handler | [DataResubmissionHandler](#dataresubmissionhandler9) | Handler for resubmitting web form data.| 2673 2674**Example** 2675 2676 ```ts 2677 // xxx.ets 2678 import web_webview from '@ohos.web.webview' 2679 @Entry 2680 @Component 2681 struct WebComponent { 2682 controller: web_webview.WebviewController = new web_webview.WebviewController() 2683 build() { 2684 Column() { 2685 Web({ src:'www.example.com', controller: this.controller }) 2686 .onDataResubmitted((event) => { 2687 console.log('onDataResubmitted') 2688 event.handler.resend(); 2689 }) 2690 } 2691 } 2692 } 2693 ``` 2694 2695### onPageVisible<sup>9+</sup> 2696 2697onPageVisible(callback: (event: {url: string}) => void) 2698 2699Called when the old page is not displayed and the new page is about to be visible. 2700 2701**Parameters** 2702 2703| Name| Type| Description | 2704| ------ | -------- | ------------------------------------------------- | 2705| url | string | URL of the new page that is able to be visible when the old page is not displayed.| 2706 2707**Example** 2708 2709 ```ts 2710 // xxx.ets 2711 import web_webview from '@ohos.web.webview' 2712 @Entry 2713 @Component 2714 struct WebComponent { 2715 controller: web_webview.WebviewController = new web_webview.WebviewController() 2716 build() { 2717 Column() { 2718 Web({ src:'www.example.com', controller: this.controller }) 2719 .onPageVisible((event) => { 2720 console.log('onPageVisible url:' + event.url) 2721 }) 2722 } 2723 } 2724 } 2725 ``` 2726 2727### onInterceptKeyEvent<sup>9+</sup> 2728 2729onInterceptKeyEvent(callback: (event: KeyEvent) => boolean) 2730 2731Called when the key event is intercepted and before it is consumed by the Webview. 2732 2733**Parameters** 2734 2735| Name| Type | Description | 2736| ------ | ------------------------------------------------------- | -------------------- | 2737| event | [KeyEvent](ts-universal-events-key.md#keyevent) | Key event that is triggered.| 2738 2739**Return value** 2740 2741| Type | Description | 2742| ------- | ------------------------------------------------------------ | 2743| boolean | Whether to continue to transfer the key event to the Webview kernel.| 2744 2745**Example** 2746 2747 ```ts 2748 // xxx.ets 2749 import web_webview from '@ohos.web.webview' 2750 @Entry 2751 @Component 2752 struct WebComponent { 2753 controller: web_webview.WebviewController = new web_webview.WebviewController() 2754 build() { 2755 Column() { 2756 Web({ src:'www.example.com', controller: this.controller }) 2757 .onInterceptKeyEvent((event) => { 2758 if (event.keyCode == 2017 || event.keyCode == 2018) { 2759 console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`) 2760 return true; 2761 } 2762 return false; 2763 }) 2764 } 2765 } 2766 } 2767 ``` 2768 2769### onTouchIconUrlReceived<sup>9+</sup> 2770 2771onTouchIconUrlReceived(callback: (event: {url: string, precomposed: boolean}) => void) 2772 2773Called when an apple-touch-icon URL is received. 2774 2775**Parameters** 2776 2777| Name | Type| Description | 2778| ----------- | -------- | ---------------------------------- | 2779| url | string | Received apple-touch-icon URL.| 2780| precomposed | boolean | Whether the apple-touch-icon is precomposed.| 2781 2782**Example** 2783 2784 ```ts 2785 // xxx.ets 2786 import web_webview from '@ohos.web.webview' 2787 @Entry 2788 @Component 2789 struct WebComponent { 2790 controller: web_webview.WebviewController = new web_webview.WebviewController() 2791 build() { 2792 Column() { 2793 Web({ src:'www.baidu.com', controller: this.controller }) 2794 .onTouchIconUrlReceived((event) => { 2795 console.log('onTouchIconUrlReceived:' + JSON.stringify(event)) 2796 }) 2797 } 2798 } 2799 } 2800 ``` 2801 2802### onFaviconReceived<sup>9+</sup> 2803 2804onFaviconReceived(callback: (event: {favicon: image.PixelMap}) => void) 2805 2806Called when this web page receives a new favicon. 2807 2808**Parameters** 2809 2810| Name | Type | Description | 2811| ------- | ---------------------------------------------- | ----------------------------------- | 2812| favicon | [PixelMap](../apis/js-apis-image.md#pixelmap7) | **PixelMap** object of the received favicon.| 2813 2814**Example** 2815 2816 ```ts 2817 // xxx.ets 2818 import web_webview from '@ohos.web.webview' 2819 import image from "@ohos.multimedia.image" 2820 @Entry 2821 @Component 2822 struct WebComponent { 2823 controller: web_webview.WebviewController = new web_webview.WebviewController() 2824 @State icon: image.PixelMap = undefined; 2825 build() { 2826 Column() { 2827 Web({ src:'www.example.com', controller: this.controller }) 2828 .onFaviconReceived((event) => { 2829 console.log('onFaviconReceived:' + JSON.stringify(event)) 2830 this.icon = event.favicon; 2831 }) 2832 } 2833 } 2834 } 2835 ``` 2836 2837## ConsoleMessage 2838 2839Implements the **ConsoleMessage** object. For the sample code, see [onConsole](#onconsole). 2840 2841### getLineNumber 2842 2843getLineNumber(): number 2844 2845Obtains the number of rows in this console message. 2846 2847**Return value** 2848 2849| Type | Description | 2850| ------ | -------------------- | 2851| number | Number of rows in the console message.| 2852 2853### getMessage 2854 2855getMessage(): string 2856 2857Obtains the log information of this console message. 2858 2859**Return value** 2860 2861| Type | Description | 2862| ------ | ---------------------- | 2863| string | Log information of the console message.| 2864 2865### getMessageLevel 2866 2867getMessageLevel(): MessageLevel 2868 2869Obtains the level of this console message. 2870 2871**Return value** 2872 2873| Type | Description | 2874| --------------------------------- | ---------------------- | 2875| [MessageLevel](#messagelevel)| Level of the console message.| 2876 2877### getSourceId 2878 2879getSourceId(): string 2880 2881Obtains the path and name of the web page source file. 2882 2883**Return value** 2884 2885| Type | Description | 2886| ------ | ------------- | 2887| string | Path and name of the web page source file.| 2888 2889## JsResult 2890 2891Implements the **JsResult** object, which indicates the result returned to the **\<Web>** component to indicate the user operation performed in the dialog box. For the sample code, see [onAlert Event](#onalert). 2892 2893### handleCancel 2894 2895handleCancel(): void 2896 2897Notifies the **\<Web>** component of the user's cancel operation in the dialog box. 2898 2899### handleConfirm 2900 2901handleConfirm(): void 2902 2903Notifies the **\<Web>** component of the user's confirm operation in the dialog box. 2904 2905### handlePromptConfirm<sup>9+</sup> 2906 2907handlePromptConfirm(result: string): void 2908 2909Notifies the **\<Web>** component of the user's confirm operation in the dialog box as well as the dialog box content. 2910 2911**Parameters** 2912 2913| Name | Type | Mandatory | Default Value | Description | 2914| ------ | ------ | ---- | ---- | ----------- | 2915| result | string | Yes | - | User input in the dialog box.| 2916 2917## FullScreenExitHandler<sup>9+</sup> 2918 2919Implements a **FullScreenExitHandler** object for listening for exiting full screen mode. For the sample code, see [onFullScreenEnter](#onfullscreenenter9). 2920 2921### exitFullScreen<sup>9+</sup> 2922 2923exitFullScreen(): void 2924 2925Exits full screen mode. 2926 2927## ControllerHandler<sup>9+</sup> 2928 2929Implements a **WebviewController** object for new **\<Web>** components. For the sample code, see [onWindowNew](#onwindownew9). 2930 2931### setWebController<sup>9+</sup> 2932 2933setWebController(controller: WebviewController): void 2934 2935Sets a **WebviewController** object. 2936 2937**Parameters** 2938 2939| Name | Type | Mandatory | Default Value | Description | 2940| ---------- | ------------- | ---- | ---- | ------------------------- | 2941| controller | [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | Yes | - | **WebviewController** object of the **\<Web>** component.| 2942 2943## WebResourceError 2944 2945Implements the **WebResourceError** object. For the sample code, see [onErrorReceive](#onerrorreceive). 2946 2947### getErrorCode 2948 2949getErrorCode(): number 2950 2951Obtains the error code for resource loading. 2952 2953**Return value** 2954 2955| Type | Description | 2956| ------ | ----------- | 2957| number | Error code for resource loading.| 2958 2959### getErrorInfo 2960 2961getErrorInfo(): string 2962 2963Obtains error information about resource loading. 2964 2965**Return value** 2966 2967| Type | Description | 2968| ------ | ------------ | 2969| string | Error information about resource loading.| 2970 2971## WebResourceRequest 2972 2973Implements the **WebResourceRequest** object. For the sample code, see [onErrorReceive](#onerrorreceive). 2974 2975### getRequestHeader 2976 2977getResponseHeader() : Array\<Header\> 2978 2979Obtains the information about the resource request header. 2980 2981**Return value** 2982 2983| Type | Description | 2984| -------------------------- | ---------- | 2985| Array\<[Header](#header)\> | Information about the resource request header.| 2986 2987### getRequestUrl 2988 2989getRequestUrl(): string 2990 2991Obtains the URL of the resource request. 2992 2993**Return value** 2994 2995| Type | Description | 2996| ------ | ------------- | 2997| string | URL of the resource request.| 2998 2999### isMainFrame 3000 3001isMainFrame(): boolean 3002 3003Checks whether the resource request is in the main frame. 3004 3005**Return value** 3006 3007| Type | Description | 3008| ------- | ---------------- | 3009| boolean | Whether the resource request is in the main frame.| 3010 3011### isRedirect 3012 3013isRedirect(): boolean 3014 3015Checks whether the resource request is redirected by the server. 3016 3017**Return value** 3018 3019| Type | Description | 3020| ------- | ---------------- | 3021| boolean | Whether the resource request is redirected by the server.| 3022 3023### isRequestGesture 3024 3025isRequestGesture(): boolean 3026 3027Checks whether the resource request is associated with a gesture (for example, a tap). 3028 3029**Return value** 3030 3031| Type | Description | 3032| ------- | -------------------- | 3033| boolean | Whether the resource request is associated with a gesture (for example, a tap).| 3034 3035## Header 3036 3037Describes the request/response header returned by the **\<Web>** component. 3038 3039| Name | Type | Description | 3040| ----------- | ------ | ------------- | 3041| headerKey | string | Key of the request/response header. | 3042| headerValue | string | Value of the request/response header.| 3043 3044 3045## WebResourceResponse 3046 3047Implements the **WebResourceResponse** object. For the sample code, see [onHttpErrorReceive](#onhttperrorreceive). 3048 3049### getReasonMessage 3050 3051getReasonMessage(): string 3052 3053Obtains the status code description of the resource response. 3054 3055**Return value** 3056 3057| Type | Description | 3058| ------ | ------------- | 3059| string | Status code description of the resource response.| 3060 3061### getResponseCode 3062 3063getResponseCode(): number 3064 3065Obtains the status code of the resource response. 3066 3067**Return value** 3068 3069| Type | Description | 3070| ------ | ----------- | 3071| number | Status code of the resource response.| 3072 3073### getResponseData 3074 3075getResponseData(): string 3076 3077Obtains the data in the resource response. 3078 3079**Return value** 3080 3081| Type | Description | 3082| ------ | --------- | 3083| string | Data in the resource response.| 3084 3085### getResponseEncoding 3086 3087getResponseEncoding(): string 3088 3089Obtains the encoding string of the resource response. 3090 3091**Return value** 3092 3093| Type | Description | 3094| ------ | ---------- | 3095| string | Encoding string of the resource response.| 3096 3097### getResponseHeader 3098 3099getResponseHeader() : Array\<Header\> 3100 3101Obtains the resource response header. 3102 3103**Return value** 3104 3105| Type | Description | 3106| -------------------------- | -------- | 3107| Array\<[Header](#header)\> | Resource response header.| 3108 3109### getResponseMimeType 3110 3111getResponseMimeType(): string 3112 3113Obtains the MIME type of the resource response. 3114 3115**Return value** 3116 3117| Type | Description | 3118| ------ | ------------------ | 3119| string | MIME type of the resource response.| 3120 3121### setResponseData<sup>9+</sup> 3122 3123setResponseData(data: string | number) 3124 3125Sets the data in the resource response. 3126 3127**Parameters** 3128 3129| Name| Type | Mandatory| Default Value| Description | 3130| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ | 3131| data | string \| number | Yes | - | Resource response data to set. When set to a number, the value indicates a file handle.| 3132 3133### setResponseEncoding<sup>9+</sup> 3134 3135setResponseEncoding(encoding: string) 3136 3137Sets the encoding string of the resource response. 3138 3139**Parameters** 3140 3141| Name | Type | Mandatory | Default Value | Description | 3142| -------- | ------ | ---- | ---- | ------------ | 3143| encoding | string | Yes | - | Encoding string to set.| 3144 3145### setResponseMimeType<sup>9+</sup> 3146 3147setResponseMimeType(mimeType: string) 3148 3149Sets the MIME type of the resource response. 3150 3151**Parameters** 3152 3153| Name | Type | Mandatory | Default Value | Description | 3154| -------- | ------ | ---- | ---- | -------------------- | 3155| mimeType | string | Yes | - | MIME type to set.| 3156 3157### setReasonMessage<sup>9+</sup> 3158 3159setReasonMessage(reason: string) 3160 3161Sets the status code description of the resource response. 3162 3163**Parameters** 3164 3165| Name | Type | Mandatory | Default Value | Description | 3166| ------ | ------ | ---- | ---- | --------------- | 3167| reason | string | Yes | - | Status code description to set.| 3168 3169### setResponseHeader<sup>9+</sup> 3170 3171setResponseHeader(header: Array\<Header\>) 3172 3173Sets the resource response header. 3174 3175**Parameters** 3176 3177| Name | Type | Mandatory | Default Value | Description | 3178| ------ | -------------------------- | ---- | ---- | ---------- | 3179| header | Array\<[Header](#header)\> | Yes | - | Resource response header to set.| 3180 3181### setResponseCode<sup>9+</sup> 3182 3183setResponseCode(code: number) 3184 3185Sets the status code of the resource response. 3186 3187**Parameters** 3188 3189| Name | Type | Mandatory | Default Value | Description | 3190| ---- | ------ | ---- | ---- | ------------- | 3191| code | number | Yes | - | Status code to set.| 3192 3193### setResponseIsReady<sup>9+</sup> 3194 3195setResponseIsReady(IsReady: boolean) 3196 3197Sets whether the resource response data is ready. 3198 3199**Parameters** 3200 3201| Name | Type| Mandatory| Default Value| Description | 3202| ------- | -------- | ---- | ------ | -------------------------- | 3203| IsReady | boolean | Yes | true | Whether the resource response data is ready.| 3204 3205## FileSelectorResult<sup>9+</sup> 3206 3207Notifies the **\<Web>** component of the file selection result. For the sample code, see [onShowFileSelector](#onshowfileselector9). 3208 3209### handleFileList<sup>9+</sup> 3210 3211handleFileList(fileList: Array\<string\>): void 3212 3213Instructs the **\<Web>** component to select a file. 3214 3215**Parameters** 3216 3217| Name | Type | Mandatory | Default Value | Description | 3218| -------- | --------------- | ---- | ---- | ------------ | 3219| fileList | Array\<string\> | Yes | - | List of files to operate.| 3220 3221## FileSelectorParam<sup>9+</sup> 3222 3223Implements the **FileSelectorParam** object. For the sample code, see [onShowFileSelector](#onshowfileselector9). 3224 3225### getTitle<sup>9+</sup> 3226 3227getTitle(): string 3228 3229Obtains the title of this file selector. 3230 3231**Return value** 3232 3233| Type | Description | 3234| ------ | -------- | 3235| string | Title of the file selector.| 3236 3237### getMode<sup>9+</sup> 3238 3239getMode(): FileSelectorMode 3240 3241Obtains the mode of the file selector. 3242 3243**Return value** 3244 3245| Type | Description | 3246| ---------------------------------------- | ----------- | 3247| [FileSelectorMode](#fileselectormode)| Mode of the file selector.| 3248 3249### getAcceptType<sup>9+</sup> 3250 3251getAcceptType(): Array\<string\> 3252 3253Obtains the file filtering type. 3254 3255**Return value** 3256 3257| Type | Description | 3258| --------------- | --------- | 3259| Array\<string\> | File filtering type.| 3260 3261### isCapture<sup>9+</sup> 3262 3263isCapture(): boolean 3264 3265Checks whether multimedia capabilities are invoked. 3266 3267**Return value** 3268 3269| Type | Description | 3270| ------- | ------------ | 3271| boolean | Whether multimedia capabilities are invoked.| 3272 3273## HttpAuthHandler<sup>9+</sup> 3274 3275Implements the **HttpAuthHandler** object. For the sample code, see [onHttpAuthRequest](#onhttpauthrequest9). 3276 3277### cancel<sup>9+</sup> 3278 3279cancel(): void 3280 3281Cancels HTTP authentication as requested by the user. 3282 3283### confirm<sup>9+</sup> 3284 3285confirm(userName: string, pwd: string): boolean 3286 3287Performs HTTP authentication with the user name and password provided by the user. 3288 3289**Parameters** 3290 3291| Name | Type | Mandatory | Default Value | Description | 3292| -------- | ------ | ---- | ---- | ---------- | 3293| userName | string | Yes | - | HTTP authentication user name.| 3294| pwd | string | Yes | - | HTTP authentication password. | 3295 3296**Return value** 3297 3298| Type | Description | 3299| ------- | --------------------- | 3300| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.| 3301 3302### isHttpAuthInfoSaved<sup>9+</sup> 3303 3304isHttpAuthInfoSaved(): boolean 3305 3306Uses the account name and password cached on the server for authentication. 3307 3308**Return value** 3309 3310| Type | Description | 3311| ------- | ------------------------- | 3312| boolean | Returns **true** if the authentication is successful; returns **false** otherwise.| 3313 3314## SslErrorHandler<sup>9+</sup> 3315 3316Implements an **SslErrorHandler** object. For the sample code, see [onSslErrorEventReceive Event](#onsslerroreventreceive9). 3317 3318### handleCancel<sup>9+</sup> 3319 3320handleCancel(): void 3321 3322Cancels this request. 3323 3324### handleConfirm<sup>9+</sup> 3325 3326handleConfirm(): void 3327 3328Continues using the SSL certificate. 3329 3330## ClientAuthenticationHandler<sup>9+</sup> 3331 3332Implements a **ClientAuthenticationHandler** object returned by the **\<Web>** component. For the sample code, see [onClientAuthenticationRequest](#onclientauthenticationrequest9). 3333 3334### confirm<sup>9+</sup> 3335 3336confirm(priKeyFile : string, certChainFile : string): void 3337 3338Uses the specified private key and client certificate chain. 3339 3340**Parameters** 3341 3342| Name | Type | Mandatory | Description | 3343| ------------- | ------ | ---- | ------------------ | 3344| priKeyFile | string | Yes | File that stores the private key, which is a directory including the file name. | 3345| certChainFile | string | Yes | File that stores the certificate chain, which is a directory including the file name.| 3346 3347### cancel<sup>9+</sup> 3348 3349cancel(): void 3350 3351Cancels 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. 3352 3353### ignore<sup>9+</sup> 3354 3355ignore(): void 3356 3357Ignores this request. 3358 3359## PermissionRequest<sup>9+</sup> 3360 3361Implements the **PermissionRequest** object. For the sample code, see [onPermissionRequest](#onpermissionrequest9). 3362 3363### deny<sup>9+</sup> 3364 3365deny(): void 3366 3367Denies the permission requested by the web page. 3368 3369### getOrigin<sup>9+</sup> 3370 3371getOrigin(): string 3372 3373Obtains the origin of this web page. 3374 3375**Return value** 3376 3377| Type | Description | 3378| ------ | ------------ | 3379| string | Origin of the web page that requests the permission.| 3380 3381### getAccessibleResource<sup>9+</sup> 3382 3383getAccessibleResource(): Array\<string\> 3384 3385Obtains the list of accessible resources requested for the web page. For details about the resource types, see [ProtectedResourceType](#protectedresourcetype9). 3386 3387**Return value** 3388 3389| Type | Description | 3390| --------------- | ------------- | 3391| Array\<string\> | List of accessible resources requested by the web page.| 3392 3393### grant<sup>9+</sup> 3394 3395grant(resources: Array\<string\>): void 3396 3397Grants the permission for resources requested by the web page. 3398 3399**Parameters** 3400 3401| Name | Type | Mandatory | Default Value | Description | 3402| --------- | --------------- | ---- | ---- | ------------- | 3403| resources | Array\<string\> | Yes | - | List of resources that can be requested by the web page with the permission to grant.| 3404 3405## ContextMenuSourceType<sup>9+</sup> 3406| Name | Description | 3407| -------------------- | ---------- | 3408| None | Other event sources. | 3409| Mouse | Mouse event. | 3410| LongPress | Long press event. | 3411 3412## ContextMenuMediaType<sup>9+</sup> 3413 3414| Name | Description | 3415| ------------ | ----------- | 3416| None | Non-special media or other media types.| 3417| Image | Image. | 3418 3419## ContextMenuInputFieldType<sup>9+</sup> 3420 3421| Name | Description | 3422| ------------ | ----------- | 3423| None | Non-input field. | 3424| PlainText | Plain text field, such as the text, search, or email field. | 3425| Password | Password field. | 3426| Number | Numeric field. | 3427| Telephone | Phone number field.| 3428| Other | Field of any other type. | 3429 3430## ContextMenuEditStateFlags<sup>9+</sup> 3431 3432| Name | Description | 3433| ------------ | ----------- | 3434| NONE | Editing is not allowed. | 3435| CAN_CUT | The cut operation is allowed. | 3436| CAN_COPY | The copy operation is allowed. | 3437| CAN_PASTE | The paste operation is allowed. | 3438| CAN_SELECT_ALL | The select all operation is allowed.| 3439 3440## WebContextMenuParam<sup>9+</sup> 3441 3442Implements 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). 3443 3444### x<sup>9+</sup> 3445 3446x(): number 3447 3448Obtains the X coordinate of the context menu. 3449 3450**Return value** 3451 3452| Type | Description | 3453| ------ | ------------------ | 3454| number | If the display is normal, a non-negative integer is returned. Otherwise, **-1** is returned.| 3455 3456### y<sup>9+</sup> 3457 3458y(): number 3459 3460Obtains the Y coordinate of the context menu. 3461 3462**Return value** 3463 3464| Type | Description | 3465| ------ | ------------------ | 3466| number | If the display is normal, a non-negative integer is returned. Otherwise, **-1** is returned.| 3467 3468### getLinkUrl<sup>9+</sup> 3469 3470getLinkUrl(): string 3471 3472Obtains the URL of the destination link. 3473 3474**Return value** 3475 3476| Type | Description | 3477| ------ | ------------------------- | 3478| string | If it is a link that is being long pressed, the URL that has passed the security check is returned.| 3479 3480### getUnfilteredLinkUrl<sup>9+</sup> 3481 3482getUnfilteredLinkUrl(): string 3483 3484Obtains the URL of the destination link. 3485 3486**Return value** 3487 3488| Type | Description | 3489| ------ | --------------------- | 3490| string | If it is a link that is being long pressed, the original URL is returned.| 3491 3492### getSourceUrl<sup>9+</sup> 3493 3494getSourceUrl(): string 3495 3496Obtain the source URL. 3497 3498**Return value** 3499 3500| Type | Description | 3501| ------ | ------------------------ | 3502| string | If the selected element has the **src** attribute, the URL in the **src** is returned.| 3503 3504### existsImageContents<sup>9+</sup> 3505 3506existsImageContents(): boolean 3507 3508Checks whether image content exists. 3509 3510**Return value** 3511 3512| Type | Description | 3513| ------- | ------------------------- | 3514| boolean | The value **true** means that there is image content in the element being long pressed, and **false** means the opposite.| 3515 3516### getMediaType<sup>9+</sup> 3517 3518getMediaType(): ContextMenuMediaType 3519 3520Obtains the media type of this web page element. 3521 3522**Return value** 3523 3524| Type | Description | 3525| ---------------------------------------- | ----------- | 3526| [ContextMenuMediaType](#contextmenumediatype9) | Media type of the web page element.| 3527 3528### getSelectionText<sup>9+</sup> 3529 3530getSelectionText(): string 3531 3532Obtains the selected text. 3533 3534**Return value** 3535 3536| Type | Description | 3537| ------- | ------------------------- | 3538| string | Selected text for the context menu. If no text is selected, null is returned.| 3539 3540### getSourceType<sup>9+</sup> 3541 3542getSourceType(): ContextMenuSourceType 3543 3544Obtains the event source of the context menu. 3545 3546**Return value** 3547 3548| Type | Description | 3549| ---------------------------------------- | ----------- | 3550| [ContextMenuSourceType](#contextmenusourcetype9) | Event source of the context menu.| 3551 3552### getInputFieldType<sup>9+</sup> 3553 3554getInputFieldType(): ContextMenuInputFieldType 3555 3556Obtains the input field type of this web page element. 3557 3558**Return value** 3559 3560| Type | Description | 3561| ---------------------------------------- | ----------- | 3562| [ContextMenuInputFieldType](#contextmenuinputfieldtype9) | Input field type.| 3563 3564### isEditable<sup>9+</sup> 3565 3566isEditable(): boolean 3567 3568Checks whether this web page element is editable. 3569 3570**Return value** 3571 3572| Type | Description | 3573| ------- | ------------------------- | 3574| boolean | Returns **true** if the web page element is editable; returns **false** otherwise.| 3575 3576### getEditStateFlags<sup>9+</sup> 3577 3578getEditStateFlags(): number 3579 3580Obtains the edit state flag of this web page element. 3581 3582**Return value** 3583 3584| Type | Description | 3585| ------- | ------------------------- | 3586| number | Edit state flag of the web page element. For details, see [ContextMenuEditStateFlags](#contextmenueditstateflags9).| 3587 3588## WebContextMenuResult<sup>9+</sup> 3589 3590Implements a **WebContextMenuResult** object. For the sample code, see [onContextMenuShow](#oncontextmenushow9). 3591 3592### closeContextMenu<sup>9+</sup> 3593 3594closeContextMenu(): void 3595 3596Closes this context menu. This API must be called when no operations in **WebContextMenuResult** are performed. 3597 3598### copyImage<sup>9+</sup> 3599 3600copyImage(): void 3601 3602Copies the image specified in **WebContextMenuParam**. 3603 3604### copy<sup>9+</sup> 3605 3606copy(): void 3607 3608Performs the copy operation related to this context menu. 3609 3610### paste<sup>9+</sup> 3611 3612paste(): void 3613 3614Performs the paste operation related to this context menu. 3615 3616### cut<sup>9+</sup> 3617 3618cut(): void 3619 3620Performs the cut operation related to this context menu. 3621 3622### selectAll<sup>9+</sup> 3623 3624selectAll(): void 3625 3626Performs the select all operation related to this context menu. 3627 3628## JsGeolocation 3629 3630Implements the **PermissionRequest** object. For the sample code, see [onGeolocationShow Event](#ongeolocationshow). 3631 3632### invoke 3633 3634invoke(origin: string, allow: boolean, retain: boolean): void 3635 3636Sets the geolocation permission status of a web page. 3637 3638**Parameters** 3639 3640| Name | Type | Mandatory | Default Value | Description | 3641| ------ | ------- | ---- | ---- | ---------------------------------------- | 3642| origin | string | Yes | - | Index of the origin. | 3643| allow | boolean | Yes | - | Geolocation permission status. | 3644| 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).| 3645 3646## MessageLevel 3647 3648| Name | Description | 3649| ----- | :---- | 3650| Debug | Debug level.| 3651| Error | Error level.| 3652| Info | Information level.| 3653| Log | Log level.| 3654| Warn | Warning level. | 3655 3656## RenderExitReason 3657 3658Enumerates the reasons why the rendering process exits. 3659 3660| Name | Description | 3661| -------------------------- | ----------------- | 3662| ProcessAbnormalTermination | The rendering process exits abnormally. | 3663| ProcessWasKilled | The rendering process receives a SIGKILL message or is manually terminated.| 3664| ProcessCrashed | The rendering process crashes due to segmentation or other errors. | 3665| ProcessOom | The program memory is running low. | 3666| ProcessExitUnknown | Other reason. | 3667 3668## MixedMode 3669 3670| Name | Description | 3671| ---------- | ---------------------------------- | 3672| All | HTTP and HTTPS hybrid content can be loaded. This means that all insecure content can be loaded.| 3673| Compatible | HTTP and HTTPS hybrid content can be loaded in compatibility mode. This means that some insecure content may be loaded. | 3674| None | HTTP and HTTPS hybrid content cannot be loaded. | 3675 3676## CacheMode 3677| Name | Description | 3678| ------- | ------------------------------------ | 3679| 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.| 3680| 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. | 3681| Online | The cache is not used to load the resources. All resources are obtained from the Internet. | 3682| Only | The cache alone is used to load the resources. | 3683 3684## FileSelectorMode 3685| Name | Description | 3686| -------------------- | ---------- | 3687| FileOpenMode | Open and upload a file. | 3688| FileOpenMultipleMode | Open and upload multiple files. | 3689| FileOpenFolderMode | Open and upload a folder.| 3690| FileSaveMode | Save a file. | 3691 3692 ## HitTestType 3693 3694| Name | Description | 3695| ------------- | ------------------------ | 3696| EditText | Editable area. | 3697| Email | Email address. | 3698| HttpAnchor | Hyperlink whose **src** is **http**. | 3699| HttpAnchorImg | Image with a hyperlink, where **src** is **http**.| 3700| Img | HTML::img tag. | 3701| Map | Geographical address. | 3702| Phone | Phone number. | 3703| Unknown | Unknown content. | 3704 3705## SslError<sup>9+</sup> 3706 3707Enumerates the error codes returned by **onSslErrorEventReceive** API. 3708 3709| Name | Description | 3710| ------------ | ----------- | 3711| Invalid | Minor error. | 3712| HostMismatch | The host name does not match. | 3713| DateInvalid | The certificate has an invalid date. | 3714| Untrusted | The certificate issuer is not trusted.| 3715 3716## ProtectedResourceType<sup>9+</sup> 3717 3718| Name | Description | Remarks | 3719| --------- | ------------- | -------------------------- | 3720| MidiSysex | MIDI SYSEX resource.| Currently, only permission events can be reported. MIDI devices are not yet supported.| 3721 3722## WebDarkMode<sup>9+</sup> 3723| Name | Description | 3724| ------- | ------------------------------------ | 3725| Off | The web dark mode is disabled. | 3726| On | The web dark mode is enabled. | 3727| Auto | The web dark mode setting follows the system settings. | 3728 3729## DataResubmissionHandler<sup>9+</sup> 3730 3731Implements the **DataResubmissionHandler** object for resubmitting or canceling the web form data. 3732 3733### resend<sup>9+</sup> 3734 3735resend(): void 3736 3737Resends the web form data. 3738 3739**Example** 3740 3741 ```ts 3742 // xxx.ets 3743 import web_webview from '@ohos.web.webview' 3744 @Entry 3745 @Component 3746 struct WebComponent { 3747 controller: web_webview.WebviewController = new web_webview.WebviewController() 3748 build() { 3749 Column() { 3750 Web({ src:'www.example.com', controller: this.controller }) 3751 .onDataResubmitted((event) => { 3752 console.log('onDataResubmitted') 3753 event.handler.resend(); 3754 }) 3755 } 3756 } 3757 } 3758 ``` 3759 3760### cancel<sup>9+</sup> 3761 3762cancel(): void 3763 3764Cancels the resending of web form data. 3765 3766**Example** 3767 3768 ```ts 3769 // xxx.ets 3770 import web_webview from '@ohos.web.webview' 3771 @Entry 3772 @Component 3773 struct WebComponent { 3774 controller: web_webview.WebviewController = new web_webview.WebviewController() 3775 build() { 3776 Column() { 3777 Web({ src:'www.example.com', controller: this.controller }) 3778 .onDataResubmitted((event) => { 3779 console.log('onDataResubmitted') 3780 event.handler.cancel(); 3781 }) 3782 } 3783 } 3784 } 3785 ``` 3786 3787 ## WebController 3788 3789Implements 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. 3790 3791This API is deprecated since API version 9. You are advised to use [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller). 3792 3793### Creating an Object 3794 3795``` 3796webController: WebController = new WebController() 3797``` 3798 3799### getCookieManager<sup>9+</sup> 3800 3801getCookieManager(): WebCookie 3802 3803Obtains the cookie management object of the **\<Web>** component. 3804 3805**Return value** 3806 3807| Type | Description | 3808| --------- | ---------------------------------------- | 3809| WebCookie | Cookie management object. For details, see [WebCookie](#webcookie).| 3810 3811**Example** 3812 3813 ```ts 3814 // xxx.ets 3815 @Entry 3816 @Component 3817 struct WebComponent { 3818 controller: WebController = new WebController() 3819 3820 build() { 3821 Column() { 3822 Button('getCookieManager') 3823 .onClick(() => { 3824 let cookieManager = this.controller.getCookieManager() 3825 }) 3826 Web({ src: 'www.example.com', controller: this.controller }) 3827 } 3828 } 3829 } 3830 ``` 3831 3832### requestFocus<sup>(deprecated)</sup> 3833 3834requestFocus() 3835 3836Requests focus for this web page. 3837 3838This API is deprecated since API version 9. You are advised to use [requestFocus<sup>9+</sup>](../apis/js-apis-webview.md#requestfocus). 3839 3840**Example** 3841 3842 ```ts 3843 // xxx.ets 3844 @Entry 3845 @Component 3846 struct WebComponent { 3847 controller: WebController = new WebController() 3848 3849 build() { 3850 Column() { 3851 Button('requestFocus') 3852 .onClick(() => { 3853 this.controller.requestFocus() 3854 }) 3855 Web({ src: 'www.example.com', controller: this.controller }) 3856 } 3857 } 3858 } 3859 ``` 3860 3861### accessBackward<sup>(deprecated)</sup> 3862 3863accessBackward(): boolean 3864 3865Checks whether going to the previous page can be performed on the current page. 3866 3867This API is deprecated since API version 9. You are advised to use [accessBackward<sup>9+</sup>](../apis/js-apis-webview.md#accessbackward). 3868 3869**Return value** 3870 3871| Type | Description | 3872| ------- | --------------------- | 3873| boolean | Returns **true** if going to the previous page can be performed on the current page; returns **false** otherwise.| 3874 3875**Example** 3876 3877 ```ts 3878 // xxx.ets 3879 @Entry 3880 @Component 3881 struct WebComponent { 3882 controller: WebController = new WebController() 3883 3884 build() { 3885 Column() { 3886 Button('accessBackward') 3887 .onClick(() => { 3888 let result = this.controller.accessBackward() 3889 console.log('result:' + result) 3890 }) 3891 Web({ src: 'www.example.com', controller: this.controller }) 3892 } 3893 } 3894 } 3895 ``` 3896 3897### accessForward<sup>(deprecated)</sup> 3898 3899accessForward(): boolean 3900 3901Checks whether going to the next page can be performed on the current page. 3902 3903This API is deprecated since API version 9. You are advised to use [accessForward<sup>9+</sup>](../apis/js-apis-webview.md#accessforward). 3904 3905**Return value** 3906 3907| Type | Description | 3908| ------- | --------------------- | 3909| boolean | Returns **true** if going to the next page can be performed on the current page; returns **false** otherwise.| 3910 3911**Example** 3912 3913 ```ts 3914 // xxx.ets 3915 @Entry 3916 @Component 3917 struct WebComponent { 3918 controller: WebController = new WebController() 3919 3920 build() { 3921 Column() { 3922 Button('accessForward') 3923 .onClick(() => { 3924 let result = this.controller.accessForward() 3925 console.log('result:' + result) 3926 }) 3927 Web({ src: 'www.example.com', controller: this.controller }) 3928 } 3929 } 3930 } 3931 ``` 3932 3933### accessStep<sup>(deprecated)</sup> 3934 3935accessStep(step: number): boolean 3936 3937Performs a specific number of steps forward or backward from the current page. 3938 3939This API is deprecated since API version 9. You are advised to use [accessStep<sup>9+</sup>](../apis/js-apis-webview.md#accessstep). 3940 3941**Parameters** 3942 3943| Name | Type | Mandatory | Default Value | Description | 3944| ---- | ------ | ---- | ---- | --------------------- | 3945| step | number | Yes | - | Number of the steps to take. A positive number means to go forward, and a negative number means to go backward.| 3946 3947**Return value** 3948 3949| Type | Description | 3950| ------- | --------- | 3951| boolean | Whether going forward or backward from the current page is successful.| 3952 3953**Example** 3954 3955 ```ts 3956 // xxx.ets 3957 @Entry 3958 @Component 3959 struct WebComponent { 3960 controller: WebController = new WebController() 3961 @State steps: number = 2 3962 3963 build() { 3964 Column() { 3965 Button('accessStep') 3966 .onClick(() => { 3967 let result = this.controller.accessStep(this.steps) 3968 console.log('result:' + result) 3969 }) 3970 Web({ src: 'www.example.com', controller: this.controller }) 3971 } 3972 } 3973 } 3974 ``` 3975 3976### backward<sup>(deprecated)</sup> 3977 3978backward(): void 3979 3980Goes to the previous page based on the history stack. This API is generally used together with **accessBackward**. 3981 3982This API is deprecated since API version 9. You are advised to use [backward<sup>9+</sup>](../apis/js-apis-webview.md#backward). 3983 3984**Example** 3985 3986 ```ts 3987 // xxx.ets 3988 @Entry 3989 @Component 3990 struct WebComponent { 3991 controller: WebController = new WebController() 3992 3993 build() { 3994 Column() { 3995 Button('backward') 3996 .onClick(() => { 3997 this.controller.backward() 3998 }) 3999 Web({ src: 'www.example.com', controller: this.controller }) 4000 } 4001 } 4002 } 4003 ``` 4004 4005### forward<sup>(deprecated)</sup> 4006 4007forward(): void 4008 4009Goes to the next page based on the history stack. This API is generally used together with **accessForward**. 4010 4011This API is deprecated since API version 9. You are advised to use [forward<sup>9+</sup>](../apis/js-apis-webview.md#forward). 4012 4013**Example** 4014 4015 ```ts 4016 // xxx.ets 4017 @Entry 4018 @Component 4019 struct WebComponent { 4020 controller: WebController = new WebController() 4021 4022 build() { 4023 Column() { 4024 Button('forward') 4025 .onClick(() => { 4026 this.controller.forward() 4027 }) 4028 Web({ src: 'www.example.com', controller: this.controller }) 4029 } 4030 } 4031 } 4032 ``` 4033 4034### deleteJavaScriptRegister<sup>(deprecated)</sup> 4035 4036deleteJavaScriptRegister(name: string) 4037 4038Deletes a specific application JavaScript object that is registered with the window through **registerJavaScriptProxy**. The deletion takes effect immediately, with no need for invoking the [refresh](#refresh) API. 4039 4040This API is deprecated since API version 9. You are advised to use [deleteJavaScriptRegister<sup>9+</sup>](../apis/js-apis-webview.md#deletejavascriptregister). 4041 4042**Parameters** 4043 4044| Name | Type | Mandatory | Default Value | Description | 4045| ---- | ------ | ---- | ---- | ---------------------------------------- | 4046| 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.| 4047 4048**Example** 4049 4050 ```ts 4051 // xxx.ets 4052 @Entry 4053 @Component 4054 struct WebComponent { 4055 controller: WebController = new WebController() 4056 @State name: string = 'Object' 4057 4058 build() { 4059 Column() { 4060 Button('deleteJavaScriptRegister') 4061 .onClick(() => { 4062 this.controller.deleteJavaScriptRegister(this.name) 4063 }) 4064 Web({ src: 'www.example.com', controller: this.controller }) 4065 } 4066 } 4067 } 4068 ``` 4069 4070### getHitTest<sup>(deprecated)</sup> 4071 4072getHitTest(): HitTestType 4073 4074Obtains the element type of the area being clicked. 4075 4076This API is deprecated since API version 9. You are advised to use [getHitTest<sup>9+</sup>](../apis/js-apis-webview.md#gethittest). 4077 4078**Return value** 4079 4080| Type | Description | 4081| ------------------------------- | ----------- | 4082| [HitTestType](#hittesttype)| Element type of the area being clicked.| 4083 4084**Example** 4085 4086 ```ts 4087 // xxx.ets 4088 @Entry 4089 @Component 4090 struct WebComponent { 4091 controller: WebController = new WebController() 4092 4093 build() { 4094 Column() { 4095 Button('getHitTest') 4096 .onClick(() => { 4097 let hitType = this.controller.getHitTest() 4098 console.log("hitType: " + hitType) 4099 }) 4100 Web({ src: 'www.example.com', controller: this.controller }) 4101 } 4102 } 4103 } 4104 ``` 4105 4106### loadData<sup>(deprecated)</sup> 4107 4108loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string }) 4109 4110Loads data. If **baseUrl** is empty, the specified character string will be loaded using the data protocol. 4111 4112If **baseUrl** is set to a data URL, the encoded string will be loaded by the **\<Web>** component using the data protocol. 4113 4114If **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**. 4115 4116This API is deprecated since API version 9. You are advised to use [loadData<sup>9+</sup>](../apis/js-apis-webview.md#loaddata). 4117 4118**Parameters** 4119 4120| Name | Type | Mandatory | Default Value | Description | 4121| ---------- | ------ | ---- | ---- | ---------------------------------------- | 4122| data | string | Yes | - | Character string obtained after being Base64 or URL encoded. | 4123| mimeType | string | Yes | - | Media type (MIME). | 4124| encoding | string | Yes | - | Encoding type, which can be Base64 or URL. | 4125| baseUrl | string | No | - | URL (HTTP/HTTPS/data compliant), which is assigned by the **\<Web>** component to **window.origin**.| 4126| 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.| 4127 4128**Example** 4129 4130 ```ts 4131 // xxx.ets 4132 @Entry 4133 @Component 4134 struct WebComponent { 4135 controller: WebController = new WebController() 4136 4137 build() { 4138 Column() { 4139 Button('loadData') 4140 .onClick(() => { 4141 this.controller.loadData({ 4142 data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>", 4143 mimeType: "text/html", 4144 encoding: "UTF-8" 4145 }) 4146 }) 4147 Web({ src: 'www.example.com', controller: this.controller }) 4148 } 4149 } 4150 } 4151 ``` 4152 4153### loadUrl<sup>(deprecated)</sup> 4154 4155loadUrl(options: { url: string | Resource, headers?: Array\<Header\> }) 4156 4157Loads a URL using the specified HTTP header. 4158 4159The object injected through **loadUrl** is valid only in the current document. It will be invalid on a new page navigated to through **loadUrl**. 4160 4161The object injected through **registerJavaScriptProxy** is still valid on a new page redirected through **loadUrl**. 4162 4163This API is deprecated since API version 9. You are advised to use [loadUrl<sup>9+</sup>](../apis/js-apis-webview.md#loadurl). 4164 4165**Parameters** 4166 4167| Name | Type | Mandatory | Default Value | Description | 4168| ------- | -------------------------- | ---- | ---- | -------------- | 4169| url | string | Yes | - | URL to load. | 4170| headers | Array\<[Header](#header)\> | No | [] | Additional HTTP request header of the URL.| 4171 4172**Example** 4173 4174 ```ts 4175 // xxx.ets 4176 @Entry 4177 @Component 4178 struct WebComponent { 4179 controller: WebController = new WebController() 4180 4181 build() { 4182 Column() { 4183 Button('loadUrl') 4184 .onClick(() => { 4185 this.controller.loadUrl({ url: 'www.example.com' }) 4186 }) 4187 Web({ src: 'www.example.com', controller: this.controller }) 4188 } 4189 } 4190 } 4191 ``` 4192 4193### onActive<sup>(deprecated)</sup> 4194 4195onActive(): void 4196 4197Called when the **\<Web>** component enters the active state. 4198 4199This API is deprecated since API version 9. You are advised to use [onActive<sup>9+</sup>](../apis/js-apis-webview.md#onactive). 4200 4201**Example** 4202 4203 ```ts 4204 // xxx.ets 4205 @Entry 4206 @Component 4207 struct WebComponent { 4208 controller: WebController = new WebController() 4209 4210 build() { 4211 Column() { 4212 Button('onActive') 4213 .onClick(() => { 4214 this.controller.onActive() 4215 }) 4216 Web({ src: 'www.example.com', controller: this.controller }) 4217 } 4218 } 4219 } 4220 ``` 4221 4222### onInactive<sup>(deprecated)</sup> 4223 4224onInactive(): void 4225 4226Called when the **\<Web>** component enters the inactive state. 4227 4228This API is deprecated since API version 9. You are advised to use [onInactive<sup>9+</sup>](../apis/js-apis-webview.md#oninactive). 4229 4230**Example** 4231 4232 ```ts 4233 // xxx.ets 4234 @Entry 4235 @Component 4236 struct WebComponent { 4237 controller: WebController = new WebController() 4238 4239 build() { 4240 Column() { 4241 Button('onInactive') 4242 .onClick(() => { 4243 this.controller.onInactive() 4244 }) 4245 Web({ src: 'www.example.com', controller: this.controller }) 4246 } 4247 } 4248 } 4249 ``` 4250 4251### zoom<sup>(deprecated)</sup> 4252zoom(factor: number): void 4253 4254Sets a zoom factor for the current web page. 4255 4256This API is deprecated since API version 9. You are advised to use [zoom<sup>9+</sup>](../apis/js-apis-webview.md#zoom). 4257 4258**Parameters** 4259 4260| Name | Type | Mandatory | Description | 4261| ------ | ------ | ---- | ------------------------------ | 4262| factor | number | Yes | Zoom factor to set. A positive value indicates zoom-in, and a negative value indicates zoom-out.| 4263 4264**Example** 4265 4266 ```ts 4267 // xxx.ets 4268 @Entry 4269 @Component 4270 struct WebComponent { 4271 controller: WebController = new WebController() 4272 @State factor: number = 1 4273 4274 build() { 4275 Column() { 4276 Button('zoom') 4277 .onClick(() => { 4278 this.controller.zoom(this.factor) 4279 }) 4280 Web({ src: 'www.example.com', controller: this.controller }) 4281 } 4282 } 4283 } 4284 ``` 4285 4286### refresh<sup>(deprecated)</sup> 4287 4288refresh() 4289 4290Called when the **\<Web>** component refreshes the web page. 4291 4292This API is deprecated since API version 9. You are advised to use [refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh). 4293 4294**Example** 4295 4296 ```ts 4297 // xxx.ets 4298 @Entry 4299 @Component 4300 struct WebComponent { 4301 controller: WebController = new WebController() 4302 4303 build() { 4304 Column() { 4305 Button('refresh') 4306 .onClick(() => { 4307 this.controller.refresh() 4308 }) 4309 Web({ src: 'www.example.com', controller: this.controller }) 4310 } 4311 } 4312 } 4313 ``` 4314 4315### registerJavaScriptProxy<sup>(deprecated)</sup> 4316 4317registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> }) 4318 4319Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. You must invoke the [refresh](#refresh) API for the registration to take effect. 4320 4321This API is deprecated since API version 9. You are advised to use [registerJavaScriptProxy<sup>9+</sup>](../apis/js-apis-webview.md#registerjavascriptproxy). 4322 4323**Parameters** 4324 4325| Name | Type | Mandatory | Default Value | Description | 4326| ---------- | --------------- | ---- | ---- | ---------------------------------------- | 4327| 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.| 4328| 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.| 4329| methodList | Array\<string\> | Yes | - | Methods of the JavaScript object to be registered at the application side. | 4330 4331**Example** 4332 4333 ```ts 4334 // xxx.ets 4335 @Entry 4336 @Component 4337 struct Index { 4338 controller: WebController = new WebController() 4339 testObj = { 4340 test: (data) => { 4341 return "ArkUI Web Component" 4342 }, 4343 toString: () => { 4344 console.log('Web Component toString') 4345 } 4346 } 4347 build() { 4348 Column() { 4349 Row() { 4350 Button('Register JavaScript To Window').onClick(() => { 4351 this.controller.registerJavaScriptProxy({ 4352 object: this.testObj, 4353 name: "objName", 4354 methodList: ["test", "toString"], 4355 }) 4356 }) 4357 } 4358 Web({ src: $rawfile('index.html'), controller: this.controller }) 4359 .javaScriptAccess(true) 4360 } 4361 } 4362 } 4363 ``` 4364 4365 ```html 4366 <!-- index.html --> 4367 <!DOCTYPE html> 4368 <html> 4369 <meta charset="utf-8"> 4370 <body> 4371 Hello world! 4372 </body> 4373 <script type="text/javascript"> 4374 function htmlTest() { 4375 str = objName.test("test function") 4376 console.log('objName.test result:'+ str) 4377 } 4378 </script> 4379 </html> 4380 4381 ``` 4382 4383### runJavaScript<sup>(deprecated)</sup> 4384 4385runJavaScript(options: { script: string, callback?: (result: string) => void }) 4386 4387Executes 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**. 4388 4389This API is deprecated since API version 9. You are advised to use [runJavaScript<sup>9+</sup>](../apis/js-apis-webview.md#runjavascript). 4390 4391**Parameters** 4392 4393| Name | Type | Mandatory | Default Value | Description | 4394| -------- | ------------------------ | ---- | ---- | ---------------------------------------- | 4395| script | string | Yes | - | JavaScript script. | 4396| 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.| 4397 4398**Example** 4399 4400 ```ts 4401 // xxx.ets 4402 @Entry 4403 @Component 4404 struct WebComponent { 4405 controller: WebController = new WebController() 4406 @State webResult: string = '' 4407 build() { 4408 Column() { 4409 Text(this.webResult).fontSize(20) 4410 Web({ src: $rawfile('index.html'), controller: this.controller }) 4411 .javaScriptAccess(true) 4412 .onPageEnd(e => { 4413 this.controller.runJavaScript({ 4414 script: 'test()', 4415 callback: (result: string)=> { 4416 this.webResult = result 4417 console.info(`The test() return value is: ${result}`) 4418 }}) 4419 console.info('url: ', e.url) 4420 }) 4421 } 4422 } 4423 } 4424 ``` 4425 4426 ```html 4427 <!-- index.html --> 4428 <!DOCTYPE html> 4429 <html> 4430 <meta charset="utf-8"> 4431 <body> 4432 Hello world! 4433 </body> 4434 <script type="text/javascript"> 4435 function test() { 4436 console.log('Ark WebComponent') 4437 return "This value is from index.html" 4438 } 4439 </script> 4440 </html> 4441 4442 ``` 4443 4444### stop<sup>(deprecated)</sup> 4445 4446stop() 4447 4448Stops page loading. 4449 4450This API is deprecated since API version 9. You are advised to use [stop<sup>9+</sup>](../apis/js-apis-webview.md#stop). 4451 4452**Example** 4453 4454 ```ts 4455 // xxx.ets 4456 @Entry 4457 @Component 4458 struct WebComponent { 4459 controller: WebController = new WebController() 4460 4461 build() { 4462 Column() { 4463 Button('stop') 4464 .onClick(() => { 4465 this.controller.stop() 4466 }) 4467 Web({ src: 'www.example.com', controller: this.controller }) 4468 } 4469 } 4470 } 4471 ``` 4472 4473### clearHistory<sup>(deprecated)</sup> 4474 4475clearHistory(): void 4476 4477Clears the browsing history. 4478 4479This API is deprecated since API version 9. You are advised to use [clearHistory<sup>9+</sup>](../apis/js-apis-webview.md#clearhistory). 4480 4481**Example** 4482 4483 ```ts 4484 // xxx.ets 4485 @Entry 4486 @Component 4487 struct WebComponent { 4488 controller: WebController = new WebController() 4489 4490 build() { 4491 Column() { 4492 Button('clearHistory') 4493 .onClick(() => { 4494 this.controller.clearHistory() 4495 }) 4496 Web({ src: 'www.example.com', controller: this.controller }) 4497 } 4498 } 4499 } 4500 ``` 4501 4502## WebCookie<sup>(deprecated)</sup> 4503 4504Manages 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. 4505 4506### setCookie<sup>(deprecated)</sup> 4507setCookie(url: string, value: string): boolean 4508 4509Sets the cookie. This API returns the result synchronously. Returns **true** if the operation is successful; returns **false** otherwise. 4510This API is deprecated since API version 9. You are advised to use [setCookie<sup>9+</sup>](../apis/js-apis-webview.md#setcookie) instead. 4511 4512**Parameters** 4513 4514| Name | Type | Mandatory | Default Value | Description | 4515| ----- | ------ | ---- | ---- | ----------------- | 4516| url | string | Yes | - | URL of the cookie to set. A complete URL is recommended.| 4517| value | string | Yes | - | Value of the cookie to set. | 4518 4519**Return value** 4520 4521| Type | Description | 4522| ------- | ------------- | 4523| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 4524 4525**Example** 4526 4527 ```ts 4528 // xxx.ets 4529 @Entry 4530 @Component 4531 struct WebComponent { 4532 controller: WebController = new WebController() 4533 4534 build() { 4535 Column() { 4536 Button('setCookie') 4537 .onClick(() => { 4538 let result = this.controller.getCookieManager().setCookie("https://www.example.com", "a=b") 4539 console.log("result: " + result) 4540 }) 4541 Web({ src: 'www.example.com', controller: this.controller }) 4542 } 4543 } 4544 } 4545 ``` 4546### saveCookie<sup>(deprecated)</sup> 4547saveCookie(): boolean 4548 4549Saves the cookies in the memory to the drive. This API returns the result synchronously. 4550This API is deprecated since API version 9. You are advised to use [saveCookieAsync<sup>9+</sup>](../apis/js-apis-webview.md#savecookieasync) instead. 4551 4552**Return value** 4553 4554| Type | Description | 4555| ------- | -------------------- | 4556| boolean | Operation result.| 4557 4558**Example** 4559 4560 ```ts 4561 // xxx.ets 4562 @Entry 4563 @Component 4564 struct WebComponent { 4565 controller: WebController = new WebController() 4566 4567 build() { 4568 Column() { 4569 Button('saveCookie') 4570 .onClick(() => { 4571 let result = this.controller.getCookieManager().saveCookie() 4572 console.log("result: " + result) 4573 }) 4574 Web({ src: 'www.example.com', controller: this.controller }) 4575 } 4576 } 4577 } 4578 ``` 4579