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