ts-basic-components-web.md 151.7 KB
Newer Older
Z
zengyawen 已提交
1 2
# Web

H
HelloCrease 已提交
3 4
提供具有网页显示能力的Web组件。

H
geshi  
HelloCrease 已提交
5
> **说明:** 
6 7 8
>
> - 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
> - 示例效果请以真机运行为准,当前IDE预览器不支持。
Z
zengyawen 已提交
9

L
update  
laosan_ted 已提交
10 11
## 需要权限
访问在线网页时需添加网络权限:ohos.permission.INTERNET,具体申请方式请参考[权限申请声明](../../security/accesstoken-guidelines.md)
L
liwenzhen 已提交
12

Z
zengyawen 已提交
13 14 15 16 17 18
## 子组件



## 接口

Y
yuhaoge 已提交
19
Web(options: { src: ResourceStr, controller: WebController | WebviewController})
Z
zhou-liting125 已提交
20

L
laosan_ted 已提交
21 22 23 24
> **说明:**
>
> 不支持转场动画。

Z
zhou-liting125 已提交
25
**参数:**
L
laosan_ted 已提交
26

H
HelloCrease 已提交
27 28 29
| 参数名        | 参数类型                                     | 必填   | 参数描述    |
| ---------- | ---------------------------------------- | ---- | ------- |
| src        | [ResourceStr](ts-types.md)               | 是    | 网页资源地址。 |
H
HelloCrease 已提交
30
| controller | [WebController](#webcontroller) \| [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) | 是    | 控制器。    |
Z
zengyawen 已提交
31

Z
zhou-liting125 已提交
32
**示例:**
L
update  
laosan_ted 已提交
33

L
laosan_ted 已提交
34
  加载在线网页
Z
zhou-liting125 已提交
35 36
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
37
  @Entry
L
update  
laosan_ted 已提交
38 39
  @Component
  struct WebComponent {
Y
yamila 已提交
40
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
41 42
    build() {
      Column() {
43
        Web({ src: 'www.example.com', controller: this.controller })
L
update  
laosan_ted 已提交
44 45 46 47
      }
    }
  }
  ```
Y
yuhaoge 已提交
48 49 50 51 52 53 54
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
55
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yuhaoge 已提交
56 57 58 59 60 61 62
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
Z
zengyawen 已提交
63

L
laosan_ted 已提交
64 65 66 67 68 69
  加载本地网页
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
70
    controller: WebController = new WebController()
L
laosan_ted 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
      }
    }
  }
  ```

  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
      <body>
          <p>Hello World</p>
      </body>
  </html>
  ```
Z
zengyawen 已提交
88

L
update  
laosan_ted 已提交
89
## 属性
Z
zhou-liting125 已提交
90

H
HelloCrease 已提交
91
通用属性仅支持[width](ts-universal-attributes-size.md#属性)[height](ts-universal-attributes-size.md#属性)[padding](ts-universal-attributes-size.md#属性)[margin](ts-universal-attributes-size.md#属性)[border](ts-universal-attributes-border.md#属性)
Z
zhou-liting125 已提交
92 93 94 95 96 97 98

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

设置是否开启文档对象模型存储接口(DOM Storage API)权限,默认未开启。

Z
zhou-liting125 已提交
99
**参数:**
L
laosan_ted 已提交
100

H
HelloCrease 已提交
101 102
| 参数名              | 参数类型    | 必填   | 默认值   | 参数描述                                 |
| ---------------- | ------- | ---- | ----- | ------------------------------------ |
103
| domStorageAccess | boolean | 是    | false | 设置是否开启文档对象模型存储接口(DOM Storage API)权限。 |
Z
zhou-liting125 已提交
104

Z
zhou-liting125 已提交
105
**示例:**
L
laosan_ted 已提交
106

Z
zhou-liting125 已提交
107 108
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
109
  @Entry
L
update  
laosan_ted 已提交
110 111
  @Component
  struct WebComponent {
Y
yamila 已提交
112
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
113 114
    build() {
      Column() {
115 116
        Web({ src: 'www.example.com', controller: this.controller })
          .domStorageAccess(true)
L
update  
laosan_ted 已提交
117 118 119 120 121
      }
    }
  }
  ```

Z
zhou-liting125 已提交
122 123 124 125
### fileAccess

fileAccess(fileAccess: boolean)

E
echoorchid 已提交
126
设置是否开启应用中文件系统的访问,默认启用。[$rawfile(filepath/filename)](../../quick-start/resource-categories-and-access.md)中rawfile路径的文件不受该属性影响而限制访问。
Z
zhou-liting125 已提交
127

Z
zhou-liting125 已提交
128
**参数:**
L
laosan_ted 已提交
129

H
HelloCrease 已提交
130 131
| 参数名        | 参数类型    | 必填   | 默认值  | 参数描述                   |
| ---------- | ------- | ---- | ---- | ---------------------- |
L
laosan_ted 已提交
132
| fileAccess | boolean | 是    | true | 设置是否开启应用中文件系统的访问,默认启用。 |
Z
zhou-liting125 已提交
133

Z
zhou-liting125 已提交
134
**示例:**
L
laosan_ted 已提交
135

Z
zhou-liting125 已提交
136 137
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
138
  @Entry
L
update  
laosan_ted 已提交
139 140
  @Component
  struct WebComponent {
Y
yamila 已提交
141
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
142 143
    build() {
      Column() {
144
        Web({ src: 'www.example.com', controller: this.controller })
145
          .fileAccess(true)
L
update  
laosan_ted 已提交
146 147 148 149 150
      }
    }
  }
  ```

Z
zhou-liting125 已提交
151 152 153 154 155 156
### imageAccess

imageAccess(imageAccess: boolean)

设置是否允许自动加载图片资源,默认允许。

Z
zhou-liting125 已提交
157
**参数:**
L
laosan_ted 已提交
158

159 160
| 参数名         | 参数类型    | 必填   | 默认值  | 参数描述            |
| ----------- | ------- | ---- | ---- | --------------- |
161
| imageAccess | boolean | 是    | true | 设置是否允许自动加载图片资源。 |
Z
zhou-liting125 已提交
162

Z
zhou-liting125 已提交
163
**示例:**
Z
zhou-liting125 已提交
164 165
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
166
  @Entry
L
update  
laosan_ted 已提交
167 168
  @Component
  struct WebComponent {
Y
yamila 已提交
169
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
170 171
    build() {
      Column() {
172 173
        Web({ src: 'www.example.com', controller: this.controller })
          .imageAccess(true)
L
update  
laosan_ted 已提交
174 175 176 177 178
      }
    }
  }
  ```

Z
zhou-liting125 已提交
179 180 181
### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
Y
yuhaoge 已提交
182
    controller: WebController | WebviewController})
Z
zhou-liting125 已提交
183 184 185

注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。所有参数不支持更新。

Z
zhou-liting125 已提交
186
**参数:**
L
laosan_ted 已提交
187

H
HelloCrease 已提交
188 189 190 191 192 193
| 参数名        | 参数类型                                     | 必填   | 默认值  | 参数描述                      |
| ---------- | ---------------------------------------- | ---- | ---- | ------------------------- |
| object     | object                                   | 是    | -    | 参与注册的对象。只能声明方法,不能声明属性。    |
| name       | string                                   | 是    | -    | 注册对象的名称,与window中调用的对象名一致。 |
| methodList | Array\<string\>                          | 是    | -    | 参与注册的应用侧JavaScript对象的方法。  |
| controller | [WebController](#webcontroller)[WebviewController](../apis/js-apis-webview.md#webviewcontroller) | 是    | -    | 控制器。                      |
194

Z
zhou-liting125 已提交
195
**示例:**
L
laosan_ted 已提交
196

Z
zhou-liting125 已提交
197 198
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
199
  @Entry
L
update  
laosan_ted 已提交
200 201
  @Component
  struct WebComponent {
Y
yamila 已提交
202
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
203 204
    testObj = {
      test: (data1, data2, data3) => {
Y
yamila 已提交
205 206 207 208
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
L
update  
laosan_ted 已提交
209 210
      },
      toString: () => {
Y
yamila 已提交
211
        console.log('toString' + "interface instead.")
L
update  
laosan_ted 已提交
212 213 214 215
      }
    }
    build() {
      Column() {
216 217 218 219 220 221 222
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
L
update  
laosan_ted 已提交
223 224 225 226 227
        })
      }
    }
  }
  ```
Y
yuhaoge 已提交
228 229 230 231 232 233 234
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
235
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yuhaoge 已提交
236 237
    testObj = {
      test: (data1, data2, data3) => {
Y
yamila 已提交
238 239 240 241
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
Y
yuhaoge 已提交
242 243
      },
      toString: () => {
Y
yamila 已提交
244
        console.log('toString' + "interface instead.")
Y
yuhaoge 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
      }
    }
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
        })
      }
    }
  }
  ```
L
update  
laosan_ted 已提交
261

Z
zhou-liting125 已提交
262 263 264 265 266 267
### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

设置是否允许执行JavaScript脚本,默认允许执行。

Z
zhou-liting125 已提交
268
**参数:**
L
laosan_ted 已提交
269

270 271
| 参数名              | 参数类型    | 必填   | 默认值  | 参数描述                |
| ---------------- | ------- | ---- | ---- | ------------------- |
272
| javaScriptAccess | boolean | 是    | true | 是否允许执行JavaScript脚本。 |
Z
zhou-liting125 已提交
273

Z
zhou-liting125 已提交
274
**示例:**
L
laosan_ted 已提交
275

Z
zhou-liting125 已提交
276 277
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
278
  @Entry
L
update  
laosan_ted 已提交
279 280
  @Component
  struct WebComponent {
Y
yamila 已提交
281
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
282 283
    build() {
      Column() {
284 285
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
L
update  
laosan_ted 已提交
286 287 288 289 290
      }
    }
  }
  ```

Z
zhou-liting125 已提交
291 292 293 294 295 296
### mixedMode

mixedMode(mixedMode: MixedMode)

设置是否允许加载超文本传输协议(HTTP)和超文本传输安全协议(HTTPS)混合内容,默认不允许加载HTTP和HTTPS混合内容。

Z
zhou-liting125 已提交
297
**参数:**
L
laosan_ted 已提交
298

H
HelloCrease 已提交
299 300
| 参数名       | 参数类型                        | 必填   | 默认值            | 参数描述      |
| --------- | --------------------------- | ---- | -------------- | --------- |
301
| mixedMode | [MixedMode](#mixedmode枚举说明) | 是    | MixedMode.None | 要设置的混合内容。 |
Z
zhou-liting125 已提交
302

Z
zhou-liting125 已提交
303
**示例:**
L
laosan_ted 已提交
304

Z
zhou-liting125 已提交
305 306
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
307
  @Entry
L
update  
laosan_ted 已提交
308 309
  @Component
  struct WebComponent {
Y
yamila 已提交
310 311
    controller: WebController = new WebController()
    @State mode: MixedMode = MixedMode.All
L
update  
laosan_ted 已提交
312 313
    build() {
      Column() {
314 315
        Web({ src: 'www.example.com', controller: this.controller })
          .mixedMode(this.mode)
L
update  
laosan_ted 已提交
316 317 318 319 320
      }
    }
  }
  ```

Z
zhou-liting125 已提交
321 322 323 324 325 326
### onlineImageAccess

onlineImageAccess(onlineImageAccess: boolean)

设置是否允许从网络加载图片资源(通过HTTP和HTTPS访问的资源),默认允许访问。

Z
zhou-liting125 已提交
327
**参数:**
L
laosan_ted 已提交
328

329 330
| 参数名               | 参数类型    | 必填   | 默认值  | 参数描述             |
| ----------------- | ------- | ---- | ---- | ---------------- |
331
| onlineImageAccess | boolean | 是    | true | 设置是否允许从网络加载图片资源。 |
Z
zhou-liting125 已提交
332

Z
zhou-liting125 已提交
333
**示例:**
L
laosan_ted 已提交
334

Z
zhou-liting125 已提交
335 336
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
337
  @Entry
L
update  
laosan_ted 已提交
338 339
  @Component
  struct WebComponent {
Y
yamila 已提交
340
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
341 342
    build() {
      Column() {
343 344
        Web({ src: 'www.example.com', controller: this.controller })
          .onlineImageAccess(true)
L
update  
laosan_ted 已提交
345 346 347 348 349
      }
    }
  }
  ```

Z
zhou-liting125 已提交
350 351 352 353 354 355
### zoomAccess

zoomAccess(zoomAccess: boolean)

设置是否支持手势进行缩放,默认允许执行缩放。

Z
zhou-liting125 已提交
356
**参数:**
L
laosan_ted 已提交
357

358 359
| 参数名        | 参数类型    | 必填   | 默认值  | 参数描述          |
| ---------- | ------- | ---- | ---- | ------------- |
360
| zoomAccess | boolean | 是    | true | 设置是否支持手势进行缩放。 |
Z
zhou-liting125 已提交
361

Z
zhou-liting125 已提交
362
**示例:**
L
laosan_ted 已提交
363

Z
zhou-liting125 已提交
364 365
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
366
  @Entry
L
update  
laosan_ted 已提交
367 368
  @Component
  struct WebComponent {
Y
yamila 已提交
369
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
370 371
    build() {
      Column() {
372 373
        Web({ src: 'www.example.com', controller: this.controller })
          .zoomAccess(true)
L
update  
laosan_ted 已提交
374 375 376 377 378
      }
    }
  }
  ```

Z
zhou-liting125 已提交
379 380 381 382 383 384
### overviewModeAccess

overviewModeAccess(overviewModeAccess: boolean)

设置是否使用概览模式加载网页,默认使用该方式。

Z
zhou-liting125 已提交
385
**参数:**
L
laosan_ted 已提交
386

387 388
| 参数名                | 参数类型    | 必填   | 默认值  | 参数描述            |
| ------------------ | ------- | ---- | ---- | --------------- |
389
| overviewModeAccess | boolean | 是    | true | 设置是否使用概览模式加载网页。 |
Z
zhou-liting125 已提交
390

Z
zhou-liting125 已提交
391
**示例:**
L
laosan_ted 已提交
392

Z
zhou-liting125 已提交
393 394
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
395
  @Entry
L
update  
laosan_ted 已提交
396 397
  @Component
  struct WebComponent {
Y
yamila 已提交
398
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
399 400
    build() {
      Column() {
401 402
        Web({ src: 'www.example.com', controller: this.controller })
          .overviewModeAccess(true)
L
update  
laosan_ted 已提交
403 404 405 406 407
      }
    }
  }
  ```

Z
zhou-liting125 已提交
408 409 410 411 412 413
### databaseAccess

databaseAccess(databaseAccess: boolean)

设置是否开启数据库存储API权限,默认不开启。

Z
zhou-liting125 已提交
414
**参数:**
L
laosan_ted 已提交
415

H
HelloCrease 已提交
416 417
| 参数名            | 参数类型    | 必填   | 默认值   | 参数描述              |
| -------------- | ------- | ---- | ----- | ----------------- |
418
| databaseAccess | boolean | 是    | false | 设置是否开启数据库存储API权限。 |
Z
zhou-liting125 已提交
419

L
laosan_ted 已提交
420
**示例:**
L
laosan_ted 已提交
421

Z
zhou-liting125 已提交
422 423
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
424
  @Entry
L
update  
laosan_ted 已提交
425 426
  @Component
  struct WebComponent {
Y
yamila 已提交
427
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
428 429
    build() {
      Column() {
430 431
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
L
update  
laosan_ted 已提交
432 433 434 435 436
      }
    }
  }
  ```

L
laosan_ted 已提交
437 438 439 440 441 442 443
### geolocationAccess

geolocationAccess(geolocationAccess: boolean)

设置是否开启获取地理位置权限,默认开启。

**参数:**
L
laosan_ted 已提交
444

H
HelloCrease 已提交
445 446 447
| 参数名               | 参数类型    | 必填   | 默认值  | 参数描述            |
| ----------------- | ------- | ---- | ---- | --------------- |
| geolocationAccess | boolean | 是    | true | 设置是否开启获取地理位置权限。 |
L
laosan_ted 已提交
448 449

**示例:**
L
laosan_ted 已提交
450

L
laosan_ted 已提交
451 452 453 454 455
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
456
    controller: WebController = new WebController()
L
laosan_ted 已提交
457 458
    build() {
      Column() {
459 460
        Web({ src: 'www.example.com', controller: this.controller })
          .geolocationAccess(true)
L
laosan_ted 已提交
461 462 463 464 465
      }
    }
  }
  ```

L
laosan_ted 已提交
466 467 468 469
### mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

L
laosan_ted 已提交
470
设置有声视频播放是否需要用户手动点击,静音视频播放不受该接口管控。
L
laosan_ted 已提交
471 472 473

**参数:**

H
HelloCrease 已提交
474 475
| 参数名    | 参数类型    | 必填   | 默认值  | 参数描述              |
| ------ | ------- | ---- | ---- | ----------------- |
L
laosan_ted 已提交
476
| access | boolean | 是    | true | 设置有声视频播放是否需要用户手动点击。 |
L
laosan_ted 已提交
477 478 479 480 481 482 483 484

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
485 486
    controller: WebController = new WebController()
    @State access: boolean = true
L
laosan_ted 已提交
487 488 489 490 491 492 493 494 495
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .mediaPlayGestureAccess(this.access)
      }
    }
  }
  ```

X
xiongjun_gitee 已提交
496 497 498 499 500 501 502 503
### multiWindowAccess<sup>9+</sup>

multiWindowAccess(multiWindow: boolean)

设置是否开启多窗口权限,默认不开启。

**参数:**

H
HelloCrease 已提交
504 505
| 参数名         | 参数类型    | 必填   | 默认值   | 参数描述         |
| ----------- | ------- | ---- | ----- | ------------ |
X
xiongjun_gitee 已提交
506 507 508 509 510 511 512 513 514
| multiWindow | boolean | 是    | false | 设置是否开启多窗口权限。 |

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
515
    controller: WebController = new WebController()
X
xiongjun_gitee 已提交
516 517 518 519 520 521 522 523 524
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .multiWindowAccess(true)
      }
    }
  }
  ```

L
laosan_ted 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
### horizontalScrollBarAccess<sup>9+</sup>

horizontalScrollBarAccess(horizontalScrollBar: boolean)

设置是否显示横向滚动条,包括系统默认滚动条和用户自定义滚动条。默认显示。

**参数:**

| 参数名         | 参数类型    | 必填   | 默认值   | 参数描述         |
| ----------- | ------- | ---- | ----- | ------------ |
| horizontalScrollBar | boolean | 是    | true | 设置是否显示横向滚动条。 |

**示例:**

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

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

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

verticalScrollBarAccess(verticalScrollBar: boolean)

设置是否显示纵向滚动条,包括系统默认滚动条和用户自定义滚动条。默认显示。

**参数:**

| 参数名         | 参数类型    | 必填   | 默认值   | 参数描述         |
| ----------- | ------- | ---- | ----- | ------------ |
| verticalScrollBarAccess | boolean | 是    | true | 设置是否显示纵向滚动条。 |

**示例:**

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

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


Z
zhou-liting125 已提交
628 629 630 631 632 633
### cacheMode

cacheMode(cacheMode: CacheMode)

设置缓存模式。

Z
zhou-liting125 已提交
634
**参数:**
L
laosan_ted 已提交
635

H
HelloCrease 已提交
636 637
| 参数名       | 参数类型                        | 必填   | 默认值               | 参数描述      |
| --------- | --------------------------- | ---- | ----------------- | --------- |
638
| cacheMode | [CacheMode](#cachemode枚举说明) | 是    | CacheMode.Default | 要设置的缓存模式。 |
Z
zhou-liting125 已提交
639

Z
zhou-liting125 已提交
640
**示例:**
L
laosan_ted 已提交
641

Z
zhou-liting125 已提交
642 643
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
644
  @Entry
L
update  
laosan_ted 已提交
645 646
  @Component
  struct WebComponent {
Y
yamila 已提交
647 648
    controller: WebController = new WebController()
    @State mode: CacheMode = CacheMode.None
L
update  
laosan_ted 已提交
649 650
    build() {
      Column() {
651 652
        Web({ src: 'www.example.com', controller: this.controller })
          .cacheMode(this.mode)
L
update  
laosan_ted 已提交
653 654 655 656 657
      }
    }
  }
  ```

L
laosan_ted 已提交
658
### textZoomRatio<sup>9+</sup>
Z
zhou-liting125 已提交
659

L
laosan_ted 已提交
660
textZoomRatio(textZoomRatio: number)
Z
zhou-liting125 已提交
661 662 663

设置页面的文本缩放百分比,默认为100%。

Z
zhou-liting125 已提交
664
**参数:**
L
laosan_ted 已提交
665

H
HelloCrease 已提交
666 667 668
| 参数名           | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------------- | ------ | ---- | ---- | --------------- |
| textZoomRatio | number | 是    | 100  | 要设置的页面的文本缩放百分比。 |
Z
zhou-liting125 已提交
669

Z
zhou-liting125 已提交
670
**示例:**
L
laosan_ted 已提交
671

Z
zhou-liting125 已提交
672 673
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
674
  @Entry
L
update  
laosan_ted 已提交
675 676
  @Component
  struct WebComponent {
Y
yamila 已提交
677 678
    controller: WebController = new WebController()
    @State atio: number = 150
L
update  
laosan_ted 已提交
679 680
    build() {
      Column() {
681 682
        Web({ src: 'www.example.com', controller: this.controller })
          .textZoomRatio(this.atio)
L
update  
laosan_ted 已提交
683 684 685 686 687
      }
    }
  }
  ```

L
laosan_ted 已提交
688 689 690 691 692 693 694 695
### initialScale<sup>9+</sup>

initialScale(percent: number)

设置整体页面的缩放百分比,默认为100%。

**参数:**

H
HelloCrease 已提交
696 697 698
| 参数名     | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------- | ------ | ---- | ---- | --------------- |
| percent | number | 是    | 100  | 要设置的整体页面的缩放百分比。 |
L
laosan_ted 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

**示例:**

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

Z
zhou-liting125 已提交
718 719 720 721 722 723
### userAgent

userAgent(userAgent: string)

设置用户代理。

Z
zhou-liting125 已提交
724
**参数:**
L
laosan_ted 已提交
725

726 727 728
| 参数名       | 参数类型   | 必填   | 默认值  | 参数描述      |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | 是    | -    | 要设置的用户代理。 |
Z
zengyawen 已提交
729

Z
zhou-liting125 已提交
730
**示例:**
L
laosan_ted 已提交
731

Z
zhou-liting125 已提交
732 733
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
734
  @Entry
L
update  
laosan_ted 已提交
735 736
  @Component
  struct WebComponent {
Y
yamila 已提交
737 738
    controller: WebController = new WebController()
    @State userAgent:string = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
L
update  
laosan_ted 已提交
739 740
    build() {
      Column() {
741 742
        Web({ src: 'www.example.com', controller: this.controller })
          .userAgent(this.userAgent)
L
update  
laosan_ted 已提交
743 744 745 746 747
      }
    }
  }
  ```

L
laosan_ted 已提交
748 749 750 751 752 753 754 755
### webDebuggingAccess<sup>9+</sup>

webDebuggingAccess(webDebuggingAccess: boolean)

设置是否启用网页调试功能。

**参数:**

H
HelloCrease 已提交
756 757 758
| 参数名                | 参数类型    | 必填   | 默认值   | 参数描述          |
| ------------------ | ------- | ---- | ----- | ------------- |
| webDebuggingAccess | boolean | 是    | false | 设置是否启用网页调试功能。 |
L
laosan_ted 已提交
759 760 761 762 763 764 765 766

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
767 768
    controller: WebController = new WebController()
    @State webDebuggingAccess: boolean = true
L
laosan_ted 已提交
769 770 771 772 773 774 775 776 777
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webDebuggingAccess(this.webDebuggingAccess)
      }
    }
  }
  ```

C
chensi10 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
### blockNetwork<sup>9+</sup>

blockNetwork(block: boolean)

设置Web组件是否阻止从网络加载资源。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                            |
| ------ | -------- | ---- | ------ | ----------------------------------- |
| block  | boolean  | 是   | false  | 设置Web组件是否阻止从网络加载资源。 |

**示例:**

  ```ts
  // xxx.ets
C
chensi10 已提交
794
  import web_webview from '@ohos.web.webview'
C
chensi10 已提交
795 796 797
  @Entry
  @Component
  struct WebComponent {
C
chensi10 已提交
798
    controller: web_webview.WebviewController = new web_webview.WebviewController()
C
chensi10 已提交
799 800 801 802
    @State block: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
C
chensi10 已提交
803
          .blockNetwork(this.block)
C
chensi10 已提交
804 805 806 807 808 809 810 811 812
      }
    }
  }
  ```

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

defaultFixedFontSize(size: number)

813
设置网页的默认等宽字体大小。
C
chensi10 已提交
814 815 816 817 818

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                     |
| ------ | -------- | ---- | ------ | ---------------------------- |
819
| size   | number   | 是   | 13     | 设置网页的默认等宽字体大小,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
820 821 822 823 824 825 826 827 828 829

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
830
    @State fontSize: number = 16
C
chensi10 已提交
831 832 833
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
834
          .defaultFixedFontSize(this.fontSize)
C
chensi10 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
      }
    }
  }
  ```

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

defaultFontSize(size: number)

设置网页的默认字体大小。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                 |
| ------ | -------- | ---- | ------ | ------------------------ |
850
| size   | number   | 是   | 16     | 设置网页的默认字体大小,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
851 852 853 854 855 856 857 858 859 860

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
861
    @State fontSize: number = 13
C
chensi10 已提交
862 863 864
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
865
          .defaultFontSize(this.fontSize)
C
chensi10 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
      }
    }
  }
  ```

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

minFontSize(size: number)

设置网页字体大小最小值。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                 |
| ------ | -------- | ---- | ------ | ------------------------ |
881
| size   | number   | 是   | 8      | 设置网页字体大小最小值,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
882 883 884 885 886 887 888 889 890 891

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
892
    @State fontSize: number = 13
C
chensi10 已提交
893 894 895
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
896
          .minFontSize(this.fontSize)
C
chensi10 已提交
897 898 899 900 901
      }
    }
  }
  ```

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
### minLogicalFontSize<sup>9+</sup>

minLogicalFontSize(size: number)

设置网页逻辑字体大小最小值。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                 |
| ------ | -------- | ---- | ------ | ------------------------ |
| size   | number   | 是   | 8      | 设置网页逻辑字体大小最小值,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
923
    @State fontSize: number = 13
924 925 926
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
927
          .minLogicalFontSize(this.fontSize)
928 929 930 931 932 933
      }
    }
  }
  ```


C
chensi10 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
### webFixedFont<sup>9+</sup>

webFixedFont(family: string)

设置网页的fixed font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值    | 参数描述                     |
| ------ | -------- | ---- | --------- | ---------------------------- |
| family | string   | 是   | monospace | 设置网页的fixed font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "monospace"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webFixedFont(this.family)
      }
    }
  }
  ```

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

webSansSerifFont(family: string)

设置网页的sans serif font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值     | 参数描述                          |
| ------ | -------- | ---- | ---------- | --------------------------------- |
| family | string   | 是   | sans-serif | 设置网页的sans serif font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webSansSerifFont(this.family)
      }
    }
  }
  ```

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

webSerifFont(family: string)

设置网页的serif font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                     |
| ------ | -------- | ---- | ------ | ---------------------------- |
| family | string   | 是   | serif  | 设置网页的serif font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "serif"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webSerifFont(this.family)
      }
    }
  }
  ```

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

webStandardFont(family: string)

设置网页的standard font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值     | 参数描述                        |
| ------ | -------- | ---- | ---------- | ------------------------------- |
| family | string   | 是   | sans serif | 设置网页的standard font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "sans-serif"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webStandardFont(this.family)
      }
    }
  }
  ```

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

webFantasyFont(family: string)

设置网页的fantasy font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | 是   | fantasy | 设置网页的fantasy font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "fantasy"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webFantasyFont(this.family)
      }
    }
  }
  ```

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

webCursiveFont(family: string)

设置网页的cursive font字体库。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | -------- | ---- | ------- | ------------------------------ |
| family | string   | 是   | cursive | 设置网页的cursive font字体库。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State family: string = "cursive"
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .webCursiveFont(this.family)
      }
    }
  }
  ```

Y
yuhaoge 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
### darkMode<sup>9+</sup>

darkMode(mode: WebDarkMode)

设置Web深色模式,默认关闭。当深色模式开启时,Web将启用媒体查询prefer-color-scheme中网页所定义的深色样式,若网页未定义深色样式,则保持原状。如需开启强制深色模式,建议配合[forceDarkAccess](#forcedarkaccess9)使用。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | ----------- | ---- | --------------- | ------------------ |
|  mode  | [WebDarkMode](#webdarkmode9枚举说明) | 是   | WebDarkMode.Off | 设置Web的深色模式为关闭、开启或跟随系统。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .darkMode(this.mode)
      }
    }
  }
  ```

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

forceDarkAccess(access: boolean)

设置网页是否开启强制深色模式。默认关闭。该属性仅在[darkMode](#darkmode9)开启深色模式时生效。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | ------- | ---- | ----- | ------------------ |
| access | boolean | 是   | false | 设置网页是否开启强制深色模式。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State mode: WebDarkMode = WebDarkMode.On
    @State access: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .darkMode(this.mode)
          .forceDarkAccess(this.access)
      }
    }
  }
  ```

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
### pinchSmooth<sup>9+</sup>

pinchSmooth(isEnabled: boolean)

设置网页是否开启捏合流畅模式。

**参数:**

| 参数名    | 参数类型 | 必填 | 默认值 | 参数描述                   |
| --------- | -------- | ---- | ------ | -------------------------- |
| isEnabled | boolean  | 是   | false  | 网页是否开启捏合流畅模式。 |

**示例:**

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

Z
zengyawen 已提交
1214 1215
## 事件

L
liwenzhen 已提交
1216
不支持通用事件。
Z
zengyawen 已提交
1217

L
update  
laosan_ted 已提交
1218 1219 1220 1221 1222 1223
### onAlert

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

网页触发alert()告警弹窗时触发回调。

Z
zhou-liting125 已提交
1224
**参数:**
L
laosan_ted 已提交
1225

1226 1227 1228 1229 1230
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
1231

Z
zhou-liting125 已提交
1232
**返回值:**
L
laosan_ted 已提交
1233

1234 1235
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
L
dddd  
lixiang 已提交
1236
| boolean | 当回调返回true时,应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。当回调返回false时,web组件暂不支持触发默认弹窗。 |
L
update  
laosan_ted 已提交
1237

Z
zhou-liting125 已提交
1238
**示例:**
L
laosan_ted 已提交
1239

Z
zhou-liting125 已提交
1240 1241
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1242
  @Entry
L
update  
laosan_ted 已提交
1243 1244
  @Component
  struct WebComponent {
Y
yamila 已提交
1245
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
1246 1247
    build() {
      Column() {
1248
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1249 1250
          .onAlert((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
1251
              title: 'onAlert',
L
laosan_ted 已提交
1252
              message: 'text',
L
laosan_ted 已提交
1253 1254 1255 1256 1257 1258 1259 1260
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1261 1262 1263 1264 1265 1266 1267 1268
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1269
            return true
L
update  
laosan_ted 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
          })
      }
    }
  }
  ```

### onBeforeUnload

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

L
laosan_ted 已提交
1280
刷新或关闭场景下,在即将离开当前页面时触发此回调。刷新或关闭当前页面应先通过点击等方式获取焦点,才会触发此回调。
L
update  
laosan_ted 已提交
1281

Z
zhou-liting125 已提交
1282
**参数:**
L
laosan_ted 已提交
1283

1284 1285 1286 1287 1288
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
1289

Z
zhou-liting125 已提交
1290
**返回值:**
L
laosan_ted 已提交
1291

1292 1293
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
L
dddd  
lixiang 已提交
1294
| boolean | 当回调返回true时,应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。当回调返回false时,web组件暂不支持触发默认弹窗。 |
L
update  
laosan_ted 已提交
1295

Z
zhou-liting125 已提交
1296
**示例:**
L
laosan_ted 已提交
1297

Z
zhou-liting125 已提交
1298 1299
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1300
  @Entry
L
update  
laosan_ted 已提交
1301 1302
  @Component
  struct WebComponent {
Y
yamila 已提交
1303
    controller: WebController = new WebController()
L
laosan_ted 已提交
1304
  
L
update  
laosan_ted 已提交
1305 1306
    build() {
      Column() {
Z
zhou-liting125 已提交
1307
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1308
          .onBeforeUnload((event) => {
Y
yamila 已提交
1309 1310
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
L
laosan_ted 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
            AlertDialog.show({
              title: 'onBeforeUnload',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1330
            return true
L
laosan_ted 已提交
1331 1332
          })
      }
L
update  
laosan_ted 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    }
  }
  ```

### onConfirm

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

网页调用confirm()告警时触发此回调。

Z
zhou-liting125 已提交
1343
**参数:**
L
laosan_ted 已提交
1344

1345 1346 1347 1348 1349
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
1350

Z
zhou-liting125 已提交
1351
**返回值:**
L
laosan_ted 已提交
1352

1353 1354
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
L
dddd  
lixiang 已提交
1355
| boolean | 当回调返回true时,应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。当回调返回false时,web组件暂不支持触发默认弹窗。 |
L
update  
laosan_ted 已提交
1356

Z
zhou-liting125 已提交
1357
**示例:**
L
laosan_ted 已提交
1358

Z
zhou-liting125 已提交
1359 1360
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1361
  @Entry
L
update  
laosan_ted 已提交
1362 1363
  @Component
  struct WebComponent {
Y
yamila 已提交
1364
    controller: WebController = new WebController()
L
laosan_ted 已提交
1365
  
L
update  
laosan_ted 已提交
1366 1367
    build() {
      Column() {
Z
zhou-liting125 已提交
1368
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1369
          .onConfirm((event) => {
Y
yamila 已提交
1370 1371 1372
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            console.log("event.result:" + event.result)
L
laosan_ted 已提交
1373
            AlertDialog.show({
L
laosan_ted 已提交
1374
              title: 'onConfirm',
L
laosan_ted 已提交
1375
              message: 'text',
L
laosan_ted 已提交
1376 1377 1378 1379 1380 1381 1382 1383
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1384 1385 1386 1387 1388 1389 1390 1391
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1392
            return true
L
update  
laosan_ted 已提交
1393
          })
L
laosan_ted 已提交
1394
      }
L
update  
laosan_ted 已提交
1395 1396 1397 1398 1399 1400 1401 1402
    }
  }
  ```

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

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

Z
zhou-liting125 已提交
1403
**参数:**
L
laosan_ted 已提交
1404

1405 1406 1407 1408 1409
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
1410

Z
zhou-liting125 已提交
1411
**返回值:**
L
laosan_ted 已提交
1412

1413 1414
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
L
dddd  
lixiang 已提交
1415
| boolean | 当回调返回true时,应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。当回调返回false时,web组件暂不支持触发默认弹窗。 |
L
update  
laosan_ted 已提交
1416

Z
zhou-liting125 已提交
1417
**示例:**
L
laosan_ted 已提交
1418

Z
zhou-liting125 已提交
1419 1420
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1421
  @Entry
L
update  
laosan_ted 已提交
1422 1423
  @Component
  struct WebComponent {
Y
yamila 已提交
1424
    controller: WebController = new WebController()
L
laosan_ted 已提交
1425
  
L
update  
laosan_ted 已提交
1426 1427
    build() {
      Column() {
1428
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1429
          .onPrompt((event) => {
Y
yamila 已提交
1430 1431 1432
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
L
laosan_ted 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
            AlertDialog.show({
              title: 'onPrompt',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1452
            return true
L
laosan_ted 已提交
1453 1454
          })
      }
L
update  
laosan_ted 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    }
  }
  ```

### onConsole

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

通知宿主应用JavaScript console消息。

Z
zhou-liting125 已提交
1465
**参数:**
L
laosan_ted 已提交
1466

1467 1468 1469
| 参数名     | 参数类型                              | 参数描述      |
| ------- | --------------------------------- | --------- |
| message | [ConsoleMessage](#consolemessage) | 触发的控制台信息。 |
L
update  
laosan_ted 已提交
1470

Z
zhou-liting125 已提交
1471
**返回值:**
L
laosan_ted 已提交
1472

1473 1474 1475
| 类型      | 说明                                  |
| ------- | ----------------------------------- |
| boolean | 当返回true时,该条消息将不会再打印至控制台,反之仍会打印至控制台。 |
L
update  
laosan_ted 已提交
1476

Z
zhou-liting125 已提交
1477
**示例:**
L
laosan_ted 已提交
1478

Z
zhou-liting125 已提交
1479 1480
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1481
  @Entry
L
update  
laosan_ted 已提交
1482 1483
  @Component
  struct WebComponent {
Y
yamila 已提交
1484
    controller: WebController = new WebController()
L
laosan_ted 已提交
1485
  
L
update  
laosan_ted 已提交
1486 1487
    build() {
      Column() {
Z
zhou-liting125 已提交
1488
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1489
          .onConsole((event) => {
Y
yamila 已提交
1490 1491 1492 1493 1494
            console.log('getMessage:' + event.message.getMessage())
            console.log('getSourceId:' + event.message.getSourceId())
            console.log('getLineNumber:' + event.message.getLineNumber())
            console.log('getMessageLevel:' + event.message.getMessageLevel())
            return false
L
laosan_ted 已提交
1495 1496
          })
      }
L
update  
laosan_ted 已提交
1497 1498 1499 1500 1501 1502 1503 1504
    }
  }
  ```

### onDownloadStart

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

Z
zhou-liting125 已提交
1505
**参数:**
L
laosan_ted 已提交
1506

1507 1508 1509 1510 1511 1512
| 参数名                | 参数类型          | 参数描述                                |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | 文件下载的URL。                           |
| contentDisposition | string        | 服务器返回的 Content-Disposition响应头,可能为空。 |
| mimetype           | string        | 服务器返回内容媒体类型(MIME)信息。                |
| contentLength      | contentLength | 服务器返回文件的长度。                         |
L
update  
laosan_ted 已提交
1513

Z
zhou-liting125 已提交
1514
**示例:**
L
laosan_ted 已提交
1515

Z
zhou-liting125 已提交
1516 1517
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1518
  @Entry
L
update  
laosan_ted 已提交
1519 1520
  @Component
  struct WebComponent {
Y
yamila 已提交
1521
    controller: WebController = new WebController()
L
laosan_ted 已提交
1522
  
L
update  
laosan_ted 已提交
1523 1524
    build() {
      Column() {
Z
zhou-liting125 已提交
1525
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1526
          .onDownloadStart((event) => {
Y
yamila 已提交
1527 1528 1529 1530 1531
            console.log('url:' + event.url)
            console.log('userAgent:' + event.userAgent)
            console.log('contentDisposition:' + event.contentDisposition)
            console.log('contentLength:' + event.contentLength)
            console.log('mimetype:' + event.mimetype)
L
laosan_ted 已提交
1532 1533
          })
      }
L
update  
laosan_ted 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    }
  }
  ```

### onErrorReceive

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

网页加载遇到错误时触发该回调。出于性能考虑,建议此回调中尽量执行简单逻辑。

Z
zhou-liting125 已提交
1544
**参数:**
L
laosan_ted 已提交
1545

1546 1547 1548 1549
| 参数名     | 参数类型                                     | 参数描述            |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。      |
| error   | [WebResourceError](#webresourceerror)    | 网页加载资源错误的封装信息 。 |
L
update  
laosan_ted 已提交
1550

Z
zhou-liting125 已提交
1551
**示例:**
L
laosan_ted 已提交
1552

Z
zhou-liting125 已提交
1553 1554
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1555
  @Entry
L
update  
laosan_ted 已提交
1556 1557
  @Component
  struct WebComponent {
Y
yamila 已提交
1558
    controller: WebController = new WebController()
L
laosan_ted 已提交
1559
  
L
update  
laosan_ted 已提交
1560 1561
    build() {
      Column() {
Z
zhou-liting125 已提交
1562
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1563
          .onErrorReceive((event) => {
Y
yamila 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572
            console.log('getErrorInfo:' + event.error.getErrorInfo())
            console.log('getErrorCode:' + event.error.getErrorCode())
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
L
laosan_ted 已提交
1573
            for (let i of result) {
Y
yamila 已提交
1574
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
L
laosan_ted 已提交
1575 1576 1577
            }
          })
      }
L
update  
laosan_ted 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    }
  }
  ```

### onHttpErrorReceive

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

网页加载资源遇到的HTTP错误(响应码>=400)时触发该回调。

Z
zhou-liting125 已提交
1588
**参数:**
L
laosan_ted 已提交
1589

1590 1591 1592
| 参数名     | 参数类型                                     | 参数描述            |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。      |
L
laosan_ted 已提交
1593
| response | [WebResourceResponse](#webresourceresponse)    | 资源响应的封装信息。 |
L
update  
laosan_ted 已提交
1594

Z
zhou-liting125 已提交
1595
**示例:**
L
laosan_ted 已提交
1596

Z
zhou-liting125 已提交
1597 1598
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1599
  @Entry
L
update  
laosan_ted 已提交
1600 1601
  @Component
  struct WebComponent {
Y
yamila 已提交
1602
    controller: WebController = new WebController()
L
laosan_ted 已提交
1603
  
L
update  
laosan_ted 已提交
1604 1605
    build() {
      Column() {
Z
zhou-liting125 已提交
1606
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1607
          .onHttpErrorReceive((event) => {
Y
yamila 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getResponseData:' + event.response.getResponseData())
            console.log('getResponseEncoding:' + event.response.getResponseEncoding())
            console.log('getResponseMimeType:' + event.response.getResponseMimeType())
            console.log('getResponseCode:' + event.response.getResponseCode())
            console.log('getReasonMessage:' + event.response.getReasonMessage())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
L
laosan_ted 已提交
1619
            for (let i of result) {
Y
yamila 已提交
1620
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1621
            }
Y
yamila 已提交
1622 1623
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
L
laosan_ted 已提交
1624
            for (let i of resph) {
Y
yamila 已提交
1625
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1626 1627 1628
            }
          })
      }
L
update  
laosan_ted 已提交
1629 1630 1631 1632 1633 1634 1635 1636
    }
  }
  ```

### onPageBegin

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

1637

L
update  
laosan_ted 已提交
1638 1639
网页开始加载时触发该回调,且只在主frame触发,iframe或者frameset的内容加载时不会触发此回调。

Z
zhou-liting125 已提交
1640
**参数:**
L
laosan_ted 已提交
1641

1642 1643 1644
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1645

Z
zhou-liting125 已提交
1646
**示例:**
L
laosan_ted 已提交
1647

Z
zhou-liting125 已提交
1648 1649
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1650
  @Entry
L
update  
laosan_ted 已提交
1651 1652
  @Component
  struct WebComponent {
Y
yamila 已提交
1653
    controller: WebController = new WebController()
L
laosan_ted 已提交
1654
  
L
update  
laosan_ted 已提交
1655 1656
    build() {
      Column() {
Z
zhou-liting125 已提交
1657
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1658
          .onPageBegin((event) => {
Y
yamila 已提交
1659
            console.log('url:' + event.url)
L
laosan_ted 已提交
1660 1661
          })
      }
L
update  
laosan_ted 已提交
1662 1663 1664 1665 1666 1667 1668 1669
    }
  }
  ```

### onPageEnd

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

1670

L
update  
laosan_ted 已提交
1671 1672
网页加载完成时触发该回调,且只在主frame触发。

Z
zhou-liting125 已提交
1673
**参数:**
L
laosan_ted 已提交
1674

1675 1676 1677
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1678

Z
zhou-liting125 已提交
1679
**示例:**
L
laosan_ted 已提交
1680

Z
zhou-liting125 已提交
1681 1682
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1683
  @Entry
L
update  
laosan_ted 已提交
1684 1685
  @Component
  struct WebComponent {
Y
yamila 已提交
1686
    controller: WebController = new WebController()
L
laosan_ted 已提交
1687
  
L
update  
laosan_ted 已提交
1688 1689
    build() {
      Column() {
Z
zhou-liting125 已提交
1690
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1691
          .onPageEnd((event) => {
Y
yamila 已提交
1692
            console.log('url:' + event.url)
L
laosan_ted 已提交
1693 1694
          })
      }
L
update  
laosan_ted 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
    }
  }
  ```

### onProgressChange

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

网页加载进度变化时触发该回调。

Z
zhou-liting125 已提交
1705
**参数:**
L
laosan_ted 已提交
1706

1707 1708 1709
| 参数名         | 参数类型   | 参数描述                  |
| ----------- | ------ | --------------------- |
| newProgress | number | 新的加载进度,取值范围为0到100的整数。 |
L
update  
laosan_ted 已提交
1710

L
laosan_ted 已提交
1711 1712
**示例:**

Z
zhou-liting125 已提交
1713 1714
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1715
  @Entry
L
update  
laosan_ted 已提交
1716 1717
  @Component
  struct WebComponent {
Y
yamila 已提交
1718
    controller: WebController = new WebController()
L
laosan_ted 已提交
1719
  
L
update  
laosan_ted 已提交
1720 1721
    build() {
      Column() {
Z
zhou-liting125 已提交
1722
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1723 1724 1725 1726
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
L
update  
laosan_ted 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
    }
  }
  ```

### onTitleReceive

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

网页document标题更改时触发该回调。

Z
zhou-liting125 已提交
1737
**参数:**
L
laosan_ted 已提交
1738

1739 1740 1741
| 参数名   | 参数类型   | 参数描述          |
| ----- | ------ | ------------- |
| title | string | document标题内容。 |
L
update  
laosan_ted 已提交
1742

L
laosan_ted 已提交
1743 1744
**示例:**

Z
zhou-liting125 已提交
1745 1746
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1747
  @Entry
L
update  
laosan_ted 已提交
1748 1749
  @Component
  struct WebComponent {
Y
yamila 已提交
1750
    controller: WebController = new WebController()
L
laosan_ted 已提交
1751
  
L
update  
laosan_ted 已提交
1752 1753
    build() {
      Column() {
Z
zhou-liting125 已提交
1754
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1755 1756 1757 1758
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
L
update  
laosan_ted 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    }
  }
  ```

### onRefreshAccessedHistory

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

加载网页页面完成时触发该回调,用于应用更新其访问的历史链接。

Z
zhou-liting125 已提交
1769
**参数:**
L
laosan_ted 已提交
1770

H
HelloCrease 已提交
1771 1772 1773
| 参数名         | 参数类型    | 参数描述                                     |
| ----------- | ------- | ---------------------------------------- |
| url         | string  | 访问的url。                                  |
L
laosan_ted 已提交
1774
| isRefreshed | boolean | true表示该页面是被重新加载的(调用[refresh](#refresh)接口),false表示该页面是新加载的。 |
L
update  
laosan_ted 已提交
1775

L
laosan_ted 已提交
1776 1777
**示例:**

Z
zhou-liting125 已提交
1778 1779
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1780
  @Entry
L
update  
laosan_ted 已提交
1781 1782
  @Component
  struct WebComponent {
Y
yamila 已提交
1783
    controller: WebController = new WebController()
L
laosan_ted 已提交
1784
  
L
update  
laosan_ted 已提交
1785 1786
    build() {
      Column() {
Z
zhou-liting125 已提交
1787
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1788
          .onRefreshAccessedHistory((event) => {
Y
yamila 已提交
1789
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
L
laosan_ted 已提交
1790 1791
          })
      }
L
update  
laosan_ted 已提交
1792 1793 1794 1795
    }
  }
  ```

L
laosan_ted 已提交
1796
### onRenderExited<sup>9+</sup>
L
update  
laosan_ted 已提交
1797 1798 1799 1800 1801

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

应用渲染进程异常退出时触发该回调。

Z
zhou-liting125 已提交
1802
**参数:**
L
laosan_ted 已提交
1803

1804 1805 1806
| 参数名              | 参数类型                                     | 参数描述             |
| ---------------- | ---------------------------------------- | ---------------- |
| renderExitReason | [RenderExitReason](#renderexitreason枚举说明) | 渲染进程进程异常退出的具体原因。 |
L
update  
laosan_ted 已提交
1807

L
laosan_ted 已提交
1808 1809
**示例:**

Z
zhou-liting125 已提交
1810 1811
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1812
  @Entry
L
update  
laosan_ted 已提交
1813 1814
  @Component
  struct WebComponent {
Y
yamila 已提交
1815
    controller: WebController = new WebController()
L
laosan_ted 已提交
1816
  
L
update  
laosan_ted 已提交
1817 1818
    build() {
      Column() {
L
laosan_ted 已提交
1819 1820
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
Y
yamila 已提交
1821
            console.log('reason:' + event.renderExitReason)
L
laosan_ted 已提交
1822 1823
          })
      }
L
update  
laosan_ted 已提交
1824 1825 1826 1827 1828 1829
    }
  }
  ```

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

L
laosan_ted 已提交
1830
onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => boolean)
L
update  
laosan_ted 已提交
1831

Z
zhou-liting125 已提交
1832
调用此函数以处理具有“文件”输入类型的HTML表单,以响应用户按下的“选择文件”按钮。
L
update  
laosan_ted 已提交
1833

Z
zhou-liting125 已提交
1834
**参数:**
L
laosan_ted 已提交
1835

1836 1837 1838 1839
| 参数名          | 参数类型                                     | 参数描述              |
| ------------ | ---------------------------------------- | ----------------- |
| result       | [FileSelectorResult](#fileselectorresult9) | 用于通知Web组件文件选择的结果。 |
| fileSelector | [FileSelectorParam](#fileselectorparam9) | 文件选择器的相关信息。       |
L
update  
laosan_ted 已提交
1840

L
laosan_ted 已提交
1841 1842
**返回值:**

H
HelloCrease 已提交
1843 1844
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
L
dddd  
lixiang 已提交
1845
| boolean | 当返回值为true时,用户可以调用系统提供的弹窗能力。当回调返回false时,web组件暂不支持触发默认弹窗。 |
L
laosan_ted 已提交
1846

L
laosan_ted 已提交
1847 1848
**示例:**

Z
zhou-liting125 已提交
1849 1850
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1851
  @Entry
L
update  
laosan_ted 已提交
1852 1853
  @Component
  struct WebComponent {
Y
yamila 已提交
1854
    controller: WebController = new WebController()
L
laosan_ted 已提交
1855

L
update  
laosan_ted 已提交
1856 1857
    build() {
      Column() {
Z
zhou-liting125 已提交
1858
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
          .onShowFileSelector((event) => {
            AlertDialog.show({
              title: event.fileSelector.getTitle(),
              message: 'isCapture:' + event.fileSelector.isCapture() + " mode:" + event.fileSelector.getMode() + 'acceptType:' + event.fileSelector.getAcceptType(),
              confirm: {
                value: 'upload',
                action: () => {
                  let fileList: Array<string> = [
                    '/data/storage/el2/base/test',
                  ]
                  event.result.handleFileList(fileList)
                }
              },
              cancel: () => {
                let fileList: Array<string> = []
                event.result.handleFileList(fileList)
L
update  
laosan_ted 已提交
1875
              }
L
laosan_ted 已提交
1876
            })
Y
yamila 已提交
1877
            return true
L
update  
laosan_ted 已提交
1878
          })
L
laosan_ted 已提交
1879
      }
L
update  
laosan_ted 已提交
1880 1881 1882 1883
    }
  }
  ```

L
laosan_ted 已提交
1884 1885 1886 1887 1888 1889 1890 1891
### onResourceLoad<sup>9+</sup>

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

通知Web组件所加载的资源文件url信息。

**参数:**

H
HelloCrease 已提交
1892 1893 1894
| 参数名  | 参数类型   | 参数描述           |
| ---- | ------ | -------------- |
| url  | string | 所加载的资源文件url信息。 |
L
laosan_ted 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923

**示例:**

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

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

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

当前页面显示比例的变化时触发该回调。

**参数:**

H
HelloCrease 已提交
1924 1925
| 参数名      | 参数类型   | 参数描述         |
| -------- | ------ | ------------ |
L
laosan_ted 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
| oldScale | number | 变化前的显示比例百分比。 |
| newScale | number | 变化后的显示比例百分比。 |

**示例:**

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

L
update  
laosan_ted 已提交
1949 1950 1951 1952 1953 1954
### onUrlLoadIntercept

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

当Web组件加载url之前触发该回调,用于是否阻止此次访问。

Z
zhou-liting125 已提交
1955
**参数:**
L
laosan_ted 已提交
1956

1957 1958 1959
| 参数名  | 参数类型                                     | 参数描述      |
| ---- | ---------------------------------------- | --------- |
| data | string / [WebResourceRequest](#webresourcerequest) | url的相关信息。 |
L
update  
laosan_ted 已提交
1960

Z
zhou-liting125 已提交
1961
**返回值:**
L
laosan_ted 已提交
1962

1963 1964 1965
| 类型      | 说明                       |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |
L
update  
laosan_ted 已提交
1966

L
laosan_ted 已提交
1967 1968
**示例:**

Z
zhou-liting125 已提交
1969 1970
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1971
  @Entry
L
update  
laosan_ted 已提交
1972 1973
  @Component
  struct WebComponent {
Y
yamila 已提交
1974
    controller: WebController = new WebController()
L
laosan_ted 已提交
1975
  
L
update  
laosan_ted 已提交
1976 1977
    build() {
      Column() {
Z
zhou-liting125 已提交
1978
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1979 1980
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
Y
yamila 已提交
1981
            return true
L
laosan_ted 已提交
1982 1983
          })
      }
L
update  
laosan_ted 已提交
1984 1985 1986 1987 1988 1989
    }
  }
  ```

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

L
laosan_ted 已提交
1990
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
L
update  
laosan_ted 已提交
1991 1992 1993

当Web组件加载url之前触发该回调,用于拦截url并返回响应数据。

Z
zhou-liting125 已提交
1994
**参数:**
L
laosan_ted 已提交
1995

1996 1997 1998
| 参数名     | 参数类型                                     | 参数描述        |
| ------- | ---------------------------------------- | ----------- |
| request | [WebResourceRequest](#webresourcerequest) | url请求的相关信息。 |
L
update  
laosan_ted 已提交
1999

Z
zhou-liting125 已提交
2000
**返回值:**
L
laosan_ted 已提交
2001

H
HelloCrease 已提交
2002 2003
| 类型                                       | 说明                                       |
| ---------------------------------------- | ---------------------------------------- |
L
laosan_ted 已提交
2004
| [WebResourceResponse](#webresourceresponse) | 返回响应数据则按照响应数据加载,无响应数据则返回null表示按照原来的方式加载。 |
L
update  
laosan_ted 已提交
2005

L
laosan_ted 已提交
2006 2007
**示例:**

Z
zhou-liting125 已提交
2008 2009
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2010
  @Entry
L
update  
laosan_ted 已提交
2011 2012
  @Component
  struct WebComponent {
Y
yamila 已提交
2013 2014 2015
    controller: WebController = new WebController()
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
L
laosan_ted 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024
    @State webdata: string = "<!DOCTYPE html>\n" +
    "<html>\n"+
    "<head>\n"+
    "<title>intercept test</title>\n"+
    "</head>\n"+
    "<body>\n"+
    "<h1>intercept test</h1>\n"+
    "</body>\n"+
    "</html>"
L
update  
laosan_ted 已提交
2025 2026
    build() {
      Column() {
2027
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2028
          .onInterceptRequest((event) => {
Y
yamila 已提交
2029
            console.log('url:' + event.request.getRequestUrl())
L
laosan_ted 已提交
2030 2031 2032 2033 2034 2035 2036 2037
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
Y
yamila 已提交
2038 2039 2040 2041 2042 2043 2044 2045 2046
            var length = this.heads.push(head1)
            length = this.heads.push(head2)
            this.responseweb.setResponseHeader(this.heads)
            this.responseweb.setResponseData(this.webdata)
            this.responseweb.setResponseEncoding('utf-8')
            this.responseweb.setResponseMimeType('text/html')
            this.responseweb.setResponseCode(200)
            this.responseweb.setReasonMessage('OK')
            return this.responseweb
L
laosan_ted 已提交
2047
          })
L
update  
laosan_ted 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
      }
    }
  }
  ```

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

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

通知收到http auth认证请求。

Z
zhou-liting125 已提交
2059
**参数:**
L
laosan_ted 已提交
2060

2061 2062 2063 2064 2065
| 参数名     | 参数类型                                 | 参数描述             |
| ------- | ------------------------------------ | ---------------- |
| handler | [HttpAuthHandler](#httpauthhandler9) | 通知Web组件用户操作行为。   |
| host    | string                               | HTTP身份验证凭据应用的主机。 |
| realm   | string                               | HTTP身份验证凭据应用的域。  |
L
update  
laosan_ted 已提交
2066

Z
zhou-liting125 已提交
2067
**返回值:**
L
laosan_ted 已提交
2068

2069 2070 2071
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 返回false表示此次认证失败,否则成功。 |
L
update  
laosan_ted 已提交
2072

L
laosan_ted 已提交
2073 2074
**示例:**

Z
zhou-liting125 已提交
2075 2076
  ```ts
  // xxx.ets
L
laosan_ted 已提交
2077
  import web_webview from '@ohos.web.webview'
Z
zhou-liting125 已提交
2078
  @Entry
L
update  
laosan_ted 已提交
2079 2080
  @Component
  struct WebComponent {
Y
yamila 已提交
2081 2082
    controller: WebController = new WebController()
    httpAuth: boolean = false
L
laosan_ted 已提交
2083
  
L
update  
laosan_ted 已提交
2084 2085
    build() {
      Column() {
2086 2087 2088
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
2089
              title: 'onHttpAuthRequest',
2090
              message: 'text',
L
laosan_ted 已提交
2091 2092 2093
              primaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2094
                  event.handler.cancel()
L
laosan_ted 已提交
2095 2096 2097 2098
                }
              },
              secondaryButton: {
                value: 'ok',
2099
                action: () => {
Y
yamila 已提交
2100
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
2101
                  if (this.httpAuth == false) {
L
laosan_ted 已提交
2102
                    web_webview.WebDataBase.saveHttpAuthCredentials(
2103 2104 2105 2106 2107
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
Y
yamila 已提交
2108
                    event.handler.cancel()
2109
                  }
L
update  
laosan_ted 已提交
2110
                }
2111 2112
              },
              cancel: () => {
Y
yamila 已提交
2113
                event.handler.cancel()
L
update  
laosan_ted 已提交
2114
              }
2115
            })
Y
yamila 已提交
2116
            return true
L
update  
laosan_ted 已提交
2117
          })
Y
yu-shihao4 已提交
2118
      }
L
update  
laosan_ted 已提交
2119 2120 2121
    }
  }
  ```
2122 2123
### onSslErrorEventReceive<sup>9+</sup>

I
i-am-a-little-bird 已提交
2124
onSslErrorEventReceive(callback: (event: { handler: SslErrorHandler, error: SslError }) => void)
2125

I
i-am-a-little-bird 已提交
2126
通知用户加载资源时发生SSL错误。
2127 2128

**参数:**
I
i-am-a-little-bird 已提交
2129

H
HelloCrease 已提交
2130 2131
| 参数名     | 参数类型                                 | 参数描述           |
| ------- | ------------------------------------ | -------------- |
I
bugfix  
i-am-a-little-bird 已提交
2132
| handler | [SslErrorHandler](#sslerrorhandler9) | 通知Web组件用户操作行为。 |
H
HelloCrease 已提交
2133
| error   | [SslError](#sslerror9枚举说明)           | 错误码。           |
I
bugfix  
i-am-a-little-bird 已提交
2134 2135

**示例:**
2136 2137 2138

  ```ts
  // xxx.ets
I
bugfix  
i-am-a-little-bird 已提交
2139
  import web_webview from '@ohos.web.webview'
2140 2141 2142
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2143
    controller: WebController = new WebController()
I
bugfix  
i-am-a-little-bird 已提交
2144
  
2145 2146 2147
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
I
bugfix  
i-am-a-little-bird 已提交
2148
          .onSslErrorEventReceive((event) => {
2149
            AlertDialog.show({
I
bugfix  
i-am-a-little-bird 已提交
2150
              title: 'onSslErrorEventReceive',
2151
              message: 'text',
I
bugfix  
i-am-a-little-bird 已提交
2152 2153
              primaryButton: {
                value: 'confirm',
2154
                action: () => {
Y
yamila 已提交
2155
                  event.handler.handleConfirm()
I
bugfix  
i-am-a-little-bird 已提交
2156 2157 2158 2159 2160
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2161
                  event.handler.handleCancel()
2162 2163 2164
                }
              },
              cancel: () => {
Y
yamila 已提交
2165
                event.handler.handleCancel()
2166 2167
              }
            })
Y
yamila 已提交
2168
            return true
2169 2170 2171 2172 2173 2174
          })
      }
    }
  }
  ```

I
i-am-a-little-bird 已提交
2175 2176 2177 2178
### onClientAuthenticationRequest<sup>9+</sup>

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

I
bugfix  
i-am-a-little-bird 已提交
2179
通知用户收到SSL客户端证书请求事件。
I
i-am-a-little-bird 已提交
2180 2181 2182

**参数:**

H
HelloCrease 已提交
2183 2184 2185 2186 2187 2188 2189
| 参数名      | 参数类型                                     | 参数描述            |
| -------- | ---------------------------------------- | --------------- |
| handler  | [ClientAuthenticationHandler](#clientauthenticationhandler9) | 通知Web组件用户操作行为。  |
| host     | string                                   | 请求证书服务器的主机名。    |
| port     | number                                   | 请求证书服务器的端口号。    |
| keyTypes | Array<string>                            | 可接受的非对称秘钥类型。    |
| issuers  | Array<string>                            | 与私钥匹配的证书可接受颁发者。 |
I
i-am-a-little-bird 已提交
2190 2191 2192 2193 2194 2195 2196 2197

  **示例:**
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2198
    controller: WebController = new WebController()
I
i-am-a-little-bird 已提交
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'onClientAuthenticationRequest',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
Y
yamila 已提交
2210
                  event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem")
I
i-am-a-little-bird 已提交
2211 2212 2213 2214 2215
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2216
                  event.handler.cancel()
I
i-am-a-little-bird 已提交
2217 2218 2219
                }
              },
              cancel: () => {
Y
yamila 已提交
2220
                event.handler.ignore()
I
i-am-a-little-bird 已提交
2221 2222
              }
            })
Y
yamila 已提交
2223
            return true
I
i-am-a-little-bird 已提交
2224 2225 2226 2227 2228 2229
          })
      }
    }
  }
  ```

2230 2231 2232 2233 2234 2235 2236
### onPermissionRequest<sup>9+</sup>

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

通知收到获取权限请求。

**参数:**
L
laosan_ted 已提交
2237

H
HelloCrease 已提交
2238 2239 2240
| 参数名     | 参数类型                                     | 参数描述           |
| ------- | ---------------------------------------- | -------------- |
| request | [PermissionRequest](#permissionrequest9) | 通知Web组件用户操作行为。 |
2241

L
laosan_ted 已提交
2242 2243
**示例:**

2244 2245 2246 2247 2248
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2249
    controller: WebController = new WebController()
2250 2251
    build() {
      Column() {
2252 2253 2254 2255 2256
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
L
laosan_ted 已提交
2257 2258 2259
              primaryButton: {
                value: 'deny',
                action: () => {
Y
yamila 已提交
2260
                  event.request.deny()
L
laosan_ted 已提交
2261 2262 2263
                }
              },
              secondaryButton: {
2264 2265
                value: 'onConfirm',
                action: () => {
Y
yamila 已提交
2266
                  event.request.grant(event.request.getAccessibleResource())
2267 2268 2269
                }
              },
              cancel: () => {
Y
yamila 已提交
2270
                event.request.deny()
2271
              }
2272
            })
2273
          })
Y
yu-shihao4 已提交
2274
      }
2275 2276 2277
    }
  }
  ```
2278

2279 2280 2281 2282
### onContextMenuShow<sup>9+</sup>

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

I
i-am-a-little-bird 已提交
2283
长按特定元素(例如图片,链接)或鼠标右键,跳出菜单。
2284 2285

**参数:**
Y
yu-shihao4 已提交
2286

H
HelloCrease 已提交
2287 2288 2289 2290
| 参数名    | 参数类型                                     | 参数描述        |
| ------ | ---------------------------------------- | ----------- |
| param  | [WebContextMenuParam](#webcontextmenuparam9) | 菜单相关参数。     |
| result | [WebContextMenuResult](#webcontextmenuresult9) | 菜单相应事件传入内核。 |
2291

Y
yu-shihao4 已提交
2292 2293
**返回值:**

H
HelloCrease 已提交
2294 2295
| 类型      | 说明                       |
| ------- | ------------------------ |
Y
yu-shihao4 已提交
2296 2297
| boolean | 自定义菜单返回true,默认菜单返回false。 |

L
laosan_ted 已提交
2298
**示例:**
Y
yu-shihao4 已提交
2299

2300 2301 2302 2303 2304
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2305
    controller: WebController = new WebController()
2306 2307
    build() {
      Column() {
2308
        Web({ src: 'www.example.com', controller: this.controller })
Y
yu-shihao4 已提交
2309
          .onContextMenuShow((event) => {
Y
yamila 已提交
2310 2311 2312
            console.info("x coord = " + event.param.x())
            console.info("link url = " + event.param.getLinkUrl())
            return true
2313 2314
        })
      }
2315 2316 2317
    }
  }
  ```
2318

L
laosan_ted 已提交
2319 2320 2321 2322 2323 2324 2325 2326
### onScroll<sup>9+</sup>

onScroll(callback: (event: {xOffset: number, yOffset: number}) => void)

通知网页滚动条滚动位置。

**参数:**

H
HelloCrease 已提交
2327 2328 2329 2330
| 参数名     | 参数类型   | 参数描述         |
| ------- | ------ | ------------ |
| xOffset | number | 水平滚动条滚动所在位置。 |
| yOffset | number | 竖直滚动条滚动所在位置。 |
L
laosan_ted 已提交
2331 2332 2333 2334 2335 2336 2337 2338

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2339
    controller: WebController = new WebController()
L
laosan_ted 已提交
2340 2341 2342 2343
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .onScroll((event) => {
Y
yamila 已提交
2344 2345
            console.info("x = " + event.xOffset)
            console.info("y = " + event.yOffset)
L
laosan_ted 已提交
2346 2347 2348 2349 2350 2351
        })
      }
    }
  }
  ```

2352 2353 2354 2355
### onGeolocationShow

onGeolocationShow(callback: (event?: { origin: string, geolocation: JsGeolocation }) => void)

2356
通知用户收到地理位置信息获取请求。
2357 2358 2359

**参数:**

H
HelloCrease 已提交
2360 2361
| 参数名         | 参数类型                            | 参数描述           |
| ----------- | ------------------------------- | -------------- |
2362
| origin      | string                          | 指定源的字符串索引。     |
H
HelloCrease 已提交
2363
| geolocation | [JsGeolocation](#jsgeolocation) | 通知Web组件用户操作行为。 |
2364 2365

**示例:**
2366

2367 2368 2369 2370 2371
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2372
    controller:WebController = new WebController()
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationShow((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
Y
yamila 已提交
2384
                event.geolocation.invoke(event.origin, true, true)
2385 2386 2387
              }
            },
            cancel: () => {
Y
yamila 已提交
2388
              event.geolocation.invoke(event.origin, false, true)
2389 2390 2391 2392 2393 2394 2395 2396
            }
          })
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2397 2398 2399 2400 2401 2402 2403 2404
### onGeolocationHide

onGeolocationHide(callback: () => void)

通知用户先前被调用[onGeolocationShow](#ongeolocationshow)时收到地理位置信息获取请求已被取消。

**参数:**

H
HelloCrease 已提交
2405 2406 2407
| 参数名      | 参数类型       | 参数描述                 |
| -------- | ---------- | -------------------- |
| callback | () => void | 地理位置信息获取请求已被取消的回调函数。 |
L
laosan_ted 已提交
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428

**示例:**

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

E
echoorchid 已提交
2429 2430 2431 2432 2433 2434 2435 2436
### onFullScreenEnter<sup>9+</sup>

onFullScreenEnter(callback: (event: { handler: FullScreenExitHandler }) => void)

通知开发者web组件进入全屏模式。

**参数:**

H
HelloCrease 已提交
2437 2438 2439
| 参数名     | 参数类型                                     | 参数描述           |
| ------- | ---------------------------------------- | -------------- |
| handler | [FullScreenExitHandler](#fullscreenexithandler9) | 用于退出全屏模式的函数句柄。 |
E
echoorchid 已提交
2440 2441 2442 2443 2444 2445 2446 2447

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2448 2449
    controller:WebController = new WebController()
    handler: FullScreenExitHandler = null
E
echoorchid 已提交
2450 2451 2452 2453
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenEnter((event) => {
Y
yamila 已提交
2454 2455
          console.log("onFullScreenEnter...")
          this.handler = event.handler
E
echoorchid 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
        })
      }
    }
  }
  ```

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

onFullScreenExit(callback: () => void)

通知开发者web组件退出全屏模式。

**参数:**

H
HelloCrease 已提交
2470 2471 2472
| 参数名      | 参数类型       | 参数描述          |
| -------- | ---------- | ------------- |
| callback | () => void | 退出全屏模式时的回调函数。 |
E
echoorchid 已提交
2473 2474 2475 2476 2477 2478 2479 2480

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2481 2482
    controller:WebController = new WebController()
    handler: FullScreenExitHandler = null
E
echoorchid 已提交
2483 2484 2485 2486
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenExit(() => {
Y
yamila 已提交
2487 2488
          console.log("onFullScreenExit...")
          this.handler.exitFullScreen()
E
echoorchid 已提交
2489 2490
        })
        .onFullScreenEnter((event) => {
Y
yamila 已提交
2491
          this.handler = event.handler
E
echoorchid 已提交
2492 2493 2494 2495 2496 2497
        })
      }
    }
  }
  ```

X
xiongjun_gitee 已提交
2498 2499 2500 2501 2502 2503 2504 2505
### onWindowNew<sup>9+</sup>

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

通知用户新建窗口请求。

**参数:**

H
HelloCrease 已提交
2506 2507 2508 2509 2510 2511
| 参数名           | 参数类型                                     | 参数描述                       |
| ------------- | ---------------------------------------- | -------------------------- |
| isAlert       | boolean                                  | true代表请求创建对话框,false代表新标签页。 |
| isUserTrigger | boolean                                  | true代表用户触发,false代表非用户触发。   |
| targetUrl     | string                                   | 目标url。                     |
| handler       | [ControllerHandler](#controllerhandler9) | 用于设置新建窗口的WebController实例。  |
X
xiongjun_gitee 已提交
2512 2513 2514 2515 2516

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
2517
  import web_webview from '@ohos.web.webview'
X
xiongjun_gitee 已提交
2518 2519 2520
  @Entry
  @Component
  struct WebComponent {
2521
    controller: web_webview.WebviewController = new web_webview.WebviewController()
X
xiongjun_gitee 已提交
2522 2523 2524 2525 2526
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
        .multiWindowAccess(true)
        .onWindowNew((event) => {
Y
yamila 已提交
2527
          console.log("onWindowNew...")
2528
          var popController: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2529
          event.handler.setWebController(popController)
X
xiongjun_gitee 已提交
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
        })
      }
    }
  }
  ```

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

onWindowExit(callback: () => void)

通知用户窗口关闭请求。

**参数:**

H
HelloCrease 已提交
2544 2545 2546
| 参数名      | 参数类型       | 参数描述         |
| -------- | ---------- | ------------ |
| callback | () => void | 窗口请求关闭的回调函数。 |
X
xiongjun_gitee 已提交
2547 2548 2549 2550 2551 2552 2553 2554

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
2555
    controller:WebController = new WebController()
X
xiongjun_gitee 已提交
2556 2557 2558
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2559
        .onWindowExit(() => {
Y
yamila 已提交
2560
          console.log("onWindowExit...")
X
xiongjun_gitee 已提交
2561 2562 2563 2564 2565 2566
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2567 2568 2569 2570 2571 2572 2573 2574
### onSearchResultReceive<sup>9+</sup>

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

回调通知调用方网页页内查找的结果。

**参数:**

H
HelloCrease 已提交
2575 2576 2577 2578 2579
| 参数名                | 参数类型    | 参数描述                                     |
| ------------------ | ------- | ---------------------------------------- |
| activeMatchOrdinal | number  | 当前匹配的查找项的序号(从0开始)。                       |
| numberOfMatches    | number  | 所有匹配到的关键词的个数。                            |
| isDoneCounting     | boolean | 当次页内查找操作是否结束。该方法可能会回调多次,直到isDoneCounting为true为止。 |
L
laosan_ted 已提交
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601

**示例:**

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

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

C
chensi10 已提交
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
### onDataResubmitted<sup>9+</sup>

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

设置网页表单可以重新提交时触发的回调函数。

**参数:**

| 参数名  | 参数类型                                             | 参数描述               |
| ------- | ---------------------------------------------------- | ---------------------- |
C
chensi10 已提交
2612
| handler | [DataResubmissionHandler](#dataresubmissionhandler9) | 表单数据重新提交句柄。 |
C
chensi10 已提交
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2626
         .onDataResubmitted((event) => {
C
chensi10 已提交
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

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

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

C
chensi10 已提交
2639
设置旧页面不再呈现,新页面即将可见时触发的回调函数。
C
chensi10 已提交
2640 2641 2642

**参数:**

C
chensi10 已提交
2643 2644 2645
| 参数名 | 参数类型 | 参数描述                                          |
| ------ | -------- | ------------------------------------------------- |
| url    | string   | 旧页面不再呈现,新页面即将可见时新页面的url地址。 |
C
chensi10 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2659
         .onPageVisible((event) => {
C
chensi10 已提交
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
          console.log('onPageVisible url:' + event.url)
        })
      }
    }
  }
  ```

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

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

设置键盘事件的回调函数,该回调在被Webview消费前触发。

**参数:**

| 参数名 | 参数类型                                                | 参数描述             |
| ------ | ------------------------------------------------------- | -------------------- |
| event  | [KeyEvent](ts-universal-events-key.md#keyevent对象说明) | 触发的KeyEvent事件。 |

C
chensi10 已提交
2679 2680 2681 2682 2683 2684
**返回值:**

| 类型    | 说明                                                         |
| ------- | ------------------------------------------------------------ |
| boolean | 回调函数通过返回boolean类型值来决定是否继续将该KeyEvent传入Webview内核。 |

C
chensi10 已提交
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2697
         .onInterceptKeyEvent((event) => {
C
chensi10 已提交
2698 2699 2700 2701 2702
          	if (event.keyCode == 2017 || event.keyCode == 2018) {
            console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`)
            return true;
          }
          return false;
C
chensi10 已提交
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
        })
      }
    }
  }
  ```

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

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

设置接收到apple-touch-icon url地址时的回调函数。

**参数:**

| 参数名      | 参数类型 | 参数描述                           |
| ----------- | -------- | ---------------------------------- |
| url         | string   | 接收到的apple-touch-icon url地址。 |
| precomposed | boolean  | 对应apple-touch-icon是否为预合成。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
C
chensi10 已提交
2733 2734
        Web({ src:'www.baidu.com', controller: this.controller })
         .onTouchIconUrlReceived((event) => {
C
chensi10 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
          console.log('onTouchIconUrlReceived:' + JSON.stringify(event))
        })
      }
    }
  }
  ```

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

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

设置应用为当前页面接收到新的favicon时的回调函数。

**参数:**

| 参数名  | 参数类型                                       | 参数描述                            |
| ------- | ---------------------------------------------- | ----------------------------------- |
| favicon | [PixelMap](../apis/js-apis-image.md#pixelmap7) | 接收到的favicon图标的PixelMap对象。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  import image from "@ohos.multimedia.image"
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State icon: image.PixelMap = undefined;
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2768 2769
         .onFaviconReceived((event) => {
          console.log('onFaviconReceived:' + JSON.stringify(event))
C
chensi10 已提交
2770 2771 2772 2773 2774 2775 2776
          this.icon = event.favicon;
        })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
2777
## ConsoleMessage
2778

L
update  
laosan_ted 已提交
2779
Web组件获取控制台信息对象。示例代码参考[onConsole事件](#onconsole)
2780

Z
zhou-liting125 已提交
2781
### getLineNumber
2782

Z
zhou-liting125 已提交
2783 2784 2785 2786
getLineNumber(): number

获取ConsoleMessage的行数。

Z
zhou-liting125 已提交
2787
**返回值:** 
L
laosan_ted 已提交
2788

2789 2790 2791
| 类型     | 说明                   |
| ------ | -------------------- |
| number | 返回ConsoleMessage的行数。 |
Z
zhou-liting125 已提交
2792 2793 2794 2795 2796 2797 2798

### getMessage

getMessage(): string

获取ConsoleMessage的日志信息。

Z
zhou-liting125 已提交
2799
**返回值:** 
L
laosan_ted 已提交
2800

2801 2802 2803
| 类型     | 说明                     |
| ------ | ---------------------- |
| string | 返回ConsoleMessage的日志信息。 |
Z
zhou-liting125 已提交
2804 2805 2806 2807

### getMessageLevel

getMessageLevel(): MessageLevel
2808

Z
zhou-liting125 已提交
2809 2810
获取ConsoleMessage的信息级别。

Z
zhou-liting125 已提交
2811
**返回值:** 
L
laosan_ted 已提交
2812

2813 2814 2815
| 类型                                | 说明                     |
| --------------------------------- | ---------------------- |
| [MessageLevel](#messagelevel枚举说明) | 返回ConsoleMessage的信息级别。 |
Z
zhou-liting125 已提交
2816 2817 2818 2819 2820 2821 2822

### getSourceId

getSourceId(): string

获取网页源文件路径和名字。

Z
zhou-liting125 已提交
2823
**返回值:** 
L
laosan_ted 已提交
2824

2825 2826 2827
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回网页源文件路径和名字。 |
Z
zhou-liting125 已提交
2828 2829

## JsResult
L
lixingchi1 已提交
2830

L
update  
laosan_ted 已提交
2831
Web组件返回的弹窗确认或弹窗取消功能对象。示例代码参考[onAlert事件](#onalert)
L
add web  
liujinwei 已提交
2832

Z
zhou-liting125 已提交
2833 2834 2835
### handleCancel

handleCancel(): void
L
add web  
liujinwei 已提交
2836

Z
zhou-liting125 已提交
2837
通知Web组件用户取消弹窗操作。
L
add web  
liujinwei 已提交
2838

Z
zhou-liting125 已提交
2839
### handleConfirm
L
add web  
liujinwei 已提交
2840

Z
zhou-liting125 已提交
2841
handleConfirm(): void
L
add web  
liujinwei 已提交
2842

Z
zhou-liting125 已提交
2843
通知Web组件用户确认弹窗操作。
L
add web  
liujinwei 已提交
2844

Z
zhou-liting125 已提交
2845 2846 2847 2848 2849 2850
### handlePromptConfirm<sup>9+</sup>

handlePromptConfirm(result: string): void

通知Web组件用户确认弹窗操作及对话框内容。

Z
zhou-liting125 已提交
2851
**参数:**
L
laosan_ted 已提交
2852

2853 2854 2855
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述        |
| ------ | ------ | ---- | ---- | ----------- |
| result | string | 是    | -    | 用户输入的对话框内容。 |
Z
zhou-liting125 已提交
2856

E
echoorchid 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
## FullScreenExitHandler<sup>9+</sup>

通知开发者Web组件退出全屏。示例代码参考[onFullScreenEnter事件](#onfullscreenenter9)

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

exitFullScreen(): void

通知开发者Web组件退出全屏。

X
xiongjun_gitee 已提交
2867 2868
## ControllerHandler<sup>9+</sup>

2869
设置用户新建web组件的的WebviewController对象。示例代码参考[onWindowNew事件](#onwindownew9)
X
xiongjun_gitee 已提交
2870 2871 2872

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

2873
setWebController(controller: WebviewController): void
X
xiongjun_gitee 已提交
2874

2875
设置WebviewController对象。
X
xiongjun_gitee 已提交
2876 2877 2878

**参数:**

H
HelloCrease 已提交
2879 2880
| 参数名        | 参数类型          | 必填   | 默认值  | 参数描述                      |
| ---------- | ------------- | ---- | ---- | ------------------------- |
2881
| controller | [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | 是    | -    | 新建web组件的的WebviewController对象。 |
X
xiongjun_gitee 已提交
2882

Z
zhou-liting125 已提交
2883 2884
## WebResourceError

L
update  
laosan_ted 已提交
2885
web组件资源管理错误信息对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
2886 2887 2888 2889 2890 2891 2892

### getErrorCode

getErrorCode(): number

获取加载资源的错误码。

Z
zhou-liting125 已提交
2893
**返回值:** 
L
laosan_ted 已提交
2894

2895 2896 2897
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回加载资源的错误码。 |
Z
zhou-liting125 已提交
2898 2899 2900 2901 2902 2903 2904

### getErrorInfo

getErrorInfo(): string

获取加载资源的错误信息。

Z
zhou-liting125 已提交
2905
**返回值:** 
L
laosan_ted 已提交
2906

2907 2908 2909
| 类型     | 说明           |
| ------ | ------------ |
| string | 返回加载资源的错误信息。 |
Z
zhou-liting125 已提交
2910 2911 2912

## WebResourceRequest

L
update  
laosan_ted 已提交
2913
web组件获取资源请求对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
2914 2915 2916 2917 2918 2919 2920

### getRequestHeader

getResponseHeader() : Array\<Header\>

获取资源请求头信息。

Z
zhou-liting125 已提交
2921
**返回值:** 
L
laosan_ted 已提交
2922

2923 2924 2925
| 类型                         | 说明         |
| -------------------------- | ---------- |
| Array\<[Header](#header)\> | 返回资源请求头信息。 |
Z
zhou-liting125 已提交
2926 2927 2928 2929 2930 2931 2932

### getRequestUrl

getRequestUrl(): string

获取资源请求的URL信息。

Z
zhou-liting125 已提交
2933
**返回值:** 
L
laosan_ted 已提交
2934

2935 2936 2937
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源请求的URL信息。 |
Z
zhou-liting125 已提交
2938 2939 2940 2941 2942 2943 2944

### isMainFrame

isMainFrame(): boolean

判断资源请求是否为主frame。

Z
zhou-liting125 已提交
2945
**返回值:** 
L
laosan_ted 已提交
2946

2947 2948 2949
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否为主frame。 |
Z
zhou-liting125 已提交
2950 2951 2952 2953 2954 2955 2956

### isRedirect

isRedirect(): boolean

判断资源请求是否被服务端重定向。

Z
zhou-liting125 已提交
2957
**返回值:** 
L
laosan_ted 已提交
2958

2959 2960 2961
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否被服务端重定向。 |
Z
zhou-liting125 已提交
2962 2963 2964 2965 2966 2967 2968

### isRequestGesture

isRequestGesture(): boolean

获取资源请求是否与手势(如点击)相关联。

Z
zhou-liting125 已提交
2969
**返回值:** 
L
laosan_ted 已提交
2970

2971 2972 2973
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 返回资源请求是否与手势(如点击)相关联。 |
Z
zhou-liting125 已提交
2974 2975

## Header
L
liwenzhen 已提交
2976 2977 2978

Web组件返回的请求/响应头对象。

2979 2980 2981 2982
| 名称          | 类型     | 描述            |
| ----------- | ------ | ------------- |
| headerKey   | string | 请求/响应头的key。   |
| headerValue | string | 请求/响应头的value。 |
L
liwenzhen 已提交
2983

L
add web  
liujinwei 已提交
2984

Z
zhou-liting125 已提交
2985
## WebResourceResponse
L
add web  
liujinwei 已提交
2986

Z
zhou-liting125 已提交
2987
web组件资源响应对象。示例代码参考[onHttpErrorReceive事件](#onhttperrorreceive)
L
add web  
liujinwei 已提交
2988

Z
zhou-liting125 已提交
2989
### getReasonMessage
T
Ted 已提交
2990

Z
zhou-liting125 已提交
2991
getReasonMessage(): string
T
Ted 已提交
2992

Z
zhou-liting125 已提交
2993
获取资源响应的状态码描述。
T
Ted 已提交
2994

Z
zhou-liting125 已提交
2995
**返回值:** 
L
laosan_ted 已提交
2996

2997 2998 2999
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源响应的状态码描述。 |
T
Ted 已提交
3000

Z
zhou-liting125 已提交
3001
### getResponseCode
T
Ted 已提交
3002

Z
zhou-liting125 已提交
3003 3004 3005 3006
getResponseCode(): number

获取资源响应的状态码。

Z
zhou-liting125 已提交
3007
**返回值:** 
L
laosan_ted 已提交
3008

3009 3010 3011
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回资源响应的状态码。 |
Z
zhou-liting125 已提交
3012 3013 3014 3015 3016 3017 3018

### getResponseData

getResponseData(): string

获取资源响应数据。

Z
zhou-liting125 已提交
3019
**返回值:** 
L
laosan_ted 已提交
3020

3021 3022 3023
| 类型     | 说明        |
| ------ | --------- |
| string | 返回资源响应数据。 |
Z
zhou-liting125 已提交
3024 3025 3026 3027 3028 3029 3030

### getResponseEncoding

getResponseEncoding(): string

获取资源响应的编码。

Z
zhou-liting125 已提交
3031
**返回值:** 
L
laosan_ted 已提交
3032

3033 3034 3035
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回资源响应的编码。 |
Z
zhou-liting125 已提交
3036 3037 3038 3039 3040 3041 3042

### getResponseHeader

getResponseHeader() : Array\<Header\>

获取资源响应头。

Z
zhou-liting125 已提交
3043
**返回值:** 
L
laosan_ted 已提交
3044

3045 3046 3047
| 类型                         | 说明       |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | 返回资源响应头。 |
Z
zhou-liting125 已提交
3048 3049 3050 3051 3052 3053 3054

### getResponseMimeType

getResponseMimeType(): string

获取资源响应的媒体(MIME)类型。

Z
zhou-liting125 已提交
3055
**返回值:** 
L
laosan_ted 已提交
3056

3057 3058 3059
| 类型     | 说明                 |
| ------ | ------------------ |
| string | 返回资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3060 3061 3062

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

3063
setResponseData(data: string | number)
Z
zhou-liting125 已提交
3064 3065 3066

设置资源响应数据。

Z
zhou-liting125 已提交
3067
**参数:**
L
laosan_ted 已提交
3068

3069 3070 3071
| 参数名 | 参数类型         | 必填 | 默认值 | 参数描述                                                     |
| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ |
| data   | string \| number | 是   | -      | 要设置的资源响应数据。string表示输入类型是字符串,number表示输入类型是文件句柄。 |
Z
zhou-liting125 已提交
3072 3073 3074 3075 3076 3077 3078

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

setResponseEncoding(encoding: string)

设置资源响应的编码。

Z
zhou-liting125 已提交
3079
**参数:**
L
laosan_ted 已提交
3080

3081 3082 3083
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述         |
| -------- | ------ | ---- | ---- | ------------ |
| encoding | string | 是    | -    | 要设置的资源响应的编码。 |
Z
zhou-liting125 已提交
3084 3085 3086 3087 3088 3089 3090

### setResponseMimeType<sup>9+</sup>

setResponseMimeType(mimeType: string)

设置资源响应的媒体(MIME)类型。

Z
zhou-liting125 已提交
3091
**参数:**
L
laosan_ted 已提交
3092

3093 3094 3095
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述                 |
| -------- | ------ | ---- | ---- | -------------------- |
| mimeType | string | 是    | -    | 要设置的资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3096 3097 3098 3099 3100 3101 3102

### setReasonMessage<sup>9+</sup>

setReasonMessage(reason: string)

设置资源响应的状态码描述。

Z
zhou-liting125 已提交
3103
**参数:**
L
laosan_ted 已提交
3104

3105 3106 3107
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------ | ------ | ---- | ---- | --------------- |
| reason | string | 是    | -    | 要设置的资源响应的状态码描述。 |
Z
zhou-liting125 已提交
3108 3109 3110 3111 3112 3113 3114

### setResponseHeader<sup>9+</sup>

setResponseHeader(header: Array\<Header\>)

设置资源响应头。

Z
zhou-liting125 已提交
3115
**参数:**
L
laosan_ted 已提交
3116

3117 3118 3119
| 参数名    | 参数类型                       | 必填   | 默认值  | 参数描述       |
| ------ | -------------------------- | ---- | ---- | ---------- |
| header | Array\<[Header](#header)\> | 是    | -    | 要设置的资源响应头。 |
Z
zhou-liting125 已提交
3120 3121 3122 3123 3124 3125 3126

### setResponseCode<sup>9+</sup>

setResponseCode(code: number)

设置资源响应的状态码。

Z
zhou-liting125 已提交
3127
**参数:**
L
laosan_ted 已提交
3128

3129 3130 3131
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述          |
| ---- | ------ | ---- | ---- | ------------- |
| code | number | 是    | -    | 要设置的资源响应的状态码。 |
Z
zhou-liting125 已提交
3132

3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
### setResponseIsReady<sup>9+</sup>

setResponseIsReady(IsReady: boolean)

设置资源响应数据是否已经就绪。

**参数:**

| 参数名  | 参数类型 | 必填 | 默认值 | 参数描述                   |
| ------- | -------- | ---- | ------ | -------------------------- |
| IsReady | boolean  | 是   | true   | 资源响应数据是否已经就绪。 |

Z
zhou-liting125 已提交
3145
## FileSelectorResult<sup>9+</sup>
T
Ted 已提交
3146

Z
zhou-liting125 已提交
3147
通知Web组件的文件选择结果。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
T
Ted 已提交
3148

Z
zhou-liting125 已提交
3149
### handleFileList<sup>9+</sup>
3150

Z
zhou-liting125 已提交
3151
handleFileList(fileList: Array\<string\>): void
3152

Z
zhou-liting125 已提交
3153
通知Web组件进行文件选择操作。
T
Ted 已提交
3154

Z
zhou-liting125 已提交
3155
**参数:**
L
laosan_ted 已提交
3156

3157 3158 3159
| 参数名      | 参数类型            | 必填   | 默认值  | 参数描述         |
| -------- | --------------- | ---- | ---- | ------------ |
| fileList | Array\<string\> | 是    | -    | 需要进行操作的文件列表。 |
3160

Z
zhou-liting125 已提交
3161 3162
## FileSelectorParam<sup>9+</sup>

Z
zhou-liting125 已提交
3163
web组件获取文件对象。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
Z
zhou-liting125 已提交
3164 3165 3166 3167 3168 3169 3170

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

getTitle(): string

获取文件选择器标题。

Z
zhou-liting125 已提交
3171
**返回值:** 
L
laosan_ted 已提交
3172

3173 3174 3175
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回文件选择器标题。 |
Z
zhou-liting125 已提交
3176 3177 3178 3179 3180 3181 3182

### getMode<sup>9+</sup>

getMode(): FileSelectorMode

获取文件选择器的模式。

Z
zhou-liting125 已提交
3183
**返回值:** 
L
laosan_ted 已提交
3184

3185 3186 3187
| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode枚举说明) | 返回文件选择器的模式。 |
Z
zhou-liting125 已提交
3188 3189 3190 3191 3192 3193 3194

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

getAcceptType(): Array\<string\>

获取文件过滤类型。

Z
zhou-liting125 已提交
3195
**返回值:** 
L
laosan_ted 已提交
3196

3197 3198 3199
| 类型              | 说明        |
| --------------- | --------- |
| Array\<string\> | 返回文件过滤类型。 |
Z
zhou-liting125 已提交
3200 3201 3202 3203 3204 3205 3206

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

isCapture(): boolean

获取是否调用多媒体能力。

Z
zhou-liting125 已提交
3207
**返回值:** 
L
laosan_ted 已提交
3208

3209 3210 3211
| 类型      | 说明           |
| ------- | ------------ |
| boolean | 返回是否调用多媒体能力。 |
3212 3213 3214

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

Z
zhou-liting125 已提交
3215
Web组件返回的http auth认证请求确认或取消和使用缓存密码认证功能对象。示例代码参考[onHttpAuthRequest事件](#onhttpauthrequest9)
3216 3217 3218 3219 3220 3221 3222 3223

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

cancel(): void

通知Web组件用户取消HTTP认证操作。

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

3225 3226 3227 3228
confirm(userName: string, pwd: string): boolean

使用用户名和密码进行HTTP认证操作。

Z
zhou-liting125 已提交
3229
**参数:**
3230

3231 3232 3233 3234
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述       |
| -------- | ------ | ---- | ---- | ---------- |
| userName | string | 是    | -    | HTTP认证用户名。 |
| pwd      | string | 是    | -    | HTTP认证密码。  |
3235

Z
zhou-liting125 已提交
3236
**返回值:**
L
laosan_ted 已提交
3237

3238 3239 3240
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 认证成功返回true,失败返回false。 |
3241 3242 3243 3244 3245 3246 3247

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

isHttpAuthInfoSaved(): boolean

通知Web组件用户使用服务器缓存的账号密码认证。

Z
zhou-liting125 已提交
3248
**返回值:**
L
laosan_ted 已提交
3249

3250 3251 3252
| 类型      | 说明                        |
| ------- | ------------------------- |
| boolean | 存在密码认证成功返回true,其他返回false。 |
3253

3254 3255
## SslErrorHandler<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3256
Web组件返回的SSL错误通知事件用户处理功能对象。示例代码参考[onSslErrorEventReceive事件](#onsslerroreventreceive9)
3257 3258 3259 3260 3261

### handleCancel<sup>9+</sup>

handleCancel(): void

I
i-am-a-little-bird 已提交
3262
通知Web组件取消此请求。
3263 3264 3265 3266 3267

### handleConfirm<sup>9+</sup>

handleConfirm(): void

I
i-am-a-little-bird 已提交
3268 3269 3270 3271
通知Web组件继续使用SSL证书。

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

I
bugfix  
i-am-a-little-bird 已提交
3272
Web组件返回的SSL客户端证书请求事件用户处理功能对象。示例代码参考[onClientAuthenticationRequest事件](#onclientauthenticationrequest9)
I
i-am-a-little-bird 已提交
3273 3274 3275 3276 3277 3278

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

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

通知Web组件使用指定的私钥和客户端证书链。
I
bugfix  
i-am-a-little-bird 已提交
3279

I
i-am-a-little-bird 已提交
3280 3281
**参数:**

H
HelloCrease 已提交
3282 3283 3284 3285
| 参数名           | 参数类型   | 必填   | 参数描述               |
| ------------- | ------ | ---- | ------------------ |
| priKeyFile    | string | 是    | 存放私钥的文件,包含路径和文件名。  |
| certChainFile | string | 是    | 存放证书链的文件,包含路径和文件名。 |
I
i-am-a-little-bird 已提交
3286 3287

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

I
i-am-a-little-bird 已提交
3289 3290
cancel(): void

I
bugfix  
i-am-a-little-bird 已提交
3291
通知Web组件取消相同host和port服务器发送的客户端证书请求事件。同时,相同host和port服务器的请求,不重复上报该事件。
I
i-am-a-little-bird 已提交
3292 3293 3294 3295 3296 3297

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

ignore(): void

通知Web组件忽略本次请求。
3298

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

X
xiongjun_gitee 已提交
3301
Web组件返回授权或拒绝权限功能的对象。示例代码参考[onPermissionRequest事件](#onpermissionrequest9)
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316

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

deny(): void

拒绝网页所请求的权限。

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

getOrigin(): string

获取网页来源。

**返回值:**

H
HelloCrease 已提交
3317 3318 3319
| 类型     | 说明           |
| ------ | ------------ |
| string | 当前请求权限网页的来源。 |
3320 3321 3322 3323 3324

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

getAccessibleResource(): Array\<string\>

X
xiongjun_gitee 已提交
3325
获取网页所请求的权限资源列表,资源列表类型参考[ProtectedResourceType](#protectedresourcetype9枚举说明)
3326 3327 3328

**返回值:**

H
HelloCrease 已提交
3329 3330
| 类型              | 说明            |
| --------------- | ------------- |
3331 3332 3333 3334 3335 3336
| Array\<string\> | 网页所请求的权限资源列表。 |

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

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

X
xiongjun_gitee 已提交
3337
对网页访问的给定权限进行授权。
3338 3339 3340

**参数:**

H
HelloCrease 已提交
3341 3342
| 参数名       | 参数类型            | 必填   | 默认值  | 参数描述          |
| --------- | --------------- | ---- | ---- | ------------- |
L
laosan_ted 已提交
3343
| resources | Array\<string\> | 是    | -    | 授予网页请求的权限的资源列表。 |
3344

I
i-am-a-little-bird 已提交
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
## ContextMenuSourceType<sup>9+</sup>枚举说明
| 名称                   | 描述         |
| -------------------- | ---------- |
| None        | 其他事件来源。  |
| Mouse       | 鼠标事件。  |
| LongPress   | 长按事件。  |

## ContextMenuMediaType<sup>9+</sup>枚举说明

| 名称           | 描述          |
| ------------ | ----------- |
| None      | 非特殊媒体或其他媒体类型。 |
| Image     | 图片。     |

## ContextMenuInputFieldType<sup>9+</sup>枚举说明

| 名称           | 描述          |
| ------------ | ----------- |
| None      | 非输入框。       |
| PlainText | 纯文本类型,包括text、search、email等。   |
| Password  | 密码类型。     |
| Number    | 数字类型。     |
| Telephone | 电话号码类型。 |
| Other     | 其他类型。     |

## ContextMenuEditStateFlags<sup>9+</sup>枚举说明

| 名称         | 描述         |
| ------------ | ----------- |
| NONE         | 不可编辑。   |
| CAN_CUT      | 支持剪切。   |
| CAN_COPY     | 支持拷贝。   |
| CAN_PASTE    | 支持粘贴。   |
| CAN_SELECT_ALL  | 支持全选。 |

3380 3381
## WebContextMenuParam<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3382
实现长按页面元素或鼠标右键弹出来的菜单信息。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
3383 3384 3385 3386 3387 3388 3389 3390 3391

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

x(): number

弹出菜单的x坐标。

**返回值:**

H
HelloCrease 已提交
3392 3393
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3394
| number | 显示正常返回非负整数,否则返回-1。 |
3395 3396 3397 3398 3399 3400 3401 3402 3403

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

y(): number

弹出菜单的y坐标。

**返回值:**

H
HelloCrease 已提交
3404 3405
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3406
| number | 显示正常返回非负整数,否则返回-1。 |
3407 3408 3409 3410 3411

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

getLinkUrl(): string

Y
yu-shihao4 已提交
3412
获取链接地址。
3413 3414 3415

**返回值:**

H
HelloCrease 已提交
3416 3417
| 类型     | 说明                        |
| ------ | ------------------------- |
Y
yu-shihao4 已提交
3418
| string | 如果长按位置是链接,返回经过安全检查的url链接。 |
3419

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

3422
getUnfilteredLinkUrl(): string
3423

Y
yu-shihao4 已提交
3424
获取链接地址。
3425 3426 3427

**返回值:**

H
HelloCrease 已提交
3428 3429
| 类型     | 说明                    |
| ------ | --------------------- |
Y
yu-shihao4 已提交
3430
| string | 如果长按位置是链接,返回原始的url链接。 |
3431 3432 3433 3434 3435 3436 3437 3438 3439

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

getSourceUrl(): string

获取sourceUrl链接。

**返回值:**

H
HelloCrease 已提交
3440 3441
| 类型     | 说明                       |
| ------ | ------------------------ |
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
| string | 如果选中的元素有src属性,返回src的url。 |

### existsImageContents<sup>9+</sup>

existsImageContents(): boolean

是否存在图像内容。

**返回值:**

H
HelloCrease 已提交
3452 3453
| 类型      | 说明                        |
| ------- | ------------------------- |
3454 3455
| boolean | 长按位置中有图片返回true,否则返回false。 |

I
i-am-a-little-bird 已提交
3456 3457
### getMediaType<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3458
getMediaType(): ContextMenuMediaType
I
i-am-a-little-bird 已提交
3459 3460 3461 3462 3463 3464 3465

获取网页元素媒体类型。

**返回值:**

| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
I
bugfix  
i-am-a-little-bird 已提交
3466
| [ContextMenuMediaType](#contextmenumediatype9枚举说明) | 网页元素媒体类型。 |
I
i-am-a-little-bird 已提交
3467 3468 3469

### getSelectionText<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3470
getSelectionText(): string
I
i-am-a-little-bird 已提交
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481

获取选中文本。

**返回值:**

| 类型      | 说明                        |
| ------- | ------------------------- |
| string | 菜单上下文选中文本内容,不存在则返回空。 |

### getSourceType<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3482
getSourceType(): ContextMenuSourceType
I
i-am-a-little-bird 已提交
3483 3484 3485 3486 3487 3488 3489

获取菜单事件来源。

**返回值:**

| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
I
bugfix  
i-am-a-little-bird 已提交
3490
| [ContextMenuSourceType](#contextmenusourcetype9枚举说明) | 菜单事件来源。 |
I
i-am-a-little-bird 已提交
3491 3492 3493

### getInputFieldType<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3494
getInputFieldType(): ContextMenuInputFieldType
I
i-am-a-little-bird 已提交
3495 3496 3497 3498 3499 3500 3501

获取网页元素输入框类型。

**返回值:**

| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
I
bugfix  
i-am-a-little-bird 已提交
3502
| [ContextMenuInputFieldType](#contextmenuinputfieldtype9枚举说明) | 输入框类型。 |
I
i-am-a-little-bird 已提交
3503 3504 3505

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

I
bugfix  
i-am-a-little-bird 已提交
3506
isEditable(): boolean
I
i-am-a-little-bird 已提交
3507 3508 3509 3510 3511 3512 3513

获取网页元素是否可编辑标识。

**返回值:**

| 类型      | 说明                        |
| ------- | ------------------------- |
I
bugfix  
i-am-a-little-bird 已提交
3514
| boolean | 网页元素可编辑返回true,不可编辑返回false。 |
I
i-am-a-little-bird 已提交
3515 3516 3517

### getEditStateFlags<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3518
getEditStateFlags(): number
I
i-am-a-little-bird 已提交
3519 3520 3521 3522 3523 3524 3525

获取网页元素可编辑标识。

**返回值:**

| 类型      | 说明                        |
| ------- | ------------------------- |
I
bugfix  
i-am-a-little-bird 已提交
3526
| number | 网页元素可编辑标识,参照[ContextMenuEditStateFlags](#contextmenueditstateflags9枚举说明)。 |
I
i-am-a-little-bird 已提交
3527

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

I
bugfix  
i-am-a-little-bird 已提交
3530
实现长按页面元素或鼠标右键弹出来的菜单所执行的响应事件。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543

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

closeContextMenu(): void

不执行WebContextMenuResult其他接口操作时,需要调用此接口关闭菜单。

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

copyImage(): void

WebContextMenuParam有图片内容则复制图片。

I
i-am-a-little-bird 已提交
3544 3545
### copy<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3546
copy(): void
I
i-am-a-little-bird 已提交
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567

执行与此上下文菜单相关的拷贝操作。

### paste<sup>9+</sup>

paste(): void

执行与此上下文菜单相关的粘贴操作。

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

cut(): void

执行与此上下文菜单相关的剪切操作。

### selectAll<sup>9+</sup>

selectAll(): void

执行与此上下文菜单相关的全选操作。

3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
## JsGeolocation

Web组件返回授权或拒绝权限功能的对象。示例代码参考[onGeolocationShow事件](#ongeolocationshow)

### invoke

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

设置网页地理位置权限状态。

**参数:**

H
HelloCrease 已提交
3580 3581 3582 3583
| 参数名    | 参数类型    | 必填   | 默认值  | 参数描述                                     |
| ------ | ------- | ---- | ---- | ---------------------------------------- |
| origin | string  | 是    | -    | 指定源的字符串索引。                               |
| allow  | boolean | 是    | -    | 设置的地理位置权限状态。                             |
L
laosan_ted 已提交
3584
| retain | boolean | 是    | -    | 是否允许将地理位置权限状态保存到系统中。可通过[GeolocationPermissions<sup>9+</sup>](../apis/js-apis-webview.md#geolocationpermissions)接口管理保存到系统的地理位置权限。 |
3585

Z
zengyawen 已提交
3586 3587
## WebController

L
liwenzhen 已提交
3588
通过WebController可以控制Web组件各种行为。一个WebController对象只能控制一个Web组件,且必须在Web组件和WebController绑定后,才能调用WebController上的方法。
Z
zengyawen 已提交
3589

L
laosan_ted 已提交
3590 3591
从API version 9开始不再维护,建议使用[WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller)代替。

Z
zengyawen 已提交
3592 3593 3594 3595 3596 3597
### 创建对象

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

L
laosan_ted 已提交
3598
### requestFocus<sup>(deprecated)</sup>
L
laosan_ted 已提交
3599 3600 3601 3602 3603

requestFocus()

使当前web页面获取焦点。

L
laosan_ted 已提交
3604 3605
从API version 9开始不再维护,建议使用[requestFocus<sup>9+</sup>](../apis/js-apis-webview.md#requestfocus)代替。

L
laosan_ted 已提交
3606
**示例:**
L
laosan_ted 已提交
3607

L
laosan_ted 已提交
3608 3609 3610 3611 3612
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
3613
    controller: WebController = new WebController()
L
laosan_ted 已提交
3614 3615 3616 3617 3618
  
    build() {
      Column() {
        Button('requestFocus')
          .onClick(() => {
Y
yamila 已提交
3619
            this.controller.requestFocus()
L
laosan_ted 已提交
3620 3621 3622 3623 3624 3625 3626
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

L
laosan_ted 已提交
3627
### accessBackward<sup>(deprecated)</sup>
L
lixingchi1 已提交
3628 3629 3630

accessBackward(): boolean

L
liwenzhen 已提交
3631
当前页面是否可后退,即当前页面是否有返回历史记录。
L
lixingchi1 已提交
3632

L
laosan_ted 已提交
3633 3634
从API version 9开始不再维护,建议使用[accessBackward<sup>9+</sup>](../apis/js-apis-webview.md#accessbackward)代替。

Z
zhou-liting125 已提交
3635
**返回值:**
L
laosan_ted 已提交
3636

3637 3638 3639
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以后退返回true,否则返回false。 |
L
update  
laosan_ted 已提交
3640

Z
zhou-liting125 已提交
3641
**示例:**
L
laosan_ted 已提交
3642

Z
zhou-liting125 已提交
3643 3644
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3645
  @Entry
L
update  
laosan_ted 已提交
3646 3647
  @Component
  struct WebComponent {
Y
yamila 已提交
3648
    controller: WebController = new WebController()
L
laosan_ted 已提交
3649
  
L
update  
laosan_ted 已提交
3650 3651 3652
    build() {
      Column() {
        Button('accessBackward')
L
laosan_ted 已提交
3653
          .onClick(() => {
Y
yamila 已提交
3654 3655
            let result = this.controller.accessBackward()
            console.log('result:' + result)
L
laosan_ted 已提交
3656
          })
Z
zhou-liting125 已提交
3657
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3658
      }
L
update  
laosan_ted 已提交
3659 3660 3661 3662
    }
  }
  ```

L
laosan_ted 已提交
3663
### accessForward<sup>(deprecated)</sup>
L
lixingchi1 已提交
3664 3665 3666

accessForward(): boolean

L
liwenzhen 已提交
3667
当前页面是否可前进,即当前页面是否有前进历史记录。
L
lixingchi1 已提交
3668

L
laosan_ted 已提交
3669 3670
从API version 9开始不再维护,建议使用[accessForward<sup>9+</sup>](../apis/js-apis-webview.md#accessforward)代替。

Z
zhou-liting125 已提交
3671
**返回值:**
L
laosan_ted 已提交
3672

3673 3674 3675
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以前进返回true,否则返回false。 |
L
update  
laosan_ted 已提交
3676

Z
zhou-liting125 已提交
3677
**示例:**
L
laosan_ted 已提交
3678

Z
zhou-liting125 已提交
3679 3680
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3681
  @Entry
L
update  
laosan_ted 已提交
3682 3683
  @Component
  struct WebComponent {
Y
yamila 已提交
3684
    controller: WebController = new WebController()
L
laosan_ted 已提交
3685
  
L
update  
laosan_ted 已提交
3686 3687 3688
    build() {
      Column() {
        Button('accessForward')
L
laosan_ted 已提交
3689
          .onClick(() => {
Y
yamila 已提交
3690 3691
            let result = this.controller.accessForward()
            console.log('result:' + result)
L
laosan_ted 已提交
3692
          })
Z
zhou-liting125 已提交
3693
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3694
      }
L
update  
laosan_ted 已提交
3695 3696 3697 3698
    }
  }
  ```

L
laosan_ted 已提交
3699
### accessStep<sup>(deprecated)</sup>
Z
zengyawen 已提交
3700

L
lixingchi1 已提交
3701
accessStep(step: number): boolean
Z
zengyawen 已提交
3702

L
liwenzhen 已提交
3703
当前页面是否可前进或者后退给定的step步。
Z
zengyawen 已提交
3704

L
laosan_ted 已提交
3705 3706
从API version 9开始不再维护,建议使用[accessStep<sup>9+</sup>](../apis/js-apis-webview.md#accessstep)代替。

Z
zhou-liting125 已提交
3707
**参数:**
Z
zengyawen 已提交
3708

3709 3710 3711
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                  |
| ---- | ------ | ---- | ---- | --------------------- |
| step | number | 是    | -    | 要跳转的步数,正数代表前进,负数代表后退。 |
L
lixingchi1 已提交
3712

Z
zhou-liting125 已提交
3713
**返回值:**
L
laosan_ted 已提交
3714

3715 3716 3717
| 类型      | 说明        |
| ------- | --------- |
| boolean | 页面是否前进或后退 |
3718

Z
zhou-liting125 已提交
3719
**示例:**
L
laosan_ted 已提交
3720

Z
zhou-liting125 已提交
3721 3722
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3723
  @Entry
L
update  
laosan_ted 已提交
3724 3725
  @Component
  struct WebComponent {
Y
yamila 已提交
3726 3727
    controller: WebController = new WebController()
    @State steps: number = 2
L
laosan_ted 已提交
3728
  
L
update  
laosan_ted 已提交
3729 3730 3731
    build() {
      Column() {
        Button('accessStep')
L
laosan_ted 已提交
3732
          .onClick(() => {
Y
yamila 已提交
3733 3734
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
L
laosan_ted 已提交
3735
          })
Z
zhou-liting125 已提交
3736
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3737
      }
L
update  
laosan_ted 已提交
3738 3739 3740 3741
    }
  }
  ```

L
laosan_ted 已提交
3742
### backward<sup>(deprecated)</sup>
L
lixingchi1 已提交
3743

L
update  
laosan_ted 已提交
3744
backward(): void
L
lixingchi1 已提交
3745

L
liwenzhen 已提交
3746
按照历史栈,后退一个页面。一般结合accessBackward一起使用。
L
lixingchi1 已提交
3747

L
laosan_ted 已提交
3748 3749
从API version 9开始不再维护,建议使用[backward<sup>9+</sup>](../apis/js-apis-webview.md#backward)代替。

Z
zhou-liting125 已提交
3750
**示例:**
L
laosan_ted 已提交
3751

Z
zhou-liting125 已提交
3752 3753
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3754
  @Entry
L
update  
laosan_ted 已提交
3755 3756
  @Component
  struct WebComponent {
Y
yamila 已提交
3757
    controller: WebController = new WebController()
L
laosan_ted 已提交
3758
  
L
update  
laosan_ted 已提交
3759 3760 3761
    build() {
      Column() {
        Button('backward')
L
laosan_ted 已提交
3762
          .onClick(() => {
Y
yamila 已提交
3763
            this.controller.backward()
L
laosan_ted 已提交
3764
          })
Z
zhou-liting125 已提交
3765
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3766
      }
L
update  
laosan_ted 已提交
3767 3768 3769
    }
  }
  ```
L
lixingchi1 已提交
3770

L
laosan_ted 已提交
3771
### forward<sup>(deprecated)</sup>
L
lixingchi1 已提交
3772

L
update  
laosan_ted 已提交
3773
forward(): void
L
lixingchi1 已提交
3774

L
liwenzhen 已提交
3775
按照历史栈,前进一个页面。一般结合accessForward一起使用。
L
lixingchi1 已提交
3776

L
laosan_ted 已提交
3777 3778
从API version 9开始不再维护,建议使用[forward<sup>9+</sup>](../apis/js-apis-webview.md#forward)代替。

Z
zhou-liting125 已提交
3779
**示例:**
L
laosan_ted 已提交
3780

Z
zhou-liting125 已提交
3781 3782
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3783
  @Entry
L
update  
laosan_ted 已提交
3784 3785
  @Component
  struct WebComponent {
Y
yamila 已提交
3786
    controller: WebController = new WebController()
L
laosan_ted 已提交
3787
  
L
update  
laosan_ted 已提交
3788 3789 3790
    build() {
      Column() {
        Button('forward')
L
laosan_ted 已提交
3791
          .onClick(() => {
Y
yamila 已提交
3792
            this.controller.forward()
L
laosan_ted 已提交
3793
          })
Z
zhou-liting125 已提交
3794
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3795
      }
L
update  
laosan_ted 已提交
3796 3797 3798 3799
    }
  }
  ```

3800 3801 3802 3803 3804 3805
### backOrForward<sup>9+</sup>

backOrForward(step: number): void

按照历史栈,前进或者后退指定步长的页面,当历史栈中不存在对应步长的页面时,不会进行页面跳转。

Z
zhou-liting125 已提交
3806
**参数:**
L
laosan_ted 已提交
3807

3808 3809 3810
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述        |
| ---- | ------ | ---- | ---- | ----------- |
| step | number | 是    | -    | 需要前进或后退的步长。 |
3811

Z
zhou-liting125 已提交
3812
**示例:**
L
laosan_ted 已提交
3813

Z
zhou-liting125 已提交
3814 3815
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3816
  @Entry
L
update  
laosan_ted 已提交
3817 3818
  @Component
  struct WebComponent {
Y
yamila 已提交
3819 3820
    controller: WebController = new WebController()
    @State step: number = -2
L
laosan_ted 已提交
3821
  
L
update  
laosan_ted 已提交
3822 3823 3824
    build() {
      Column() {
        Button('backOrForward')
L
laosan_ted 已提交
3825
          .onClick(() => {
Y
yamila 已提交
3826
            this.controller.backOrForward(this.step)
L
laosan_ted 已提交
3827
          })
3828
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3829
      }
L
update  
laosan_ted 已提交
3830 3831 3832 3833
    }
  }
  ```

L
laosan_ted 已提交
3834
### deleteJavaScriptRegister<sup>(deprecated)</sup>
L
update  
laosan_ted 已提交
3835 3836 3837

deleteJavaScriptRegister(name: string)

L
laosan_ted 已提交
3838
删除通过registerJavaScriptProxy注册到window上的指定name的应用侧JavaScript对象。删除后立即生效,无须调用[refresh](#refresh)接口。
L
update  
laosan_ted 已提交
3839

L
laosan_ted 已提交
3840 3841
从API version 9开始不再维护,建议使用[deleteJavaScriptRegister<sup>9+</sup>](../apis/js-apis-webview.md#deletejavascriptregister)代替。

Z
zhou-liting125 已提交
3842
**参数:**
L
laosan_ted 已提交
3843

3844 3845 3846
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---- | ------ | ---- | ---- | ---------------------------------------- |
| name | string | 是    | -    | 注册对象的名称,可在网页侧JavaScript中通过此名称调用应用侧JavaScript对象。 |
L
update  
laosan_ted 已提交
3847

Z
zhou-liting125 已提交
3848
**示例:**
L
laosan_ted 已提交
3849

Z
zhou-liting125 已提交
3850 3851
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3852
  @Entry
L
update  
laosan_ted 已提交
3853 3854
  @Component
  struct WebComponent {
Y
yamila 已提交
3855 3856
    controller: WebController = new WebController()
    @State name: string = 'Object'
L
laosan_ted 已提交
3857
  
L
update  
laosan_ted 已提交
3858 3859 3860
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
L
laosan_ted 已提交
3861
          .onClick(() => {
Y
yamila 已提交
3862
            this.controller.deleteJavaScriptRegister(this.name)
L
laosan_ted 已提交
3863
          })
Z
zhou-liting125 已提交
3864
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3865
      }
L
update  
laosan_ted 已提交
3866 3867 3868 3869
    }
  }
  ```

L
laosan_ted 已提交
3870
### getHitTest<sup>(deprecated)</sup>
L
lixingchi1 已提交
3871 3872 3873

getHitTest(): HitTestType

L
liwenzhen 已提交
3874
获取当前被点击区域的元素类型。	
L
lixingchi1 已提交
3875

L
laosan_ted 已提交
3876 3877
从API version 9开始不再维护,建议使用[getHitTest<sup>9+</sup>](../apis/js-apis-webview.md#gethittest)代替。

Z
zhou-liting125 已提交
3878
**返回值:**
L
laosan_ted 已提交
3879

3880 3881 3882
| 类型                              | 说明          |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype枚举说明) | 被点击区域的元素类型。 |
L
lixingchi1 已提交
3883

Z
zhou-liting125 已提交
3884
**示例:**
L
laosan_ted 已提交
3885

Z
zhou-liting125 已提交
3886 3887
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3888
  @Entry
L
update  
laosan_ted 已提交
3889 3890
  @Component
  struct WebComponent {
Y
yamila 已提交
3891
    controller: WebController = new WebController()
L
laosan_ted 已提交
3892
  
L
update  
laosan_ted 已提交
3893 3894 3895
    build() {
      Column() {
        Button('getHitTest')
L
laosan_ted 已提交
3896
          .onClick(() => {
Y
yamila 已提交
3897 3898
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
L
laosan_ted 已提交
3899
          })
Z
zhou-liting125 已提交
3900
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3901
      }
L
update  
laosan_ted 已提交
3902 3903 3904 3905
    }
  }
  ```

3906 3907 3908 3909 3910
### getHitTestValue<sup>9+</sup>
getHitTestValue(): HitTestValue

获取当前被点击区域的元素信息。

Z
zhou-liting125 已提交
3911
**返回值:**
L
laosan_ted 已提交
3912

3913 3914 3915
| 类型                             | 说明         |
| ------------------------------ | ---------- |
| [HitTestValue](#hittestvalue9) | 点击区域的元素信息。 |
3916

Z
zhou-liting125 已提交
3917
**示例:**
L
laosan_ted 已提交
3918

Z
zhou-liting125 已提交
3919 3920
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3921
  @Entry
L
update  
laosan_ted 已提交
3922 3923
  @Component
  struct WebComponent {
Y
yamila 已提交
3924
    controller: WebController = new WebController()
L
laosan_ted 已提交
3925
  
L
update  
laosan_ted 已提交
3926 3927 3928
    build() {
      Column() {
        Button('getHitTestValue')
L
laosan_ted 已提交
3929
          .onClick(() => {
Y
yamila 已提交
3930 3931 3932
            let hitValue = this.controller.getHitTestValue()
            console.log("hitType: " + hitValue.getType())
            console.log("extra: " + hitValue.getExtra())
L
laosan_ted 已提交
3933
          })
Z
zhou-liting125 已提交
3934
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3935
      }
L
update  
laosan_ted 已提交
3936 3937 3938 3939
    }
  }
  ```

3940 3941 3942 3943 3944
### getWebId<sup>9+</sup>
getWebId(): number

获取当前Web组件的索引值,用于多个Web组件的管理。

Z
zhou-liting125 已提交
3945
**返回值:**
L
laosan_ted 已提交
3946

3947 3948 3949
| 类型     | 说明           |
| ------ | ------------ |
| number | 当前Web组件的索引值。 |
3950

Z
zhou-liting125 已提交
3951
**示例:**
L
laosan_ted 已提交
3952

Z
zhou-liting125 已提交
3953 3954
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3955
  @Entry
L
update  
laosan_ted 已提交
3956 3957
  @Component
  struct WebComponent {
Y
yamila 已提交
3958
    controller: WebController = new WebController()
L
laosan_ted 已提交
3959
  
L
update  
laosan_ted 已提交
3960 3961 3962
    build() {
      Column() {
        Button('getWebId')
L
laosan_ted 已提交
3963
          .onClick(() => {
Y
yamila 已提交
3964 3965
            let id = this.controller.getWebId()
            console.log("id: " + id)
L
laosan_ted 已提交
3966
          })
Z
zhou-liting125 已提交
3967
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3968
      }
L
update  
laosan_ted 已提交
3969 3970 3971 3972
    }
  }
  ```

3973 3974 3975 3976
### getTitle<sup>9+</sup>
getTitle(): string

获取当前网页的标题。
Z
zhou-liting125 已提交
3977 3978

**返回值:**
L
laosan_ted 已提交
3979

3980 3981 3982
| 类型     | 说明       |
| ------ | -------- |
| string | 当前网页的标题。 |
3983

Z
zhou-liting125 已提交
3984
**示例:**
L
laosan_ted 已提交
3985

Z
zhou-liting125 已提交
3986 3987
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
3988
  @Entry
L
update  
laosan_ted 已提交
3989 3990
  @Component
  struct WebComponent {
Y
yamila 已提交
3991
    controller: WebController = new WebController()
L
laosan_ted 已提交
3992
  
L
update  
laosan_ted 已提交
3993 3994 3995
    build() {
      Column() {
        Button('getTitle')
L
laosan_ted 已提交
3996
          .onClick(() => {
Y
yamila 已提交
3997 3998
            let title = this.controller.getTitle()
            console.log("title: " + title)
L
laosan_ted 已提交
3999
          })
Z
zhou-liting125 已提交
4000
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4001
      }
L
update  
laosan_ted 已提交
4002 4003 4004 4005
    }
  }
  ```

4006 4007 4008 4009
### getPageHeight<sup>9+</sup>
getPageHeight(): number

获取当前网页的页面高度。
Z
zhou-liting125 已提交
4010 4011

**返回值:**
L
laosan_ted 已提交
4012

4013 4014 4015
| 类型     | 说明         |
| ------ | ---------- |
| number | 当前网页的页面高度。 |
4016

Z
zhou-liting125 已提交
4017
**示例:**
L
laosan_ted 已提交
4018

Z
zhou-liting125 已提交
4019 4020
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4021
  @Entry
L
update  
laosan_ted 已提交
4022 4023
  @Component
  struct WebComponent {
Y
yamila 已提交
4024
    controller: WebController = new WebController()
L
laosan_ted 已提交
4025
  
L
update  
laosan_ted 已提交
4026 4027 4028
    build() {
      Column() {
        Button('getPageHeight')
L
laosan_ted 已提交
4029
          .onClick(() => {
Y
yamila 已提交
4030 4031
            let pageHeight = this.controller.getPageHeight()
            console.log("pageHeight: " + pageHeight)
L
laosan_ted 已提交
4032
          })
Z
zhou-liting125 已提交
4033
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4034
      }
L
update  
laosan_ted 已提交
4035 4036 4037
    }
  }
  ```
4038 4039 4040 4041 4042

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

获取当前默认用户代理。
L
update  
laosan_ted 已提交
4043

Z
zhou-liting125 已提交
4044
**返回值:**
L
laosan_ted 已提交
4045

4046 4047 4048
| 类型     | 说明      |
| ------ | ------- |
| string | 默认用户代理。 |
L
lixingchi1 已提交
4049

Z
zhou-liting125 已提交
4050
**示例:**
L
laosan_ted 已提交
4051

Z
zhou-liting125 已提交
4052 4053
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4054
  @Entry
L
update  
laosan_ted 已提交
4055 4056
  @Component
  struct WebComponent {
Y
yamila 已提交
4057
    controller: WebController = new WebController()
L
laosan_ted 已提交
4058
  
L
update  
laosan_ted 已提交
4059 4060 4061
    build() {
      Column() {
        Button('getDefaultUserAgent')
L
laosan_ted 已提交
4062
          .onClick(() => {
Y
yamila 已提交
4063 4064
            let userAgent = this.controller.getDefaultUserAgent()
            console.log("userAgent: " + userAgent)
L
laosan_ted 已提交
4065
          })
Z
zhou-liting125 已提交
4066
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4067
      }
L
update  
laosan_ted 已提交
4068 4069 4070 4071
    }
  }
  ```

L
laosan_ted 已提交
4072
### loadData<sup>(deprecated)</sup>
L
lixingchi1 已提交
4073

Z
zhou-liting125 已提交
4074
loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string })
L
lixingchi1 已提交
4075

L
liwenzhen 已提交
4076 4077
baseUrl为空时,通过”data“协议加载指定的一段字符串。

L
liwenzhen 已提交
4078
当baseUrl为”data“协议时,编码后的data字符串将被Web组件作为”data"协议加载。
L
liwenzhen 已提交
4079

L
liwenzhen 已提交
4080
当baseUrl为“http/https"协议时,编码后的data字符串将被Web组件以类似loadUrl的方式以非编码字符串处理。
L
lixingchi1 已提交
4081

L
laosan_ted 已提交
4082 4083
从API version 9开始不再维护,建议使用[loadData<sup>9+</sup>](../apis/js-apis-webview.md#loaddata)代替。

Z
zhou-liting125 已提交
4084
**参数:**
L
laosan_ted 已提交
4085

4086 4087 4088 4089 4090 4091 4092
| 参数名        | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---------- | ------ | ---- | ---- | ---------------------------------------- |
| data       | string | 是    | -    | 按照”Base64“或者”URL"编码后的一段字符串。              |
| mimeType   | string | 是    | -    | 媒体类型(MIME)。                              |
| encoding   | string | 是    | -    | 编码类型,具体为“Base64"或者”URL编码。                |
| baseUrl    | string | 否    | -    | 指定的一个URL路径(“http”/“https”/"data"协议),并由Web组件赋值给window.origin。 |
| historyUrl | string | 否    | -    | 历史记录URL。非空时,可被历史记录管理,实现前后后退功能。当baseUrl为空时,此属性无效。 |
Z
zengyawen 已提交
4093

Z
zhou-liting125 已提交
4094
**示例:**
L
laosan_ted 已提交
4095

Z
zhou-liting125 已提交
4096 4097
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4098
  @Entry
L
update  
laosan_ted 已提交
4099 4100
  @Component
  struct WebComponent {
Y
yamila 已提交
4101
    controller: WebController = new WebController()
L
laosan_ted 已提交
4102
  
L
update  
laosan_ted 已提交
4103 4104 4105
    build() {
      Column() {
        Button('loadData')
L
laosan_ted 已提交
4106 4107 4108 4109 4110
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
Y
yamila 已提交
4111
            })
L
laosan_ted 已提交
4112
          })
Z
zhou-liting125 已提交
4113
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4114
      }
L
update  
laosan_ted 已提交
4115 4116 4117 4118
    }
  }
  ```

L
laosan_ted 已提交
4119
### loadUrl<sup>(deprecated)</sup>
Z
zengyawen 已提交
4120

Z
zhou-liting125 已提交
4121
loadUrl(options: { url: string | Resource, headers?: Array\<Header\> })
L
liwenzhen 已提交
4122 4123

使用指定的http头加载指定的URL。
Z
zengyawen 已提交
4124

L
liwenzhen 已提交
4125 4126 4127
通过loadUrl注入的对象只在当前document有效,即通过loadUrl导航到新的页面会无效。

而通过registerJavaScriptProxy注入的对象,在loadUrl导航到新的页面也会有效。
Z
zengyawen 已提交
4128

L
laosan_ted 已提交
4129 4130
从API version 9开始不再维护,建议使用[loadUrl<sup>9+</sup>](../apis/js-apis-webview.md#loadurl)代替。

Z
zhou-liting125 已提交
4131
**参数:**
L
laosan_ted 已提交
4132

4133 4134 4135 4136
| 参数名     | 参数类型                       | 必填   | 默认值  | 参数描述           |
| ------- | -------------------------- | ---- | ---- | -------------- |
| url     | string                     | 是    | -    | 需要加载的 URL。     |
| headers | Array\<[Header](#header)\> | 否    | []   | URL的附加HTTP请求头。 |
L
lixingchi1 已提交
4137

Z
zhou-liting125 已提交
4138
**示例:**
L
laosan_ted 已提交
4139

Z
zhou-liting125 已提交
4140 4141
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4142
  @Entry
L
update  
laosan_ted 已提交
4143 4144
  @Component
  struct WebComponent {
Y
yamila 已提交
4145
    controller: WebController = new WebController()
L
laosan_ted 已提交
4146
  
L
update  
laosan_ted 已提交
4147 4148 4149
    build() {
      Column() {
        Button('loadUrl')
L
laosan_ted 已提交
4150
          .onClick(() => {
Y
yamila 已提交
4151
            this.controller.loadUrl({ url: 'www.example.com' })
L
laosan_ted 已提交
4152
          })
Z
zhou-liting125 已提交
4153
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4154
      }
L
update  
laosan_ted 已提交
4155 4156 4157 4158
    }
  }
  ```

L
laosan_ted 已提交
4159
### onActive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4160 4161 4162

onActive(): void

L
liwenzhen 已提交
4163
调用此接口通知Web组件进入前台激活状态。
L
lixingchi1 已提交
4164

L
laosan_ted 已提交
4165 4166
从API version 9开始不再维护,建议使用[onActive<sup>9+</sup>](../apis/js-apis-webview.md#onactive)代替。

Z
zhou-liting125 已提交
4167
**示例:**
L
laosan_ted 已提交
4168

Z
zhou-liting125 已提交
4169 4170
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4171
  @Entry
L
update  
laosan_ted 已提交
4172 4173
  @Component
  struct WebComponent {
Y
yamila 已提交
4174
    controller: WebController = new WebController()
L
laosan_ted 已提交
4175
  
L
update  
laosan_ted 已提交
4176 4177 4178
    build() {
      Column() {
        Button('onActive')
L
laosan_ted 已提交
4179
          .onClick(() => {
Y
yamila 已提交
4180
            this.controller.onActive()
L
laosan_ted 已提交
4181
          })
Z
zhou-liting125 已提交
4182
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4183
      }
L
update  
laosan_ted 已提交
4184 4185 4186 4187
    }
  }
  ```

L
laosan_ted 已提交
4188
### onInactive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4189 4190 4191

onInactive(): void

L
liwenzhen 已提交
4192
调用此接口通知Web组件进入未激活状态。
L
lixingchi1 已提交
4193

L
laosan_ted 已提交
4194 4195
从API version 9开始不再维护,建议使用[onInactive<sup>9+</sup>](../apis/js-apis-webview.md#oninactive)代替。

Z
zhou-liting125 已提交
4196
**示例:**
L
laosan_ted 已提交
4197

Z
zhou-liting125 已提交
4198 4199
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4200
  @Entry
L
update  
laosan_ted 已提交
4201 4202
  @Component
  struct WebComponent {
Y
yamila 已提交
4203
    controller: WebController = new WebController()
L
laosan_ted 已提交
4204
  
L
update  
laosan_ted 已提交
4205 4206 4207
    build() {
      Column() {
        Button('onInactive')
L
laosan_ted 已提交
4208
          .onClick(() => {
Y
yamila 已提交
4209
            this.controller.onInactive()
L
laosan_ted 已提交
4210
          })
Z
zhou-liting125 已提交
4211
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4212
      }
L
update  
laosan_ted 已提交
4213 4214 4215 4216
    }
  }
  ```

L
laosan_ted 已提交
4217
### zoom<sup>(deprecated)</sup>
4218 4219 4220
zoom(factor: number): void

调整当前网页的缩放比例。
L
update  
laosan_ted 已提交
4221

L
laosan_ted 已提交
4222 4223
从API version 9开始不再维护,建议使用[zoom<sup>9+</sup>](../apis/js-apis-webview.md#zoom)代替。

Z
zhou-liting125 已提交
4224
**参数:**
L
laosan_ted 已提交
4225

4226 4227 4228
| 参数名    | 参数类型   | 必填   | 参数描述                           |
| ------ | ------ | ---- | ------------------------------ |
| factor | number | 是    | 基于当前网页所需调整的相对缩放比例,正值为放大,负值为缩小。 |
4229

Z
zhou-liting125 已提交
4230
**示例:**
L
laosan_ted 已提交
4231

Z
zhou-liting125 已提交
4232 4233
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4234
  @Entry
L
update  
laosan_ted 已提交
4235 4236
  @Component
  struct WebComponent {
Y
yamila 已提交
4237 4238
    controller: WebController = new WebController()
    @State factor: number = 1
L
laosan_ted 已提交
4239
  
L
update  
laosan_ted 已提交
4240 4241 4242
    build() {
      Column() {
        Button('zoom')
L
laosan_ted 已提交
4243
          .onClick(() => {
Y
yamila 已提交
4244
            this.controller.zoom(this.factor)
L
laosan_ted 已提交
4245
          })
Z
zhou-liting125 已提交
4246
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4247
      }
L
update  
laosan_ted 已提交
4248 4249 4250 4251
    }
  }
  ```

4252 4253 4254 4255
### zoomIn<sup>9+</sup>
zoomIn(): boolean

调用此接口将当前网页进行放大,比列20%。
Z
zhou-liting125 已提交
4256 4257

**返回值:**
L
laosan_ted 已提交
4258

4259 4260 4261
| 类型      | 说明          |
| ------- | ----------- |
| boolean | 放大操作是否成功执行。 |
4262

Z
zhou-liting125 已提交
4263
**示例:**
L
laosan_ted 已提交
4264

Z
zhou-liting125 已提交
4265 4266
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4267
  @Entry
L
update  
laosan_ted 已提交
4268 4269
  @Component
  struct WebComponent {
Y
yamila 已提交
4270
    controller: WebController = new WebController()
L
laosan_ted 已提交
4271
  
L
update  
laosan_ted 已提交
4272 4273 4274
    build() {
      Column() {
        Button('zoomIn')
L
laosan_ted 已提交
4275
          .onClick(() => {
Y
yamila 已提交
4276 4277
            let result = this.controller.zoomIn()
            console.log("result: " + result)
L
laosan_ted 已提交
4278
          })
Z
zhou-liting125 已提交
4279
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4280
      }
L
update  
laosan_ted 已提交
4281 4282 4283 4284
    }
  }
  ```

4285 4286 4287 4288
### zoomOut<sup>9+</sup>
zoomOut(): boolean

调用此接口将当前网页进行缩小,比列20%。
Z
zhou-liting125 已提交
4289 4290

**返回值:**
L
laosan_ted 已提交
4291

4292 4293 4294
| 类型      | 说明          |
| ------- | ----------- |
| boolean | 缩小操作是否成功执行。 |
4295

Z
zhou-liting125 已提交
4296
**示例:**
L
laosan_ted 已提交
4297

Z
zhou-liting125 已提交
4298 4299
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4300
  @Entry
L
update  
laosan_ted 已提交
4301 4302
  @Component
  struct WebComponent {
Y
yamila 已提交
4303
    controller: WebController = new WebController()
L
laosan_ted 已提交
4304
  
L
update  
laosan_ted 已提交
4305 4306 4307
    build() {
      Column() {
        Button('zoomOut')
L
laosan_ted 已提交
4308
          .onClick(() => {
Y
yamila 已提交
4309 4310
            let result = this.controller.zoomOut()
            console.log("result: " + result)
L
laosan_ted 已提交
4311
          })
Z
zhou-liting125 已提交
4312
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4313
      }
L
update  
laosan_ted 已提交
4314 4315 4316 4317
    }
  }
  ```

L
laosan_ted 已提交
4318
### refresh<sup>(deprecated)</sup>
L
lixingchi1 已提交
4319

Z
zhou-liting125 已提交
4320
refresh()
L
lixingchi1 已提交
4321

L
liwenzhen 已提交
4322
调用此接口通知Web组件刷新网页。
L
lixingchi1 已提交
4323

L
laosan_ted 已提交
4324 4325
从API version 9开始不再维护,建议使用[refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh)代替。

Z
zhou-liting125 已提交
4326
**示例:**
L
laosan_ted 已提交
4327

Z
zhou-liting125 已提交
4328 4329
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4330
  @Entry
L
update  
laosan_ted 已提交
4331 4332
  @Component
  struct WebComponent {
Y
yamila 已提交
4333
    controller: WebController = new WebController()
L
laosan_ted 已提交
4334
  
L
update  
laosan_ted 已提交
4335 4336 4337
    build() {
      Column() {
        Button('refresh')
L
laosan_ted 已提交
4338
          .onClick(() => {
Y
yamila 已提交
4339
            this.controller.refresh()
L
laosan_ted 已提交
4340
          })
Z
zhou-liting125 已提交
4341
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4342
      }
L
update  
laosan_ted 已提交
4343 4344 4345 4346
    }
  }
  ```

L
laosan_ted 已提交
4347
### registerJavaScriptProxy<sup>(deprecated)</sup>
L
lixingchi1 已提交
4348

Z
zhou-liting125 已提交
4349
registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })
L
lixingchi1 已提交
4350

L
laosan_ted 已提交
4351
注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。注册后,须调用[refresh](#refresh)接口生效。
L
lixingchi1 已提交
4352

L
laosan_ted 已提交
4353 4354
从API version 9开始不再维护,建议使用[registerJavaScriptProxy<sup>9+</sup>](../apis/js-apis-webview.md#registerjavascriptproxy)代替。

Z
zhou-liting125 已提交
4355
**参数:**
L
laosan_ted 已提交
4356

4357 4358 4359 4360 4361
| 参数名        | 参数类型            | 必填   | 默认值  | 参数描述                                     |
| ---------- | --------------- | ---- | ---- | ---------------------------------------- |
| object     | object          | 是    | -    | 参与注册的应用侧JavaScript对象。只能声明方法,不能声明属性 。其中方法的参数和返回类型只能为string,number,boolean |
| name       | string          | 是    | -    | 注册对象的名称,与window中调用的对象名一致。注册后window对象可以通过此名字访问应用侧JavaScript对象。 |
| methodList | Array\<string\> | 是    | -    | 参与注册的应用侧JavaScript对象的方法。                 |
L
lixingchi1 已提交
4362

Z
zhou-liting125 已提交
4363
**示例:**
L
laosan_ted 已提交
4364

Z
zhou-liting125 已提交
4365 4366
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4367
  @Entry
L
update  
laosan_ted 已提交
4368 4369 4370 4371 4372
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
Y
yamila 已提交
4373
        return "ArkUI Web Component"
L
update  
laosan_ted 已提交
4374 4375
      },
      toString: () => {
Y
yamila 已提交
4376
        console.log('Web Component toString')
L
update  
laosan_ted 已提交
4377 4378 4379 4380 4381 4382 4383 4384 4385 4386
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
Y
yamila 已提交
4387
            })
L
update  
laosan_ted 已提交
4388 4389 4390 4391 4392 4393 4394 4395
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```
4396

L
update  
laosan_ted 已提交
4397
  ```html
L
laosan_ted 已提交
4398
  <!-- index.html -->
L
update  
laosan_ted 已提交
4399 4400 4401 4402 4403 4404 4405 4406
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
Y
yamila 已提交
4407 4408
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
L
update  
laosan_ted 已提交
4409 4410 4411 4412 4413 4414
      }
  </script>
  </html>
  
  ```

L
laosan_ted 已提交
4415
### runJavaScript<sup>(deprecated)</sup>
L
lixingchi1 已提交
4416

Z
zhou-liting125 已提交
4417
runJavaScript(options: { script: string, callback?: (result: string) => void })
L
lixingchi1 已提交
4418

L
liwenzhen 已提交
4419
异步执行JavaScript脚本,并通过回调方式返回脚本执行的结果。runJavaScript需要在loadUrl完成后,比如onPageEnd中调用。
L
lixingchi1 已提交
4420

L
laosan_ted 已提交
4421 4422
从API version 9开始不再维护,建议使用[runJavaScript<sup>9+</sup>](../apis/js-apis-webview.md#runjavascript)代替。

Z
zhou-liting125 已提交
4423
**参数:**
L
laosan_ted 已提交
4424

4425 4426 4427 4428
| 参数名      | 参数类型                     | 必填   | 默认值  | 参数描述                                     |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | 是    | -    | JavaScript脚本。                            |
| callback | (result: string) => void | 否    | -    | 回调执行JavaScript脚本结果。JavaScript脚本若执行失败或无返回值时,返回null。 |
L
lixingchi1 已提交
4429

Z
zhou-liting125 已提交
4430
**示例:**
L
laosan_ted 已提交
4431

Z
zhou-liting125 已提交
4432 4433
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4434
  @Entry
L
update  
laosan_ted 已提交
4435 4436
  @Component
  struct WebComponent {
Y
yamila 已提交
4437
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449
    @State webResult: string = ''
    build() {
      Column() {
        Text(this.webResult).fontSize(20)
        Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          this.controller.runJavaScript({
            script: 'test()',
            callback: (result: string)=> {
              this.webResult = result
              console.info(`The test() return value is: ${result}`)
Y
yamila 已提交
4450 4451
            }})
          console.info('url: ', e.url)
L
update  
laosan_ted 已提交
4452 4453 4454 4455 4456 4457 4458
        })
      }
    }
  }
  ```

  ```html
L
laosan_ted 已提交
4459
  <!-- index.html -->
L
update  
laosan_ted 已提交
4460 4461 4462 4463 4464 4465 4466 4467
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
Y
yamila 已提交
4468
        console.log('Ark WebComponent')
L
update  
laosan_ted 已提交
4469 4470 4471 4472 4473 4474 4475
        return "This value is from index.html"
    }
    </script>
  </html>

  ```

L
laosan_ted 已提交
4476
### stop<sup>(deprecated)</sup>
L
lixingchi1 已提交
4477

Z
zhou-liting125 已提交
4478
stop()
Z
zengyawen 已提交
4479

L
lixingchi1 已提交
4480
停止页面加载。
Z
zengyawen 已提交
4481

L
laosan_ted 已提交
4482 4483
从API version 9开始不再维护,建议使用[stop<sup>9+</sup>](../apis/js-apis-webview.md#stop)代替。

Z
zhou-liting125 已提交
4484
**示例:**
L
laosan_ted 已提交
4485

Z
zhou-liting125 已提交
4486 4487
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4488
  @Entry
L
update  
laosan_ted 已提交
4489 4490
  @Component
  struct WebComponent {
Y
yamila 已提交
4491
    controller: WebController = new WebController()
L
laosan_ted 已提交
4492
  
L
update  
laosan_ted 已提交
4493 4494 4495
    build() {
      Column() {
        Button('stop')
L
laosan_ted 已提交
4496
          .onClick(() => {
Y
yamila 已提交
4497
            this.controller.stop()
L
laosan_ted 已提交
4498
          })
Z
zhou-liting125 已提交
4499
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4500
      }
L
update  
laosan_ted 已提交
4501 4502 4503 4504
    }
  }
  ```

L
laosan_ted 已提交
4505
### clearHistory<sup>(deprecated)</sup>
T
Ted 已提交
4506 4507 4508 4509 4510

clearHistory(): void

删除所有前进后退记录。

L
laosan_ted 已提交
4511 4512
从API version 9开始不再维护,建议使用[clearHistory<sup>9+</sup>](../apis/js-apis-webview.md#clearhistory)代替。

Z
zhou-liting125 已提交
4513
**示例:**
L
laosan_ted 已提交
4514

Z
zhou-liting125 已提交
4515 4516
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4517
  @Entry
L
update  
laosan_ted 已提交
4518 4519
  @Component
  struct WebComponent {
Y
yamila 已提交
4520
    controller: WebController = new WebController()
L
laosan_ted 已提交
4521
  
L
update  
laosan_ted 已提交
4522 4523 4524
    build() {
      Column() {
        Button('clearHistory')
L
laosan_ted 已提交
4525
          .onClick(() => {
Y
yamila 已提交
4526
            this.controller.clearHistory()
L
laosan_ted 已提交
4527
          })
Z
zhou-liting125 已提交
4528
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4529
      }
L
update  
laosan_ted 已提交
4530 4531 4532 4533
    }
  }
  ```

I
i-am-a-little-bird 已提交
4534 4535 4536 4537
### clearSslCache

clearSslCache(): void

I
bugfix  
i-am-a-little-bird 已提交
4538
清除Web组件记录的SSL证书错误事件对应的用户操作行为。
I
i-am-a-little-bird 已提交
4539 4540 4541 4542 4543 4544 4545 4546

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4547
    controller: WebController = new WebController()
I
i-am-a-little-bird 已提交
4548 4549 4550 4551 4552

    build() {
      Column() {
        Button('clearSslCache')
          .onClick(() => {
Y
yamila 已提交
4553
            this.controller.clearSslCache()
I
i-am-a-little-bird 已提交
4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### clearClientAuthenticationCache

clearClientAuthenticationCache(): void

I
bugfix  
i-am-a-little-bird 已提交
4565
清除Web组件记录的客户端证书请求事件对应的用户操作行为。
I
i-am-a-little-bird 已提交
4566 4567 4568 4569 4570 4571 4572 4573

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4574
    controller: WebController = new WebController()
I
i-am-a-little-bird 已提交
4575 4576 4577 4578 4579

    build() {
      Column() {
        Button('clearClientAuthenticationCache')
          .onClick(() => {
Y
yamila 已提交
4580
            this.controller.clearClientAuthenticationCache()
I
i-am-a-little-bird 已提交
4581 4582 4583 4584 4585 4586 4587
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
4588
### getCookieManager<sup>9+</sup>
T
Ted 已提交
4589 4590 4591 4592

getCookieManager(): WebCookie

获取web组件cookie管理对象。
Z
zhou-liting125 已提交
4593 4594

**返回值:**
L
laosan_ted 已提交
4595

4596 4597 4598
| 类型        | 说明                                       |
| --------- | ---------------------------------------- |
| WebCookie | web组件cookie管理对象,参考[WebCookie](#webcookie)定义。 |
4599

Z
zhou-liting125 已提交
4600
**示例:**
L
laosan_ted 已提交
4601

Z
zhou-liting125 已提交
4602 4603
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4604
  @Entry
L
update  
laosan_ted 已提交
4605 4606
  @Component
  struct WebComponent {
Y
yamila 已提交
4607
    controller: WebController = new WebController()
L
laosan_ted 已提交
4608
  
L
update  
laosan_ted 已提交
4609 4610 4611
    build() {
      Column() {
        Button('getCookieManager')
L
laosan_ted 已提交
4612
          .onClick(() => {
Y
yamila 已提交
4613
            let cookieManager = this.controller.getCookieManager()
L
laosan_ted 已提交
4614
          })
Z
zhou-liting125 已提交
4615
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4616
      }
L
update  
laosan_ted 已提交
4617 4618 4619 4620
    }
  }
  ```

4621 4622
### createWebMessagePorts<sup>9+</sup>

E
echoorchid 已提交
4623
createWebMessagePorts(): Array\<WebMessagePort\>
4624

E
echoorchid 已提交
4625
创建Web消息端口。
4626 4627

**返回值:**
4628

L
laosan_ted 已提交
4629

H
HelloCrease 已提交
4630 4631
| 类型                                       | 说明         |
| ---------------------------------------- | ---------- |
E
echoorchid 已提交
4632
| Array\<[WebMessagePort](#webmessageport9)\> | web消息端口列表。 |
4633 4634

**示例:**
L
laosan_ted 已提交
4635

4636 4637 4638 4639 4640
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4641 4642
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
4643 4644 4645 4646
    build() {
      Column() {
        Button('createWebMessagePorts')
          .onClick(() => {
Y
yamila 已提交
4647
            this.ports = this.controller.createWebMessagePorts()
4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659
            console.log("createWebMessagePorts size:" + this.ports.length)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### postMessage<sup>9+</sup>

postMessage(options: { message: WebMessageEvent, uri: string}): void

E
echoorchid 已提交
4660
发送Web消息端口到HTML5。
4661 4662

**参数:**
4663

H
HelloCrease 已提交
4664 4665 4666 4667
| 参数名     | 参数类型                                 | 必填   | 默认值  | 参数描述              |
| ------- | ------------------------------------ | ---- | ---- | ----------------- |
| message | [WebMessageEvent](#webmessageevent9) | 是    | -    | 要发送的消息,包含数据和消息端口。 |
| uri     | string                               | 是    | -    | 接收该消息的URI。        |
4668 4669

**示例:**
L
laosan_ted 已提交
4670

4671
  ```ts
E
echoorchid 已提交
4672
  // index.ets
4673 4674 4675
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4676 4677 4678 4679
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
    @State sendFromEts: string = 'Send this message from ets to HTML'
    @State receivedFromHtml: string = 'Display received message send from HTML'
E
echoorchid 已提交
4680

4681 4682
    build() {
      Column() {
E
echoorchid 已提交
4683 4684 4685 4686 4687
        // 展示接收到的来自HTML的内容
        Text(this.receivedFromHtml)
        // 输入框的内容发送到HTML
        TextInput({placeholder: 'Send this message from ets to HTML'})
        .onChange((value: string) => {
Y
yamila 已提交
4688
          this.sendFromEts = value
E
echoorchid 已提交
4689 4690 4691 4692 4693
        })

        // 1、创建两个消息端口
        Button('1.CreateWebMessagePorts')
          .onClick(() => {
Y
yamila 已提交
4694
            this.ports = this.controller.createWebMessagePorts()
E
echoorchid 已提交
4695 4696 4697 4698 4699
            console.log("createWebMessagePorts size:" + this.ports.length)
          })

        // 2、将其中一个消息端口发送到HTML侧,由HTML侧保存并使用。
        Button('2.PostMessagePort')
4700
          .onClick(() => {
Y
yamila 已提交
4701 4702 4703 4704 4705
            var sendPortArray = new Array(this.ports[1])
            var msgEvent = new WebMessageEvent()
            msgEvent.setData("__init_port__")
            msgEvent.setPorts(sendPortArray)
            this.controller.postMessage({message: msgEvent, uri: "*"})
4706
          })
E
echoorchid 已提交
4707 4708 4709 4710 4711

        // 3、另一个消息端口在应用侧注册回调事件。
        Button('3.RegisterCallback')
          .onClick(() => {
              this.ports[0].onMessageEvent((result: string) => {
Y
yamila 已提交
4712 4713
                var msg = 'Got msg from HTML: ' + result
                this.receivedFromHtml = msg
E
echoorchid 已提交
4714 4715 4716 4717 4718 4719
              })
          })

        // 4、使用应用侧的端口给另一个已经发送到HTML的消息端口发送消息。
        Button('4.SendDataToHtml5')
          .onClick(() => {
Y
yamila 已提交
4720 4721 4722
            var msg = new WebMessageEvent()
            msg.setData(this.sendFromEts)
            this.ports[0].postMessageEvent(msg)
E
echoorchid 已提交
4723 4724 4725 4726
          })
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .javaScriptAccess(true)
          .fileAccess(true)
4727 4728 4729
      }
    }
  }
E
echoorchid 已提交
4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745

  // index.html
  <!DOCTYPE html>
  <html>
      <body>
          <h1>Web Message Port Demo</h1>
          <div style="font-size: 24pt;">
            <input type="button" value="5.SendToEts" onclick="PostMsgToEts(msgFromJS.value);" /><br/>
            <input id="msgFromJS" type="text" value="send this message from HTML to ets" style="font-size: 16pt;" /><br/>
          </div>
          <p class="output">display received message send from ets</p>
      </body>
      <script src="index.js"></script>
  </html>

  // index.js
4746
  var h5Port;
E
echoorchid 已提交
4747 4748 4749
  var output = document.querySelector('.output');
  window.addEventListener('message', function(event) {
    if (event.data == '__init_port__') {
4750
      if(event.ports[0] != null) {
E
echoorchid 已提交
4751
        h5Port = event.ports[0]; // 1. 保存从ets侧发送过来的端口
4752
        h5Port.onmessage = function(event) {
E
echoorchid 已提交
4753 4754 4755
          // 2. 接收ets侧发送过来的消息.
          var msg = 'Got message from ets:' + event.data;
          output.innerHTML = msg;
4756 4757 4758 4759
        }
      }
    }
  })
E
echoorchid 已提交
4760 4761 4762 4763 4764

  // 3. 使用h5Port往ets侧发送消息.
  function PostMsgToEts(data) {
    h5Port.postMessage(data)
  }
4765 4766
  ```

Y
yu-shihao4 已提交
4767 4768 4769 4770 4771 4772 4773 4774
### getUrl<sup>9+</sup>

getUrl(): string

获取当前页面的url地址。

**返回值:**

H
HelloCrease 已提交
4775 4776
| 类型     | 说明          |
| ------ | ----------- |
Y
yu-shihao4 已提交
4777 4778 4779 4780 4781 4782 4783 4784 4785
| string | 当前页面的url地址。 |

**示例:**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4786
    controller: WebController = new WebController()
Y
yu-shihao4 已提交
4787 4788 4789 4790
    build() {
      Column() {
        Button('getUrl')
          .onClick(() => {
Y
yamila 已提交
4791
            console.log("url: " + this.controller.getUrl())
Y
yu-shihao4 已提交
4792 4793 4794 4795 4796 4797 4798
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

L
laosan_ted 已提交
4799 4800 4801 4802 4803 4804 4805 4806
### searchAllAsync<sup>9+</sup>

searchAllAsync(searchString: string): void

异步查找网页中所有匹配关键字'searchString'的内容并高亮,结果通过[onSearchResultReceive](#onsearchresultreceive9)异步返回。

**参数:**

H
HelloCrease 已提交
4807 4808
| 参数名          | 参数类型   | 必填   | 默认值  | 参数描述    |
| ------------ | ------ | ---- | ---- | ------- |
L
laosan_ted 已提交
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
| searchString | string | 是    | -    | 查找的关键字。 |

**示例:**

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

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

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

clearMatches(): void

清除所有通过[searchAllAsync](#searchallasync9)匹配到的高亮字符查找结果。

**示例:**

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

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

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

searchNext(forward: boolean): void

滚动到下一个匹配的查找结果并高亮。

**参数:**

H
HelloCrease 已提交
4880 4881
| 参数名     | 参数类型    | 必填   | 默认值  | 参数描述        |
| ------- | ------- | ---- | ---- | ----------- |
L
laosan_ted 已提交
4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
| forward | boolean | 是    | -    | 从前向后或者逆向查找。 |


**示例:**

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

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

4906
## HitTestValue<sup>9+</sup>
Z
zhou-liting125 已提交
4907
提供点击区域的元素信息。示例代码参考[getHitTestValue](#gethittestvalue9)
4908 4909 4910 4911 4912

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

获取当前被点击区域的元素类型。
Z
zhou-liting125 已提交
4913 4914

**返回值:**
L
laosan_ted 已提交
4915

4916 4917 4918
| 类型                              | 说明            |
| ------------------------------- | ------------- |
| [HitTestType](#hittesttype枚举说明) | 当前被点击区域的元素类型。 |
4919 4920 4921 4922 4923

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

若被点击区域为图片或链接,则附加参数信息为其url地址。
Z
zhou-liting125 已提交
4924 4925

**返回值:**
L
laosan_ted 已提交
4926

4927 4928 4929
| 类型     | 说明           |
| ------ | ------------ |
| string | 点击区域的附加参数信息。 |
4930

L
laosan_ted 已提交
4931

T
Ted 已提交
4932
## WebCookie
Z
zhou-liting125 已提交
4933

T
Ted 已提交
4934
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookie。通过controller方法中的getCookieManager方法可以获取WebCookie对象,进行后续的cookie管理操作。
Z
zhou-liting125 已提交
4935 4936

### setCookie<sup>9+</sup>
T
Ted 已提交
4937 4938 4939 4940
setCookie(url: string, value: string): boolean

设置cookie,该方法为同步方法。设置成功返回true,否则返回false。

Z
zhou-liting125 已提交
4941
**参数:**
L
laosan_ted 已提交
4942

4943 4944 4945 4946
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | 是    | -    | 要设置的cookie所属的url。 |
| value | string | 是    | -    | cookie的值。         |
Z
zhou-liting125 已提交
4947

Z
zhou-liting125 已提交
4948
**返回值:** 
L
laosan_ted 已提交
4949

4950 4951 4952
| 类型      | 说明            |
| ------- | ------------- |
| boolean | 设置cookie是否成功。 |
T
Ted 已提交
4953

Z
zhou-liting125 已提交
4954
**示例:**
L
laosan_ted 已提交
4955

Z
zhou-liting125 已提交
4956 4957
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4958
  @Entry
L
update  
laosan_ted 已提交
4959 4960
  @Component
  struct WebComponent {
Y
yamila 已提交
4961
    controller: WebController = new WebController()
L
laosan_ted 已提交
4962
  
L
update  
laosan_ted 已提交
4963 4964 4965
    build() {
      Column() {
        Button('setCookie')
L
laosan_ted 已提交
4966
          .onClick(() => {
Y
yamila 已提交
4967 4968
            let result = this.controller.getCookieManager().setCookie("www.example.com", "a=b")
            console.log("result: " + result)
L
laosan_ted 已提交
4969
          })
Z
zhou-liting125 已提交
4970
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4971
      }
L
update  
laosan_ted 已提交
4972 4973 4974 4975
    }
  }
  ```

Z
zhou-liting125 已提交
4976
### saveCookieSync<sup>9+</sup>
T
Ted 已提交
4977 4978 4979
saveCookieSync(): boolean

将当前存在内存中的cookie同步到磁盘中,该方法为同步方法。
Z
zhou-liting125 已提交
4980 4981

**返回值:**
L
laosan_ted 已提交
4982

4983 4984 4985
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 同步内存cookie到磁盘操作是否成功。 |
4986

Z
zhou-liting125 已提交
4987
**示例:**
L
laosan_ted 已提交
4988

Z
zhou-liting125 已提交
4989 4990
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4991
  @Entry
L
update  
laosan_ted 已提交
4992 4993
  @Component
  struct WebComponent {
Y
yamila 已提交
4994
    controller: WebController = new WebController()
L
laosan_ted 已提交
4995
  
L
update  
laosan_ted 已提交
4996 4997 4998
    build() {
      Column() {
        Button('saveCookieSync')
L
laosan_ted 已提交
4999
          .onClick(() => {
Y
yamila 已提交
5000 5001
            let result = this.controller.getCookieManager().saveCookieSync()
            console.log("result: " + result)
L
laosan_ted 已提交
5002
          })
Z
zhou-liting125 已提交
5003
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
5004
      }
L
update  
laosan_ted 已提交
5005 5006 5007 5008
    }
  }
  ```

L
laosan_ted 已提交
5009 5010
### getCookie<sup>9+</sup>
getCookie(url: string): string
5011

L
laosan_ted 已提交
5012
获取指定url对应cookie的值。
5013

L
laosan_ted 已提交
5014
**参数:**
5015

H
HelloCrease 已提交
5016 5017 5018
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述              |
| ---- | ------ | ---- | ---- | ----------------- |
| url  | string | 是    | -    | 要获取的cookie所属的url。 |
5019

L
laosan_ted 已提交
5020 5021
**返回值:**

H
HelloCrease 已提交
5022 5023
| 类型     | 说明                |
| ------ | ----------------- |
L
laosan_ted 已提交
5024
| string | 指定url对应的cookie的值。 |
5025

Z
zhou-liting125 已提交
5026
**示例:**
L
laosan_ted 已提交
5027

Z
zhou-liting125 已提交
5028 5029
  ```ts
  // xxx.ets
L
laosan_ted 已提交
5030
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5031 5032 5033
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5034
    controller: WebController = new WebController()
L
laosan_ted 已提交
5035 5036 5037
  
    build() {
      Column() {
L
laosan_ted 已提交
5038
        Button('getCookie')
L
laosan_ted 已提交
5039
          .onClick(() => {
L
laosan_ted 已提交
5040
            let value = web_webview.WebCookieManager.getCookie('www.example.com')
Y
yamila 已提交
5041
            console.log("value: " + value)
L
laosan_ted 已提交
5042
          })
Z
zhou-liting125 已提交
5043
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
5044 5045 5046
      }
    }
  }
L
update  
laosan_ted 已提交
5047 5048
  ```

L
laosan_ted 已提交
5049 5050
### setCookie<sup>9+</sup>
setCookie(url: string, value: string): boolean
5051

L
laosan_ted 已提交
5052
为指定url设置单个cookie的值。
5053

L
laosan_ted 已提交
5054
**参数:**
5055

L
laosan_ted 已提交
5056 5057 5058
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | 是    | -    | 要设置的cookie所属的url。 |
H
HelloCrease 已提交
5059
| value | string | 是    | -    | 要设置的cookie的值。     |
L
laosan_ted 已提交
5060 5061 5062

**返回值:**

H
HelloCrease 已提交
5063 5064
| 类型      | 说明            |
| ------- | ------------- |
L
laosan_ted 已提交
5065 5066 5067 5068 5069 5070
| boolean | 设置cookie是否成功。 |

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5071
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5072 5073 5074
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5075
    controller: WebController = new WebController()
L
laosan_ted 已提交
5076 5077 5078 5079 5080
  
    build() {
      Column() {
        Button('setCookie')
          .onClick(() => {
Y
yamila 已提交
5081 5082
            let result = web_webview.WebCookieManager.setCookie('www.example.com', 'a=b')
            console.log("result: " + result)
L
laosan_ted 已提交
5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

将当前存在内存中的cookie以Promise方法异步保存到磁盘中。

**返回值:**

H
HelloCrease 已提交
5097 5098
| 类型                | 说明                          |
| ----------------- | --------------------------- |
L
laosan_ted 已提交
5099 5100 5101 5102 5103 5104
| Promise\<boolean> | Promise实例,用于获取cookie是否成功保存。 |

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5105
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5106 5107 5108
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5109
    controller: WebController = new WebController()
L
laosan_ted 已提交
5110 5111 5112 5113 5114
  
    build() {
      Column() {
        Button('saveCookieAsync')
          .onClick(() => {
L
laosan_ted 已提交
5115
            web_webview.WebCookieManager.saveCookieAsync()
L
laosan_ted 已提交
5116
              .then (function(result) {
Y
yamila 已提交
5117
                console.log("result: " + result)
L
laosan_ted 已提交
5118 5119
              })
              .catch(function(error) {
Y
yamila 已提交
5120 5121
                console.error("error: " + error)
              })
L
laosan_ted 已提交
5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

将当前存在内存中的cookie异步保存到磁盘中。

**参数:**

H
HelloCrease 已提交
5136 5137 5138
| 参数名      | 参数类型                    | 必填   | 默认值  | 参数描述                         |
| -------- | ----------------------- | ---- | ---- | ---------------------------- |
| callback | AsyncCallback\<boolean> | 是    | -    | 返回cookie是否成功保存的布尔值作为回调函数的入参。 |
L
laosan_ted 已提交
5139 5140 5141 5142 5143

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5144
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5145 5146 5147
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5148
    controller: WebController = new WebController()
L
laosan_ted 已提交
5149 5150 5151 5152 5153
  
    build() {
      Column() {
        Button('saveCookieAsync')
          .onClick(() => {
L
laosan_ted 已提交
5154
            web_webview.WebCookieManager.saveCookieAsync(function(result) {
Y
yamila 已提交
5155 5156
              console.log("result: " + result)
            })
L
laosan_ted 已提交
5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

获取WebCookieManager实例是否拥有发送和接收cookie的权限。

**返回值:**

H
HelloCrease 已提交
5171 5172
| 类型      | 说明                  |
| ------- | ------------------- |
L
laosan_ted 已提交
5173 5174 5175 5176 5177 5178
| boolean | 是否拥有发送和接收cookie的权限。 |

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5179
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5180 5181 5182
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5183
    controller: WebController = new WebController()
L
laosan_ted 已提交
5184 5185 5186 5187 5188
  
    build() {
      Column() {
        Button('isCookieAllowed')
          .onClick(() => {
Y
yamila 已提交
5189 5190
            let result = web_webview.WebCookieManager.isCookieAllowed()
            console.log("result: " + result)
L
laosan_ted 已提交
5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

设置WebCookieManager实例是否拥有发送和接收cookie的权限。

**参数:**

H
HelloCrease 已提交
5205 5206 5207
| 参数名    | 参数类型    | 必填   | 默认值  | 参数描述                  |
| ------ | ------- | ---- | ---- | --------------------- |
| accept | boolean | 是    | -    | 设置是否拥有发送和接收cookie的权限。 |
L
laosan_ted 已提交
5208 5209 5210 5211 5212

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5213
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5214 5215 5216
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5217
    controller: WebController = new WebController()
L
laosan_ted 已提交
5218 5219 5220 5221 5222
  
    build() {
      Column() {
        Button('putAcceptCookieEnabled')
          .onClick(() => {
Y
yamila 已提交
5223
            web_webview.WebCookieManager.putAcceptCookieEnabled(false)
L
laosan_ted 已提交
5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

获取WebCookieManager实例是否拥有发送和接收第三方cookie的权限。

**返回值:**

H
HelloCrease 已提交
5238 5239
| 类型      | 说明                     |
| ------- | ---------------------- |
L
laosan_ted 已提交
5240 5241 5242 5243 5244 5245
| boolean | 是否拥有发送和接收第三方cookie的权限。 |

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5246
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5247 5248 5249
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5250
    controller: WebController = new WebController()
L
laosan_ted 已提交
5251 5252 5253 5254 5255
  
    build() {
      Column() {
        Button('isThirdPartyCookieAllowed')
          .onClick(() => {
Y
yamila 已提交
5256 5257
            let result = web_webview.WebCookieManager.isThirdPartyCookieAllowed()
            console.log("result: " + result)
L
laosan_ted 已提交
5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

设置WebCookieManager实例是否拥有发送和接收第三方cookie的权限。

**参数:**

H
HelloCrease 已提交
5272 5273 5274
| 参数名    | 参数类型    | 必填   | 默认值  | 参数描述                     |
| ------ | ------- | ---- | ---- | ------------------------ |
| accept | boolean | 是    | -    | 设置是否拥有发送和接收第三方cookie的权限。 |
L
laosan_ted 已提交
5275 5276 5277 5278 5279

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5280
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5281 5282 5283
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5284
    controller: WebController = new WebController()
L
laosan_ted 已提交
5285 5286 5287 5288 5289
  
    build() {
      Column() {
        Button('putAcceptThirdPartyCookieEnabled')
          .onClick(() => {
Y
yamila 已提交
5290
            web_webview.WebCookieManager.putAcceptThirdPartyCookieEnabled(false)
L
laosan_ted 已提交
5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

获取是否存在cookie。

**返回值:**

H
HelloCrease 已提交
5305 5306
| 类型      | 说明          |
| ------- | ----------- |
L
laosan_ted 已提交
5307 5308 5309 5310 5311 5312
| boolean | 是否存在cookie。 |

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5313
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5314 5315 5316
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5317
    controller: WebController = new WebController()
L
laosan_ted 已提交
5318 5319 5320 5321 5322
  
    build() {
      Column() {
        Button('existCookie')
          .onClick(() => {
Y
yamila 已提交
5323 5324
            let result = web_webview.WebCookieManager.existCookie()
            console.log("result: " + result)
L
laosan_ted 已提交
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

清除所有cookie。

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5341
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5342 5343 5344
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5345
    controller: WebController = new WebController()
L
laosan_ted 已提交
5346 5347 5348 5349 5350
  
    build() {
      Column() {
        Button('deleteEntireCookie')
          .onClick(() => {
Y
yamila 已提交
5351
            web_webview.WebCookieManager.deleteEntireCookie()
L
laosan_ted 已提交
5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

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

清除所有会话cookie。

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
5368
  import web_webview from '@ohos.web.webview'
L
laosan_ted 已提交
5369 5370 5371
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5372
    controller: WebController = new WebController()
L
laosan_ted 已提交
5373 5374 5375 5376 5377
  
    build() {
      Column() {
        Button('deleteSessionCookie')
          .onClick(() => {
L
laosan_ted 已提交
5378
            web_webview.WebCookieManager.deleteSessionCookie()
L
laosan_ted 已提交
5379 5380 5381 5382 5383 5384 5385
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

L
laosan_ted 已提交
5386
## MessageLevel枚举说明
5387

L
laosan_ted 已提交
5388 5389 5390 5391 5392 5393 5394
| 名称    | 描述    |
| ----- | :---- |
| Debug | 调试级别。 |
| Error | 错误级别。 |
| Info  | 消息级别。 |
| Log   | 日志级别。 |
| Warn  | 警告级别。 |
5395

L
laosan_ted 已提交
5396
## RenderExitReason枚举说明
5397

L
laosan_ted 已提交
5398
onRenderExited接口返回的渲染进程退出的具体原因。
5399

L
laosan_ted 已提交
5400 5401 5402 5403 5404 5405 5406
| 名称                         | 描述                |
| -------------------------- | ----------------- |
| ProcessAbnormalTermination | 渲染进程异常退出。         |
| ProcessWasKilled           | 收到SIGKILL,或被手动终止。 |
| ProcessCrashed             | 渲染进程崩溃退出,如段错误。    |
| ProcessOom                 | 程序内存不足。           |
| ProcessExitUnknown         | 其他原因。             |
5407

L
laosan_ted 已提交
5408
## MixedMode枚举说明
5409

L
laosan_ted 已提交
5410 5411 5412 5413 5414
| 名称         | 描述                                 |
| ---------- | ---------------------------------- |
| All        | 允许加载HTTP和HTTPS混合内容。所有不安全的内容都可以被加载。 |
| Compatible | 混合内容兼容性模式,部分不安全的内容可能被加载。           |
| None       | 不允许加载HTTP和HTTPS混合内容。               |
5415

L
laosan_ted 已提交
5416 5417 5418 5419 5420 5421 5422
## CacheMode枚举说明
| 名称      | 描述                                   |
| ------- | ------------------------------------ |
| Default | 使用未过期的cache加载资源,如果cache中无该资源则从网络中获取。 |
| None    | 加载资源使用cache,如果cache中无该资源则从网络中获取。     |
| Online  | 加载资源不使用cache,全部从网络中获取。               |
| Only    | 只从cache中加载资源。                        |
5423

L
laosan_ted 已提交
5424 5425 5426 5427 5428 5429 5430
## FileSelectorMode枚举说明
| 名称                   | 描述         |
| -------------------- | ---------- |
| FileOpenMode         | 打开上传单个文件。  |
| FileOpenMultipleMode | 打开上传多个文件。  |
| FileOpenFolderMode   | 打开上传文件夹模式。 |
| FileSaveMode         | 文件保存模式。    |
5431

L
laosan_ted 已提交
5432
 ## HitTestType枚举说明
5433

L
laosan_ted 已提交
5434 5435 5436 5437 5438 5439 5440 5441 5442 5443
| 名称            | 描述                       |
| ------------- | ------------------------ |
| EditText      | 可编辑的区域。                  |
| Email         | 电子邮件地址。                  |
| HttpAnchor    | 超链接,其src为http。           |
| HttpAnchorImg | 带有超链接的图片,其中超链接的src为http。 |
| Img           | HTML::img标签。             |
| Map           | 地理地址。                    |
| Phone         | 电话号码。                    |
| Unknown       | 未知内容。                    |
5444

L
laosan_ted 已提交
5445
## SslError<sup>9+</sup>枚举说明
5446

L
laosan_ted 已提交
5447
onSslErrorEventReceive接口返回的SSL错误的具体原因。
5448

L
laosan_ted 已提交
5449 5450 5451 5452 5453 5454
| 名称           | 描述          |
| ------------ | ----------- |
| Invalid      | 一般错误。       |
| HostMismatch | 主机名不匹配。     |
| DateInvalid  | 证书日期无效。     |
| Untrusted    | 证书颁发机构不受信任。 |
5455

L
laosan_ted 已提交
5456
## ProtectedResourceType<sup>9+</sup>枚举说明
5457

L
laosan_ted 已提交
5458 5459 5460
| 名称        | 描述            | 备注                         |
| --------- | ------------- | -------------------------- |
| MidiSysex | MIDI SYSEX资源。 | 目前仅支持权限事件上报,MIDI设备的使用还未支持。 |
5461

Y
yuhaoge 已提交
5462 5463 5464 5465 5466 5467 5468
## WebDarkMode<sup>9+</sup>枚举说明
| 名称      | 描述                                   |
| ------- | ------------------------------------ |
| Off     | Web深色模式关闭。                     |
| On      | Web深色模式开启。                     |
| Auto    | Web深色模式跟随系统。                 |

5469 5470 5471
## WebMessagePort<sup>9+</sup>

通过WebMessagePort可以进行消息的发送以及接收。
5472

E
echoorchid 已提交
5473
### close<sup>9+</sup>
5474 5475
close(): void

E
echoorchid 已提交
5476
关闭该消息端口。
5477

E
echoorchid 已提交
5478
### postMessageEvent<sup>9+</sup>
5479 5480
postMessageEvent(message: WebMessageEvent): void

E
echoorchid 已提交
5481
发送消息。完整示例代码参考[postMessage](#postmessage9)
5482 5483

**参数:**
5484

H
HelloCrease 已提交
5485 5486 5487
| 参数名     | 参数类型                                 | 必填   | 默认值  | 参数描述    |
| ------- | ------------------------------------ | ---- | ---- | ------- |
| message | [WebMessageEvent](#webmessageevent9) | 是    | -    | 要发送的消息。 |
5488 5489

**示例:**
L
laosan_ted 已提交
5490

5491 5492 5493 5494 5495
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5496 5497
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
5498 5499 5500 5501 5502

    build() {
      Column() {
        Button('postMessageEvent')
          .onClick(() => {
Y
yamila 已提交
5503 5504 5505
            var msg = new WebMessageEvent()
            msg.setData("post message from ets to html5")
            this.ports[0].postMessageEvent(msg)
5506 5507 5508 5509 5510 5511 5512
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

E
echoorchid 已提交
5513
### onMessageEvent<sup>9+</sup>
5514 5515
onMessageEvent(callback: (result: string) => void): void

E
echoorchid 已提交
5516
注册回调函数,接收HTML5侧发送过来的消息。完整示例代码参考[postMessage](#postmessage9)
5517 5518

**参数:**
5519

H
HelloCrease 已提交
5520 5521 5522
| 参数名      | 参数类型     | 必填   | 默认值  | 参数描述       |
| -------- | -------- | ---- | ---- | ---------- |
| callback | function | 是    | -    | 接收消息的回调函数。 |
5523 5524

**示例:**
L
laosan_ted 已提交
5525

5526 5527 5528 5529 5530
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5531 5532
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
5533 5534 5535 5536 5537

    build() {
      Column() {
        Button('onMessageEvent')
          .onClick(() => {
L
laosan_ted 已提交
5538
            this.ports[0].onMessageEvent((result: string) => {
E
echoorchid 已提交
5539
              console.log("received message from html5, on message:" + result);
5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```


## WebMessageEvent<sup>9+</sup>

通过WebMessagePort对要发送的消息和端口进行封装。

E
echoorchid 已提交
5553
### getData<sup>9+</sup>
5554 5555 5556 5557 5558
getData(): string

获取当前对象中存放的消息。

**返回值:**
5559

H
HelloCrease 已提交
5560 5561
| 类型     | 说明             |
| ------ | -------------- |
5562 5563
| string | 当前该类型对象中存放的消息。 |

E
echoorchid 已提交
5564
**示例:**
L
laosan_ted 已提交
5565

E
echoorchid 已提交
5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    build() {
      Column() {
        Button('getPorts')
          .onClick(() => {
            var msgEvent = new WebMessageEvent();
Y
yamila 已提交
5576 5577 5578
            msgEvent.setData("message event data")
            var messageData = msgEvent.getData()
            console.log("message is:" + messageData)
E
echoorchid 已提交
5579 5580 5581 5582 5583
          })
      }
    }
  }
  ```
5584

E
echoorchid 已提交
5585
### setData<sup>9+</sup>
5586 5587
setData(data: string): void

E
echoorchid 已提交
5588
设置当前对象中的消息。完整示例代码参考[postMessage](#postmessage9)
5589 5590

**参数:**
5591

H
HelloCrease 已提交
5592 5593 5594
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述    |
| ---- | ------ | ---- | ---- | ------- |
| data | string | 是    | -    | 要发送的消息。 |
5595 5596

**示例:**
L
laosan_ted 已提交
5597

5598 5599 5600 5601 5602
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5603 5604
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
5605 5606 5607 5608 5609

    build() {
      Column() {
        Button('setData')
          .onClick(() => {
Y
yamila 已提交
5610 5611 5612
            var msg = new WebMessageEvent()
            msg.setData("post message from ets to HTML5")
            this.ports[0].postMessageEvent(msg)
5613 5614 5615 5616 5617 5618
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
E
echoorchid 已提交
5619
### getPorts<sup>9+</sup>
E
echoorchid 已提交
5620
getPorts(): Array\<WebMessagePort\>
5621 5622 5623 5624

获取当前对象中存放的消息端口。

**返回值:**
5625

H
HelloCrease 已提交
5626 5627
| 类型                                       | 说明               |
| ---------------------------------------- | ---------------- |
E
echoorchid 已提交
5628
| Array\<[WebMessagePort](#webmessageport9)\> | 当前该类型对象中存放的消息端口。 |
5629

E
echoorchid 已提交
5630
**示例:**
L
laosan_ted 已提交
5631

E
echoorchid 已提交
5632 5633 5634 5635 5636
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5637
    ports: WebMessagePort[] = null
E
echoorchid 已提交
5638 5639 5640 5641
    build() {
      Column() {
        Button('getPorts')
          .onClick(() => {
Y
yamila 已提交
5642 5643 5644 5645 5646
            var sendPortArray = new Array(this.ports[0])
            var msgEvent = new WebMessageEvent()
            msgEvent.setPorts(sendPortArray)
            var getPorts = msgEvent.getPorts()
            console.log("Ports is:" + getPorts)
E
echoorchid 已提交
5647 5648 5649 5650 5651
          })
      }
    }
  }
  ```
5652

E
echoorchid 已提交
5653
### setPorts<sup>9+</sup>
E
echoorchid 已提交
5654
setPorts(ports: Array\<WebMessagePort\>): void
5655

E
echoorchid 已提交
5656
设置当前对象中的消息端口。完整示例代码参考[postMessage](#postmessage9)
5657 5658

**参数:**
5659

H
HelloCrease 已提交
5660 5661 5662
| 参数名   | 参数类型                                     | 必填   | 默认值  | 参数描述      |
| ----- | ---------------------------------------- | ---- | ---- | --------- |
| ports | Array\<[WebMessagePort](#webmessageport9)\> | 是    | -    | 要发送的消息端口。 |
5663 5664

**示例:**
L
laosan_ted 已提交
5665

5666 5667 5668 5669 5670
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
5671 5672
    controller: WebController = new WebController()
    ports: WebMessagePort[] = null
L
laosan_ted 已提交
5673
  
5674 5675 5676 5677
    build() {
      Column() {
        Button('setPorts')
          .onClick(() => {
Y
yamila 已提交
5678 5679 5680 5681 5682
            var sendPortArray = new Array(this.ports[1])
            var msgEvent = new WebMessageEvent()
            msgEvent.setData("__init_ports__")
            msgEvent.setPorts(sendPortArray)
            this.controller.postMessage({message: msgEvent, uri: "*"})
5683 5684 5685 5686 5687
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
C
chensi10 已提交
5688 5689 5690 5691
  ```

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

L
laosan_ted 已提交
5692
通过DataResubmissionHandler可以重新提交表单数据或取消提交表单数据。
C
chensi10 已提交
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711

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

resend(): void

重新发送表单数据。

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
5712
         .onDataResubmitted((event) => {
C
chensi10 已提交
5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

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

cancel(): void

取消重新发送表单数据。

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
5739
         .onDataResubmitted((event) => {
C
chensi10 已提交
5740 5741 5742 5743 5744 5745
          console.log('onDataResubmitted')
          event.handler.cancel();
        })
      }
    }
  }
C
chensi10 已提交
5746
  ```