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