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

H
geshi  
HelloCrease 已提交
3
> **说明:** 
4
> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5

L
lixingchi1 已提交
6
提供具有网页显示能力的Web组件。
Z
zengyawen 已提交
7

L
liwenzhen 已提交
8
## 权限列表
Z
zhou-liting125 已提交
9
访问在线网页时需添加网络权限:ohos.permission.INTERNET。
L
liwenzhen 已提交
10

Z
zengyawen 已提交
11 12 13 14 15 16
## 子组件



## 接口

Z
zhou-liting125 已提交
17
Web(options: { src: string, controller?: WebController })
Z
zhou-liting125 已提交
18 19

**参数:**
Z
zhou-liting125 已提交
20 21 22 23
  | 参数名     | 参数类型                        | 必填 | 默认值 | 参数描述       |
  | ---------- | ------------------------------- | ---- | ------ | -------------- |
  | src        | string                          | 是   | -      | 网页资源地址。 |
  | controller | [WebController](#webcontroller) | 否   | -      | 控制器。       |
Z
zengyawen 已提交
24

Z
zhou-liting125 已提交
25
**示例:**
Z
zhou-liting125 已提交
26 27
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
28
  @Entry
L
update  
laosan_ted 已提交
29 30 31 32 33
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
34
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
35 36 37 38
      }
    }
  }
  ```
Z
zengyawen 已提交
39

H
geshi  
HelloCrease 已提交
40
> **说明:**
Z
zengyawen 已提交
41
>
Z
zhou-liting125 已提交
42
> 不支持转场动画。
Z
zengyawen 已提交
43

L
update  
laosan_ted 已提交
44
## 属性
Z
zhou-liting125 已提交
45 46 47 48 49 50 51 52 53

web组件的网络属性。

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

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

Z
zhou-liting125 已提交
54
**参数:**
Z
zhou-liting125 已提交
55 56 57 58
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | domStorageAccess    | boolean   | 是   | -      | 设置是否开启文档对象模型存储接口(DOM Storage API)权限。 |

Z
zhou-liting125 已提交
59
**示例:**
Z
zhou-liting125 已提交
60 61
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
62
  @Entry
L
update  
laosan_ted 已提交
63 64 65 66 67
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
68
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
69 70 71 72 73 74
        .domStorageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
75 76 77 78 79 80
### fileAccess

fileAccess(fileAccess: boolean)

设置是否开启通过[$rawfile(filepath/filename)](../../ui/ts-application-resource-access.md#资源引用)访问应用中rawfile路径的文件, 默认启用。

Z
zhou-liting125 已提交
81
**参数:**
Z
zhou-liting125 已提交
82 83 84 85
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | fileAccess    | boolean   | 是   | -      | 设置是否开启通过[$rawfile(filepath/filename)](../../ui/ts-application-resource-access.md#资源引用)访问应用中rawfile路径的文件。 |

Z
zhou-liting125 已提交
86
**示例:**
Z
zhou-liting125 已提交
87 88
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
89
  @Entry
L
update  
laosan_ted 已提交
90 91 92 93 94
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
95
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
96 97 98 99 100 101
        .fileAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
102 103 104 105 106 107
### fileFromUrlAccess<sup>9+</sup>

fileFromUrlAccess(fileFromUrlAccess: boolean)

设置是否允许通过网页中的JavaScript脚本访问[$rawfile(filepath/filename)](../../ui/ts-application-resource-access.md#资源引用)的内容,默认未启用。

Z
zhou-liting125 已提交
108
**参数:**
Z
zhou-liting125 已提交
109 110 111 112
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | fileFromUrlAccess    | boolean   | 是   | -      | 设置是否允许通过网页中的JavaScript脚本访问[$rawfile(filepath/filename)](../../ui/ts-application-resource-access.md#资源引用)的内容。 |

Z
zhou-liting125 已提交
113
**示例:**
Z
zhou-liting125 已提交
114 115
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
116
  @Entry
L
update  
laosan_ted 已提交
117 118 119 120 121
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
122
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
123 124 125 126 127 128
        .fileFromUrlAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
129 130 131 132 133 134
### imageAccess

imageAccess(imageAccess: boolean)

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

Z
zhou-liting125 已提交
135
**参数:**
Z
zhou-liting125 已提交
136 137 138 139
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | imageAccess    | boolean   | 是   | -      | 设置是否允许自动加载图片资源。 |

Z
zhou-liting125 已提交
140
**示例:**
Z
zhou-liting125 已提交
141 142
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
143
  @Entry
L
update  
laosan_ted 已提交
144 145 146 147 148
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
149
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
150 151 152 153 154 155
        .imageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
156 157 158 159 160 161 162
### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
    controller: WebController })

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

Z
zhou-liting125 已提交
163
**参数:**
Z
zhou-liting125 已提交
164 165 166 167 168 169
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | object    | object   | 是   | -      | 参与注册的对象。只能声明方法,不能声明属性。 |
  | name    | string   | 是   | -      | 注册对象的名称,与window中调用的对象名一致。 |
  | methodList    | Array\<string\>   | 是   | -      | 参与注册的应用侧JavaScript对象的方法。|
 
Z
zhou-liting125 已提交
170
**示例:**
Z
zhou-liting125 已提交
171 172
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
173
  @Entry
L
update  
laosan_ted 已提交
174 175 176 177 178 179 180 181 182 183 184
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    testObj = {
      test: (data1, data2, data3) => {
        console.log("data1:" + data1);
        console.log("data2:" + data2);
        console.log("data3:" + data3);
        return "AceString";
      },
      toString: () => {
Z
zhou-liting125 已提交
185
        console.log('toString' + "interface instead.");
L
update  
laosan_ted 已提交
186 187 188 189
      }
    }
    build() {
      Column() {
Z
zhou-liting125 已提交
190
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
191
        .javaScriptAccess(true)
L
update  
laosan_ted 已提交
192
        .javaScriptProxy({
L
laosan_ted 已提交
193
          object: this.testObj,
L
update  
laosan_ted 已提交
194 195 196 197 198 199 200 201 202
          name: "objName",
          methodList: ["test", "toString"],
          controller: this.controller,
        })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
203 204 205 206 207 208
### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

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

Z
zhou-liting125 已提交
209
**参数:**
Z
zhou-liting125 已提交
210 211 212 213
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | javaScriptAccess    | boolean   | 是   | -      | 是否允许执行JavaScript脚本。 |

Z
zhou-liting125 已提交
214
**示例:**
Z
zhou-liting125 已提交
215 216
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
217
  @Entry
L
update  
laosan_ted 已提交
218 219 220 221 222
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
223
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
224 225 226 227 228 229
        .javaScriptAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
230 231 232 233 234 235
### mixedMode

mixedMode(mixedMode: MixedMode)

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

Z
zhou-liting125 已提交
236
**参数:**
Z
zhou-liting125 已提交
237 238 239 240
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | mixedMode    | [MixedMode](#mixedmode枚举说明)   | 是   | -      | 要设置的混合内容。 |

Z
zhou-liting125 已提交
241
**示例:**
Z
zhou-liting125 已提交
242 243
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
244
  @Entry
L
update  
laosan_ted 已提交
245 246 247
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
248
    @State mode:MixedMode = MixedMode.All;
L
update  
laosan_ted 已提交
249 250
    build() {
      Column() {
Z
zhou-liting125 已提交
251
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
252
        .mixedMode(this.mode)
L
update  
laosan_ted 已提交
253 254 255 256 257
      }
    }
  }
  ```

Z
zhou-liting125 已提交
258 259 260 261 262 263
### onlineImageAccess

onlineImageAccess(onlineImageAccess: boolean)

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

Z
zhou-liting125 已提交
264
**参数:**
Z
zhou-liting125 已提交
265 266 267 268
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | onlineImageAccess    | boolean   | 是   | -      | 设置是否允许从网络加载图片资源。 |

Z
zhou-liting125 已提交
269
**示例:**
Z
zhou-liting125 已提交
270 271
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
272
  @Entry
L
update  
laosan_ted 已提交
273 274 275 276 277
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
278
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
279 280 281 282 283 284
        .onlineImageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
285 286 287 288 289 290
### zoomAccess

zoomAccess(zoomAccess: boolean)

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

Z
zhou-liting125 已提交
291
**参数:**
Z
zhou-liting125 已提交
292 293 294 295
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | zoomAccess    | boolean   | 是   | -      | 设置是否支持手势进行缩放。 |

Z
zhou-liting125 已提交
296
**示例:**
Z
zhou-liting125 已提交
297 298
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
299
  @Entry
L
update  
laosan_ted 已提交
300 301 302 303 304
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
305
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
306 307 308 309 310 311
        .zoomAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
312 313 314 315 316 317
### overviewModeAccess

overviewModeAccess(overviewModeAccess: boolean)

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

Z
zhou-liting125 已提交
318
**参数:**
Z
zhou-liting125 已提交
319 320 321 322
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | overviewModeAccess    | boolean   | 是   | -      | 设置是否使用概览模式加载网页。 |

Z
zhou-liting125 已提交
323
**示例:**
Z
zhou-liting125 已提交
324 325
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
326
  @Entry
L
update  
laosan_ted 已提交
327 328 329 330 331
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
332
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
333 334 335 336 337 338
        .overviewModeAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
339 340 341 342 343 344
### databaseAccess

databaseAccess(databaseAccess: boolean)

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

Z
zhou-liting125 已提交
345
**参数:**
Z
zhou-liting125 已提交
346 347 348 349
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | databaseAccess    | boolean   | 是   | -      | 设置是否开启数据库存储API权限。 |

Z
zhou-liting125 已提交
350 351
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
352
  @Entry
L
update  
laosan_ted 已提交
353 354 355 356 357
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
358
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
359 360 361 362 363 364
        .databaseAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
365 366 367 368 369 370
### cacheMode

cacheMode(cacheMode: CacheMode)

设置缓存模式。

Z
zhou-liting125 已提交
371
**参数:**
Z
zhou-liting125 已提交
372 373 374 375
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | cacheMode    | [CacheMode](#cachemode枚举说明)   | 是   | -      | 要设置的缓存模式。 |

Z
zhou-liting125 已提交
376
**示例:**
Z
zhou-liting125 已提交
377 378
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
379
  @Entry
L
update  
laosan_ted 已提交
380 381 382
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
383
    @State mode:CacheMode = CacheMode.None;
L
update  
laosan_ted 已提交
384 385
    build() {
      Column() {
Z
zhou-liting125 已提交
386
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
387
        .cacheMode(this.mode)
L
update  
laosan_ted 已提交
388 389 390 391 392
      }
    }
  }
  ```

Z
zhou-liting125 已提交
393 394 395 396 397 398
### textZoomAtio

textZoomAtio(textZoomAtio: number)

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

Z
zhou-liting125 已提交
399
**参数:**
Z
zhou-liting125 已提交
400 401 402 403
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | textZoomAtio    | number   | 是   | -      | 要设置的页面的文本缩放百分比。 |

Z
zhou-liting125 已提交
404
**示例:**
Z
zhou-liting125 已提交
405 406
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
407
  @Entry
L
update  
laosan_ted 已提交
408 409 410
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
411
    @State atio:number = 150;
L
update  
laosan_ted 已提交
412 413
    build() {
      Column() {
Z
zhou-liting125 已提交
414
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
415
        .textZoomAtio(this.atio)
L
update  
laosan_ted 已提交
416 417 418 419 420
      }
    }
  }
  ```

Z
zhou-liting125 已提交
421 422 423 424 425 426
### userAgent

userAgent(userAgent: string)

设置用户代理。

Z
zhou-liting125 已提交
427
**参数:**
Z
zhou-liting125 已提交
428 429 430
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | userAgent    | string   | 是   | -      | 要设置的用户代理。 |
Z
zengyawen 已提交
431

Z
zhou-liting125 已提交
432
**示例:**
Z
zhou-liting125 已提交
433 434
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
435
  @Entry
L
update  
laosan_ted 已提交
436 437 438
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
439
    @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 已提交
440 441
    build() {
      Column() {
Z
zhou-liting125 已提交
442
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
443
        .userAgent(this.userAgent)
L
update  
laosan_ted 已提交
444 445 446 447 448
      }
    }
  }
  ```

H
geshi  
HelloCrease 已提交
449
>  **说明:**
Z
zengyawen 已提交
450
>
451
>  通用属性仅支持[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
zengyawen 已提交
452 453 454

## 事件

L
liwenzhen 已提交
455
不支持通用事件。
Z
zengyawen 已提交
456

L
update  
laosan_ted 已提交
457 458 459 460 461 462
### onAlert

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

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

Z
zhou-liting125 已提交
463
**参数:**
L
update  
laosan_ted 已提交
464 465 466 467 468 469
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 当前显示弹窗所在网页的URL。|
  | message | string | 弹窗中显示的信息。 |
  | result | [JsResult](#jsresult) |  通知Web组件用户操作行为。|

Z
zhou-liting125 已提交
470 471
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
472 473 474
  |----------|------|
  | boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。 |

Z
zhou-liting125 已提交
475
**示例:**
Z
zhou-liting125 已提交
476 477
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
478
  @Entry
L
update  
laosan_ted 已提交
479 480 481 482 483
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
484
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
          .onAlert((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              confirm: {
                value: 'onAlert',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true;
L
update  
laosan_ted 已提交
500 501 502 503 504 505 506 507 508 509 510 511
          })
      }
    }
  }
  ```

### onBeforeUnload

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

刷新或关闭场景下,在即将离开当前页面时触发此回调。

Z
zhou-liting125 已提交
512
**参数:**
L
update  
laosan_ted 已提交
513 514 515 516 517 518
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 当前显示弹窗所在网页的URL。|
  | message | string | 弹窗中显示的信息。 |
  | result | [JsResult](#jsresult) |  通知Web组件用户操作行为。|

Z
zhou-liting125 已提交
519 520
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
521 522 523
  |----------|------|
  | boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。 |

Z
zhou-liting125 已提交
524
**示例:**
Z
zhou-liting125 已提交
525 526
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
527
  @Entry
L
update  
laosan_ted 已提交
528 529
  @Component
  struct WebComponent {
L
laosan_ted 已提交
530 531
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
532 533
    build() {
      Column() {
Z
zhou-liting125 已提交
534
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
535 536 537 538 539 540 541
          .onBeforeUnload((event) => {
            console.log("event.url:" + event.url);
            console.log("event.message:" + event.message);
            console.log("event.result:" + event.result);
            return false;
          })
      }
L
update  
laosan_ted 已提交
542 543 544 545 546 547 548 549 550 551
    }
  }
  ```

### onConfirm

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

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

Z
zhou-liting125 已提交
552
**参数:**
L
update  
laosan_ted 已提交
553 554 555 556 557 558
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 当前显示弹窗所在网页的URL。|
  | message | string | 弹窗中显示的信息。 |
  | result | [JsResult](#jsresult) |  通知Web组件用户操作行为。|

Z
zhou-liting125 已提交
559 560
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
561 562 563
  |----------|------|
  | boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。 |

Z
zhou-liting125 已提交
564
**示例:**
Z
zhou-liting125 已提交
565 566
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
567
  @Entry
L
update  
laosan_ted 已提交
568 569
  @Component
  struct WebComponent {
L
laosan_ted 已提交
570 571
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
572 573
    build() {
      Column() {
Z
zhou-liting125 已提交
574
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
          .onConfirm((event) => {
            console.log("event.url:" + event.url);
            console.log("event.message:" + event.message);
            console.log("event.result:" + event.result);
            AlertDialog.show({
              title: 'title',
              message: 'text',
              confirm: {
                value: 'onConfirm',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true;
L
update  
laosan_ted 已提交
593
          })
L
laosan_ted 已提交
594
      }
L
update  
laosan_ted 已提交
595 596 597 598 599 600 601 602
    }
  }
  ```

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

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

Z
zhou-liting125 已提交
603
**参数:**
L
update  
laosan_ted 已提交
604 605 606 607 608 609
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 当前显示弹窗所在网页的URL。|
  | message | string | 弹窗中显示的信息。 |
  | result | [JsResult](#jsresult) |  通知Web组件用户操作行为。|

Z
zhou-liting125 已提交
610 611
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
612 613 614
  |----------|------|
  | boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。 |

Z
zhou-liting125 已提交
615
**示例:**
Z
zhou-liting125 已提交
616 617
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
618
  @Entry
L
update  
laosan_ted 已提交
619 620 621 622 623
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
624
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
        .onPrompt((event) => {
          console.log("url:" + event.url);
          console.log("message:" + event.message);
          console.log("value:" + event.value);
          console.log("result:" + event.result);
          return true;
        })
    }
  }
  ```

### onConsole

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

通知宿主应用JavaScript console消息。

Z
zhou-liting125 已提交
642
**参数:**
L
update  
laosan_ted 已提交
643 644 645 646
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | message | [ConsoleMessage](#consolemessage) | 触发的控制台信息。 |

Z
zhou-liting125 已提交
647 648
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
649 650 651
  |----------|------|
  | boolean | 当返回true时,该条消息将不会再打印至控制台,反之仍会打印至控制台。 |

Z
zhou-liting125 已提交
652
**示例:**
Z
zhou-liting125 已提交
653 654
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
655
  @Entry
L
update  
laosan_ted 已提交
656 657
  @Component
  struct WebComponent {
L
laosan_ted 已提交
658 659
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
660 661
    build() {
      Column() {
Z
zhou-liting125 已提交
662
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
663 664 665 666 667 668 669 670
          .onConsole((event) => {
            console.log('getMessage:' + event.message.getMessage());
            console.log('getSourceId:' + event.message.getSourceId());
            console.log('getLineNumber:' + event.message.getLineNumber());
            console.log('getMessageLevel:' + event.message.getMessageLevel());
            return false;
          })
      }
L
update  
laosan_ted 已提交
671 672 673 674 675 676 677 678
    }
  }
  ```

### onDownloadStart

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

Z
zhou-liting125 已提交
679
**参数:**
L
update  
laosan_ted 已提交
680 681 682 683 684 685 686
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 文件下载的URL。 |
  | contentDisposition | string | 服务器返回的 Content-Disposition响应头,可能为空。 |
  | mimetype | string | 服务器返回内容媒体类型(MIME)信息。 |
  | contentLength | contentLength | 服务器返回文件的长度。 |

Z
zhou-liting125 已提交
687
**示例:**
Z
zhou-liting125 已提交
688 689
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
690
  @Entry
L
update  
laosan_ted 已提交
691 692
  @Component
  struct WebComponent {
L
laosan_ted 已提交
693 694
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
695 696
    build() {
      Column() {
Z
zhou-liting125 已提交
697
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
698 699 700 701 702 703 704 705
          .onDownloadStart((event) => {
            console.log('url:' + event.url);
            console.log('userAgent:' + event.userAgent);
            console.log('contentDisposition:' + event.contentDisposition);
            console.log('contentLength:' + event.contentLength);
            console.log('mimetype:' + event.mimetype);
          })
      }
L
update  
laosan_ted 已提交
706 707 708 709 710 711 712 713 714 715
    }
  }
  ```

### onErrorReceive

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

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

Z
zhou-liting125 已提交
716
**参数:**
L
update  
laosan_ted 已提交
717 718 719 720 721
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。 |
  | error | [WebResourceError](#webresourceerror) | 网页加载资源错误的封装信息 。 |

Z
zhou-liting125 已提交
722
**示例:**
Z
zhou-liting125 已提交
723 724
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
725
  @Entry
L
update  
laosan_ted 已提交
726 727
  @Component
  struct WebComponent {
L
laosan_ted 已提交
728 729
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
730 731
    build() {
      Column() {
Z
zhou-liting125 已提交
732
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
          .onErrorReceive((event) => {
            console.log('getErrorInfo:' + event.error.getErrorInfo());
            console.log('getErrorCode:' + event.error.getErrorCode());
            console.log('url:' + event.request.getRequestUrl());
            console.log('isMainFrame:' + event.request.isMainFrame());
            console.log('isRedirect:' + event.request.isRedirect());
            console.log('isRequestGesture:' + event.request.isRequestGesture());
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString());
            let result = event.request.getRequestHeader();
            console.log('The request header result size is ' + result.length);
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue);
            }
          })
      }
L
update  
laosan_ted 已提交
748 749 750 751 752 753 754 755 756 757
    }
  }
  ```

### onHttpErrorReceive

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

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

Z
zhou-liting125 已提交
758
**参数:**
L
update  
laosan_ted 已提交
759 760 761 762 763
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。 |
  | error | [WebResourceError](#webresourceerror) | 网页加载资源错误的封装信息 。 |

Z
zhou-liting125 已提交
764
**示例:**
Z
zhou-liting125 已提交
765 766
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
767
  @Entry
L
update  
laosan_ted 已提交
768 769
  @Component
  struct WebComponent {
L
laosan_ted 已提交
770 771
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
772 773
    build() {
      Column() {
Z
zhou-liting125 已提交
774
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
          .onHttpErrorReceive((event) => {
            console.log('url:' + event.request.getRequestUrl());
            console.log('isMainFrame:' + event.request.isMainFrame());
            console.log('isRedirect:' + event.request.isRedirect());
            console.log('isRequestGesture:' + event.request.isRequestGesture());
            console.log('getResponseData:' + event.response.getResponseData());
            console.log('getResponseEncoding:' + event.response.getResponseEncoding());
            console.log('getResponseMimeType:' + event.response.getResponseMimeType());
            console.log('getResponseCode:' + event.response.getResponseCode());
            console.log('getReasonMessage:' + event.response.getReasonMessage());
            let result = event.request.getRequestHeader();
            console.log('The request header result size is ' + result.length);
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue);
            }
            let resph = event.response.getResponseHeader();
            console.log('The response header result size is ' + resph.length);
            for (let i of resph) {
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue);
            }
          })
      }
L
update  
laosan_ted 已提交
797 798 799 800 801 802 803 804 805 806 807
    }
  }
  ```

### onPageBegin

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

	
网页开始加载时触发该回调,且只在主frame触发,iframe或者frameset的内容加载时不会触发此回调。

Z
zhou-liting125 已提交
808
**参数:**
L
update  
laosan_ted 已提交
809 810 811 812
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 页面的URL地址。 |

Z
zhou-liting125 已提交
813
**示例:**
Z
zhou-liting125 已提交
814 815
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
816
  @Entry
L
update  
laosan_ted 已提交
817 818
  @Component
  struct WebComponent {
L
laosan_ted 已提交
819 820
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
821 822
    build() {
      Column() {
Z
zhou-liting125 已提交
823
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
824 825 826 827
          .onPageBegin((event) => {
            console.log('url:' + event.url);
          })
      }
L
update  
laosan_ted 已提交
828 829 830 831 832 833 834 835 836 837 838
    }
  }
  ```

### onPageEnd

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

	
网页加载完成时触发该回调,且只在主frame触发。

Z
zhou-liting125 已提交
839
**参数:**
L
update  
laosan_ted 已提交
840 841 842 843
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 页面的URL地址。 |

Z
zhou-liting125 已提交
844
**示例:**
Z
zhou-liting125 已提交
845 846
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
847
  @Entry
L
update  
laosan_ted 已提交
848 849
  @Component
  struct WebComponent {
L
laosan_ted 已提交
850 851
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
852 853
    build() {
      Column() {
Z
zhou-liting125 已提交
854
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
855 856 857 858
          .onPageEnd((event) => {
            console.log('url:' + event.url);
          })
      }
L
update  
laosan_ted 已提交
859 860 861 862 863 864 865 866 867 868
    }
  }
  ```

### onProgressChange

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

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

Z
zhou-liting125 已提交
869
**参数:**
L
update  
laosan_ted 已提交
870 871 872 873
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | newProgress | number | 新的加载进度,取值范围为0到100的整数。 |

Z
zhou-liting125 已提交
874
  **示例:**
Z
zhou-liting125 已提交
875 876
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
877
  @Entry
L
update  
laosan_ted 已提交
878 879
  @Component
  struct WebComponent {
L
laosan_ted 已提交
880 881
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
882 883
    build() {
      Column() {
Z
zhou-liting125 已提交
884
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
885 886 887 888
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
L
update  
laosan_ted 已提交
889 890 891 892 893 894 895 896 897 898
    }
  }
  ```

### onTitleReceive

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

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

Z
zhou-liting125 已提交
899
**参数:**
L
update  
laosan_ted 已提交
900 901 902 903
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | title | string | document标题内容。 |

Z
zhou-liting125 已提交
904
  **示例:**
Z
zhou-liting125 已提交
905 906
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
907
  @Entry
L
update  
laosan_ted 已提交
908 909
  @Component
  struct WebComponent {
L
laosan_ted 已提交
910 911
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
912 913
    build() {
      Column() {
Z
zhou-liting125 已提交
914
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
915 916 917 918
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
L
update  
laosan_ted 已提交
919 920 921 922 923 924 925 926 927 928
    }
  }
  ```

### onRefreshAccessedHistory

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

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

Z
zhou-liting125 已提交
929
**参数:**
L
update  
laosan_ted 已提交
930 931 932 933 934
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | url | string | 访问的url。 |
  | isRefreshed | boolean | true表示该页面是被重新加载的,false表示该页面是新加载的。 |

Z
zhou-liting125 已提交
935
  **示例:**
Z
zhou-liting125 已提交
936 937
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
938
  @Entry
L
update  
laosan_ted 已提交
939 940
  @Component
  struct WebComponent {
L
laosan_ted 已提交
941 942
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
943 944
    build() {
      Column() {
Z
zhou-liting125 已提交
945
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
946 947 948 949
          .onRefreshAccessedHistory((event) => {
            console.log('url:' + event.url + ' isReload:' + event.refreshed);
          })
      }
L
update  
laosan_ted 已提交
950 951 952 953 954 955 956 957 958 959
    }
  }
  ```

### onRenderExited

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

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

Z
zhou-liting125 已提交
960
**参数:**
L
update  
laosan_ted 已提交
961 962 963 964
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | renderExitReason | [RenderExitReason](#renderexitreason枚举说明) | 渲染进程进程异常退出的具体原因。 |

Z
zhou-liting125 已提交
965
  **示例:**
Z
zhou-liting125 已提交
966 967
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
968
  @Entry
L
update  
laosan_ted 已提交
969 970
  @Component
  struct WebComponent {
L
laosan_ted 已提交
971 972
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
973 974
    build() {
      Column() {
L
laosan_ted 已提交
975 976 977 978 979
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
            console.log('reason:' + event.renderExitReason);
          })
      }
L
update  
laosan_ted 已提交
980 981 982 983 984 985 986 987
    }
  }
  ```

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

onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => void)

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

Z
zhou-liting125 已提交
990
**参数:**
L
update  
laosan_ted 已提交
991 992
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
Z
zhou-liting125 已提交
993 994
  | result | [FileSelectorResult](#fileselectorresult9) |  用于通知Web组件文件选择的结果。 |
  | fileSelector | [FileSelectorParam](#fileselectorparam9) | 文件选择器的相关信息。 |
L
update  
laosan_ted 已提交
995

Z
zhou-liting125 已提交
996
  **示例:**
Z
zhou-liting125 已提交
997 998
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
999
  @Entry
L
update  
laosan_ted 已提交
1000 1001
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1002 1003
    controller: WebController = new WebController();

L
update  
laosan_ted 已提交
1004 1005
    build() {
      Column() {
Z
zhou-liting125 已提交
1006
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
          .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 已提交
1023
              }
L
laosan_ted 已提交
1024 1025
            })
            return true;
L
update  
laosan_ted 已提交
1026
          })
L
laosan_ted 已提交
1027
      }
L
update  
laosan_ted 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    }
  }
  ```

### onUrlLoadIntercept

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

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

Z
zhou-liting125 已提交
1038
**参数:**
L
update  
laosan_ted 已提交
1039 1040 1041 1042
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | data | string / [WebResourceRequest](#webresourcerequest) | url的相关信息。 |

Z
zhou-liting125 已提交
1043 1044
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
1045 1046 1047
  |----------|------|
  | boolean | 返回true表示阻止此次加载,否则允许此次加载。 |

Z
zhou-liting125 已提交
1048
  **示例:**
Z
zhou-liting125 已提交
1049 1050
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1051
  @Entry
L
update  
laosan_ted 已提交
1052 1053
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1054 1055
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1056 1057
    build() {
      Column() {
Z
zhou-liting125 已提交
1058
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1059 1060 1061 1062 1063
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
            return true;
          })
      }
L
update  
laosan_ted 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    }
  }
  ```

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

onInterceptRequest9+(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)

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

Z
zhou-liting125 已提交
1074
**参数:**
L
update  
laosan_ted 已提交
1075 1076 1077 1078
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
  | request | [WebResourceRequest](#webresourcerequest) | url请求的相关信息。 |

Z
zhou-liting125 已提交
1079 1080
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
1081 1082 1083
  |----------|------|
  | [WebResourceResponse](#webresourceresponse) | 返回响应数据为空表示按原来方式加载,否则加载响应数据。 |

Z
zhou-liting125 已提交
1084
  **示例:**
Z
zhou-liting125 已提交
1085 1086
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1087
  @Entry
L
update  
laosan_ted 已提交
1088 1089 1090
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    responseweb: WebResourceResponse = new WebResourceResponse();
    heads:Header[] = new Array();
    @State webdata: string = "<!DOCTYPE html>\n" +
    "<html>\n"+
    "<head>\n"+
    "<title>intercept test</title>\n"+
    "</head>\n"+
    "<body>\n"+
    "<h1>intercept test</h1>\n"+
    "</body>\n"+
    "</html>"
L
update  
laosan_ted 已提交
1102 1103
    build() {
      Column() {
Z
zhou-liting125 已提交
1104
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
          .onInterceptRequest((event) => {
            console.log('url:' + event.request.getRequestUrl());
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
            var length = this.heads.push(head1);
            length = this.heads.push(head2);
            this.responseweb.setResponseHeader(this.heads);
            this.responseweb.setResponseData(this.webdata);
            this.responseweb.setResponseEncoding('utf-8');
            this.responseweb.setResponseMimeType('text/html');
            this.responseweb.setResponseCode(200);
            this.responseweb.setReasonMessage('OK');
            return this.responseweb;
          })
L
update  
laosan_ted 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
      }
    }
  }
  ```

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

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

通知收到http auth认证请求。

Z
zhou-liting125 已提交
1136
**参数:**
L
update  
laosan_ted 已提交
1137 1138
  | 参数名 | 参数类型 | 参数描述                  |
  | ------ | -------- | ------------------------- |
Z
zhou-liting125 已提交
1139
  | handler | [HttpAuthHandler](#httpauthhandler9) | 通知Web组件用户操作行为。 |
L
update  
laosan_ted 已提交
1140 1141 1142
  | host | string | HTTP身份验证凭据应用的主机。 |
  | realm | string | HTTP身份验证凭据应用的域。 |

Z
zhou-liting125 已提交
1143 1144
**返回值:**
  | 类型 | 说明 |
L
update  
laosan_ted 已提交
1145 1146 1147
  |----------|------|
  | boolean | 返回false表示此次认证失败,否则成功。 |

Z
zhou-liting125 已提交
1148
  **示例:**
Z
zhou-liting125 已提交
1149 1150
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1151
  @Entry
L
update  
laosan_ted 已提交
1152 1153 1154
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
Z
zhou-liting125 已提交
1155
    httpAuth:boolean = false;
L
update  
laosan_ted 已提交
1156 1157
    build() {
      Column() {
Z
zhou-liting125 已提交
1158
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
1159 1160 1161 1162 1163 1164 1165
        .onHttpAuthRequest((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
Z
zhou-liting125 已提交
1166
                this.httpAuth = event.handler.isHttpAuthInfoSaved();
L
update  
laosan_ted 已提交
1167 1168
                if (this.httpAuth == false) {
                  web.WebDataBase.saveHttpAuthCredentials(
Z
zhou-liting125 已提交
1169 1170 1171 1172
                   event.host,
                   event.realm,
                   "2222",
                   "2222"
L
update  
laosan_ted 已提交
1173
                  )
Z
zhou-liting125 已提交
1174
                  event.handler.cancel();
L
update  
laosan_ted 已提交
1175 1176 1177 1178
                }
              }
            },
            cancel: () => {
Z
zhou-liting125 已提交
1179
             event.handler.cancel();
L
update  
laosan_ted 已提交
1180 1181 1182 1183 1184 1185 1186
            }
          })
          return true;
        })
    }
  }
  ```
1187

Z
zhou-liting125 已提交
1188
## ConsoleMessage
1189

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

Z
zhou-liting125 已提交
1192
### getLineNumber
1193

Z
zhou-liting125 已提交
1194 1195 1196 1197
getLineNumber(): number

获取ConsoleMessage的行数。

Z
zhou-liting125 已提交
1198 1199
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208
  | -------- | -------------------- |
  | number   | 返回ConsoleMessage的行数。 |

### getMessage

getMessage(): string

获取ConsoleMessage的日志信息。

Z
zhou-liting125 已提交
1209 1210
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1211 1212 1213 1214 1215 1216
  | -------- | -------------------- |
  | string   | 返回ConsoleMessage的日志信息。 |

### getMessageLevel

getMessageLevel(): MessageLevel
1217

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

Z
zhou-liting125 已提交
1220 1221
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230
  | -------- | -------------------- |
  | [MessageLevel](#messagelevel枚举说明)   | 返回ConsoleMessage的信息级别。 |

### getSourceId

getSourceId(): string

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

Z
zhou-liting125 已提交
1231 1232
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1233 1234 1235 1236
  | -------- | -------------------- |
  | string   | 返回网页源文件路径和名字。 |

## JsResult
L
lixingchi1 已提交
1237

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

Z
zhou-liting125 已提交
1240 1241 1242
### handleCancel

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

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

Z
zhou-liting125 已提交
1246
### handleConfirm
L
add web  
liujinwei 已提交
1247

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

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

Z
zhou-liting125 已提交
1252 1253 1254 1255 1256 1257
### handlePromptConfirm<sup>9+</sup>

handlePromptConfirm(result: string): void

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

Z
zhou-liting125 已提交
1258
**参数:**
Z
zhou-liting125 已提交
1259 1260 1261 1262 1263 1264
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | result    | string   | 是   | -      | 用户输入的对话框内容。 |

## WebResourceError

L
update  
laosan_ted 已提交
1265
web组件资源管理错误信息对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
1266 1267 1268 1269 1270 1271 1272

### getErrorCode

getErrorCode(): number

获取加载资源的错误码。

Z
zhou-liting125 已提交
1273 1274
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283
  | -------- | -------------------- |
  | number   | 返回加载资源的错误码。 |

### getErrorInfo

getErrorInfo(): string

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

Z
zhou-liting125 已提交
1284 1285
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1286 1287 1288 1289 1290
  | -------- | -------------------- |
  | string   | 返回加载资源的错误信息。 |

## WebResourceRequest

L
update  
laosan_ted 已提交
1291
web组件获取资源请求对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
1292 1293 1294 1295 1296 1297 1298

### getRequestHeader

getResponseHeader() : Array\<Header\>

获取资源请求头信息。

Z
zhou-liting125 已提交
1299 1300
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309
  | -------- | -------------------- |
  | Array\<[Header](#header)\>   | 返回资源请求头信息。 |

### getRequestUrl

getRequestUrl(): string

获取资源请求的URL信息。

Z
zhou-liting125 已提交
1310 1311
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320
  | -------- | -------------------- |
  | string   | 返回资源请求的URL信息。 |

### isMainFrame

isMainFrame(): boolean

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

Z
zhou-liting125 已提交
1321 1322
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331
  | -------- | -------------------- |
  | boolean   | 返回资源请求是否为主frame。 |

### isRedirect

isRedirect(): boolean

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

Z
zhou-liting125 已提交
1332 1333
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342
  | -------- | -------------------- |
  | boolean   | 返回资源请求是否被服务端重定向。 |

### isRequestGesture

isRequestGesture(): boolean

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

Z
zhou-liting125 已提交
1343 1344
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1345 1346 1347 1348
  | -------- | -------------------- |
  | boolean   | 返回资源请求是否与手势(如点击)相关联。 |

## Header
L
liwenzhen 已提交
1349 1350 1351

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

Z
zhou-liting125 已提交
1352
| 名称    | 类型 | 描述             |
1353 1354 1355
| ----------- | -------- | -------------------- |
| headerKey   | string   | 请求/响应头的key。   |
| headerValue | string   | 请求/响应头的value。 |
L
liwenzhen 已提交
1356

L
add web  
liujinwei 已提交
1357

Z
zhou-liting125 已提交
1358
## WebResourceResponse
L
add web  
liujinwei 已提交
1359

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

Z
zhou-liting125 已提交
1362
### getReasonMessage
T
Ted 已提交
1363

Z
zhou-liting125 已提交
1364
getReasonMessage(): string
T
Ted 已提交
1365

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

Z
zhou-liting125 已提交
1368 1369
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1370 1371
  | -------- | -------------------- |
  | string   | 返回资源响应的状态码描述。 |
T
Ted 已提交
1372

Z
zhou-liting125 已提交
1373
### getResponseCode
T
Ted 已提交
1374

Z
zhou-liting125 已提交
1375 1376 1377 1378
getResponseCode(): number

获取资源响应的状态码。

Z
zhou-liting125 已提交
1379 1380
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389
  | -------- | -------------------- |
  | number   | 返回资源响应的状态码。 |

### getResponseData

getResponseData(): string

获取资源响应数据。

Z
zhou-liting125 已提交
1390 1391
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400
  | -------- | -------------------- |
  | string   | 返回资源响应数据。 |

### getResponseEncoding

getResponseEncoding(): string

获取资源响应的编码。

Z
zhou-liting125 已提交
1401 1402
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411
  | -------- | -------------------- |
  | string   | 返回资源响应的编码。 |

### getResponseHeader

getResponseHeader() : Array\<Header\>

获取资源响应头。

Z
zhou-liting125 已提交
1412 1413
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422
  | -------- | -------------------- |
  | Array\<[Header](#header)\>   | 返回资源响应头。 |

### getResponseMimeType

getResponseMimeType(): string

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

Z
zhou-liting125 已提交
1423 1424
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433
  | -------- | -------------------- |
  | string   | 返回资源响应的媒体(MIME)类型。 |

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

setResponseData(data: string)

设置资源响应数据。

Z
zhou-liting125 已提交
1434
**参数:**
Z
zhou-liting125 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | data    | string   | 是   | -      | 要设置的资源响应数据。 |

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

setResponseEncoding(encoding: string)

设置资源响应的编码。

Z
zhou-liting125 已提交
1445
**参数:**
Z
zhou-liting125 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | encoding    | string   | 是   | -      | 要设置的资源响应的编码。 |

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

setResponseMimeType(mimeType: string)

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

Z
zhou-liting125 已提交
1456
**参数:**
Z
zhou-liting125 已提交
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | mimeType    | string   | 是   | -      | 要设置的资源响应的媒体(MIME)类型。 |

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

setReasonMessage(reason: string)

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

Z
zhou-liting125 已提交
1467
**参数:**
Z
zhou-liting125 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | reason    | string   | 是   | -      | 要设置的资源响应的状态码描述。 |

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

setResponseHeader(header: Array\<Header\>)

设置资源响应头。

Z
zhou-liting125 已提交
1478
**参数:**
Z
zhou-liting125 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | header    | Array\<[Header](#header)\>   | 是   | -      | 要设置的资源响应头。 |

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

setResponseCode(code: number)

设置资源响应的状态码。

Z
zhou-liting125 已提交
1489
**参数:**
Z
zhou-liting125 已提交
1490 1491 1492 1493 1494
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | code    | number   | 是   | -      | 要设置的资源响应的状态码。 |

## FileSelectorResult<sup>9+</sup>
T
Ted 已提交
1495

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

Z
zhou-liting125 已提交
1498
### handleFileList<sup>9+</sup>
1499

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

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

Z
zhou-liting125 已提交
1504
**参数:**
Z
zhou-liting125 已提交
1505 1506 1507
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | fileList    | Array\<string\>   | 是   | -      | 需要进行操作的文件列表。 |
1508

Z
zhou-liting125 已提交
1509 1510
## FileSelectorParam<sup>9+</sup>

Z
zhou-liting125 已提交
1511
web组件获取文件对象。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
Z
zhou-liting125 已提交
1512 1513 1514 1515 1516 1517 1518

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

getTitle(): string

获取文件选择器标题。

Z
zhou-liting125 已提交
1519 1520
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529
  | -------- | -------------------- |
  | string   | 返回文件选择器标题。 |

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

getMode(): FileSelectorMode

获取文件选择器的模式。

Z
zhou-liting125 已提交
1530 1531
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1532
  | -------- | -------------------- |
Z
zhou-liting125 已提交
1533
  | [FileSelectorMode](#fileselectormode枚举说明)   | 返回文件选择器的模式。 |
Z
zhou-liting125 已提交
1534 1535 1536 1537 1538 1539 1540

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

getAcceptType(): Array\<string\>

获取文件过滤类型。

Z
zhou-liting125 已提交
1541 1542
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551
  | -------- | -------------------- |
  | Array\<string\>   | 返回文件过滤类型。 |

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

isCapture(): boolean

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

Z
zhou-liting125 已提交
1552 1553
**返回值:** 
  | 类型 | 说明                 |
Z
zhou-liting125 已提交
1554 1555
  | -------- | -------------------- |
  | boolean   | 返回是否调用多媒体能力。 |
1556 1557 1558

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

Z
zhou-liting125 已提交
1559
Web组件返回的http auth认证请求确认或取消和使用缓存密码认证功能对象。示例代码参考[onHttpAuthRequest事件](#onhttpauthrequest9)
1560 1561 1562 1563 1564 1565 1566 1567

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

cancel(): void

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

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

1569 1570 1571 1572
confirm(userName: string, pwd: string): boolean

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

Z
zhou-liting125 已提交
1573
**参数:**
1574 1575 1576 1577 1578 1579

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

Z
zhou-liting125 已提交
1580 1581
**返回值:**
  | 类型  | 说明                            |
1582 1583 1584 1585 1586 1587 1588 1589 1590
  | -------- | ------------------------------- |
  | boolean  | 认证成功返回true,失败返回false。 |

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

isHttpAuthInfoSaved(): boolean

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

Z
zhou-liting125 已提交
1591 1592
**返回值:**
  | 类型  | 说明                                  |
1593 1594
  | -------- | ------------------------------------- |
  | boolean  | 存在密码认证成功返回true,其他返回false。|
1595

Z
zengyawen 已提交
1596 1597
## WebController

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

### 创建对象

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

L
lixingchi1 已提交
1606 1607 1608 1609
### accessBackward

accessBackward(): boolean

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

Z
zhou-liting125 已提交
1612 1613
**返回值:**
  | 类型  | 说明                                  |
L
update  
laosan_ted 已提交
1614 1615 1616
  | -------- | ------------------------------------- |
  | boolean  | 可以后退返回true,否则返回false。 |

Z
zhou-liting125 已提交
1617
**示例:**
Z
zhou-liting125 已提交
1618 1619
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1620
  @Entry
L
update  
laosan_ted 已提交
1621 1622
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1623 1624
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1625 1626 1627
    build() {
      Column() {
        Button('accessBackward')
L
laosan_ted 已提交
1628 1629 1630 1631
          .onClick(() => {
            let result = this.controller.accessBackward();
            console.log('result:' + result);
          })
Z
zhou-liting125 已提交
1632
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1633
      }
L
update  
laosan_ted 已提交
1634 1635 1636 1637
    }
  }
  ```

L
lixingchi1 已提交
1638 1639 1640 1641
### accessForward

accessForward(): boolean

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

Z
zhou-liting125 已提交
1644 1645
**返回值:**
  | 类型  | 说明                                  |
L
update  
laosan_ted 已提交
1646 1647 1648
  | -------- | ------------------------------------- |
  | boolean  | 可以前进返回true,否则返回false。 |

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

L
lixingchi1 已提交
1670
### accessStep
Z
zengyawen 已提交
1671

L
lixingchi1 已提交
1672
accessStep(step: number): boolean
Z
zengyawen 已提交
1673

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

Z
zhou-liting125 已提交
1676
**参数:**
Z
zengyawen 已提交
1677

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

Z
zhou-liting125 已提交
1682 1683
**返回值:**
  | 类型 | 说明               |
1684 1685
  | -------- | ------------------ |
  | boolean  | 页面是否前进或后退 |
1686

Z
zhou-liting125 已提交
1687
**示例:**
Z
zhou-liting125 已提交
1688 1689
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1690
  @Entry
L
update  
laosan_ted 已提交
1691 1692
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1693
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
1694
    @State steps: number = 2;
L
laosan_ted 已提交
1695
  
L
update  
laosan_ted 已提交
1696 1697 1698
    build() {
      Column() {
        Button('accessStep')
L
laosan_ted 已提交
1699 1700 1701 1702
          .onClick(() => {
            let result = this.controller.accessStep(this.steps);
            console.log('result:' + result);
          })
Z
zhou-liting125 已提交
1703
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1704
      }
L
update  
laosan_ted 已提交
1705 1706 1707 1708
    }
  }
  ```

L
lixingchi1 已提交
1709 1710
### backward

L
update  
laosan_ted 已提交
1711
backward(): void
L
lixingchi1 已提交
1712

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

Z
zhou-liting125 已提交
1715
**示例:**
Z
zhou-liting125 已提交
1716 1717
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1718
  @Entry
L
update  
laosan_ted 已提交
1719 1720
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1721 1722
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1723 1724 1725
    build() {
      Column() {
        Button('backward')
L
laosan_ted 已提交
1726 1727 1728
          .onClick(() => {
            this.controller.backward();
          })
Z
zhou-liting125 已提交
1729
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1730
      }
L
update  
laosan_ted 已提交
1731 1732 1733
    }
  }
  ```
L
lixingchi1 已提交
1734 1735 1736

### forward

L
update  
laosan_ted 已提交
1737
forward(): void
L
lixingchi1 已提交
1738

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

Z
zhou-liting125 已提交
1741
**示例:**
Z
zhou-liting125 已提交
1742 1743
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1744
  @Entry
L
update  
laosan_ted 已提交
1745 1746
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1747 1748
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1749 1750 1751
    build() {
      Column() {
        Button('forward')
L
laosan_ted 已提交
1752 1753 1754
          .onClick(() => {
            this.controller.forward();
          })
Z
zhou-liting125 已提交
1755
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1756
      }
L
update  
laosan_ted 已提交
1757 1758 1759 1760
    }
  }
  ```

1761 1762 1763 1764 1765 1766
### backOrForward<sup>9+</sup>

backOrForward(step: number): void

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

Z
zhou-liting125 已提交
1767
**参数:**
1768 1769 1770 1771
  | 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                                     |
  | ---- | ------ | ---- | ---- | ---------------------------------------- |
  | step | number | 是    |-    |需要前进或后退的步长。 |

Z
zhou-liting125 已提交
1772
**示例:**
Z
zhou-liting125 已提交
1773 1774
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1775
  @Entry
L
update  
laosan_ted 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
        Button('backOrForward')
        .onClick(() => {
          this.controller.backOrForward();
        })
Z
zhou-liting125 已提交
1785
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
    }
  }
  ```

### deleteJavaScriptRegister

deleteJavaScriptRegister(name: string)

删除通过registerJavaScriptProxy注册到window上的指定name的应用侧JavaScript对象。

Z
zhou-liting125 已提交
1796
**参数:**
L
update  
laosan_ted 已提交
1797 1798 1799 1800
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                                                     |
  | ------ | -------- | ---- | ------ | ------------------------------------------------------------ |
  | name   | string   | 是   | -      | 注册对象的名称,可在网页侧JavaScript中通过此名称调用应用侧JavaScript对象。 |

Z
zhou-liting125 已提交
1801
**示例:**
Z
zhou-liting125 已提交
1802 1803
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1804
  @Entry
L
update  
laosan_ted 已提交
1805 1806
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1807
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
1808
    @State name: string = 'Object';
L
laosan_ted 已提交
1809
  
L
update  
laosan_ted 已提交
1810 1811 1812
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
L
laosan_ted 已提交
1813 1814 1815
          .onClick(() => {
            this.controller.deleteJavaScriptRegister(this.name);
          })
Z
zhou-liting125 已提交
1816
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1817
      }
L
update  
laosan_ted 已提交
1818 1819 1820 1821
    }
  }
  ```

L
lixingchi1 已提交
1822 1823 1824 1825
### getHitTest

getHitTest(): HitTestType

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

Z
zhou-liting125 已提交
1828 1829
**返回值:**
  | 类型 | 说明               |
1830 1831
  | -------- | ------------------ |
  | [HitTestType](#hittesttype枚举说明)  | 被点击区域的元素类型。 |
L
lixingchi1 已提交
1832

Z
zhou-liting125 已提交
1833
**示例:**
Z
zhou-liting125 已提交
1834 1835
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1836
  @Entry
L
update  
laosan_ted 已提交
1837 1838
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1839 1840
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1841 1842 1843
    build() {
      Column() {
        Button('getHitTest')
L
laosan_ted 已提交
1844 1845 1846 1847
          .onClick(() => {
            let hitType = this.controller.getHitTest();
            console.log("hitType: " + hitType);
          })
Z
zhou-liting125 已提交
1848
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1849
      }
L
update  
laosan_ted 已提交
1850 1851 1852 1853
    }
  }
  ```

1854 1855 1856 1857 1858
### getHitTestValue<sup>9+</sup>
getHitTestValue(): HitTestValue

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

Z
zhou-liting125 已提交
1859 1860
**返回值:**
  | 类型 | 说明 |
1861
  |----------|------|
Z
zhou-liting125 已提交
1862
  | [HitTestValue](#hittestvalue9) | 点击区域的元素信息。 |
1863

Z
zhou-liting125 已提交
1864
**示例:**
Z
zhou-liting125 已提交
1865 1866
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1867
  @Entry
L
update  
laosan_ted 已提交
1868 1869
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1870 1871
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1872 1873 1874
    build() {
      Column() {
        Button('getHitTestValue')
L
laosan_ted 已提交
1875 1876 1877 1878 1879
          .onClick(() => {
            let hitValue = this.controller.getHitTestValue();
            console.log("hitType: " + hitValue.getType());
            console.log("extra: " + hitValue.getExtra());
          })
Z
zhou-liting125 已提交
1880
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1881
      }
L
update  
laosan_ted 已提交
1882 1883 1884 1885
    }
  }
  ```

1886 1887 1888 1889 1890
### getWebId<sup>9+</sup>
getWebId(): number

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

Z
zhou-liting125 已提交
1891 1892
**返回值:**
  | 类型 | 说明 |
1893 1894 1895
  |----------|------|
  | number | 当前Web组件的索引值。 |

Z
zhou-liting125 已提交
1896
**示例:**
Z
zhou-liting125 已提交
1897 1898
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1899
  @Entry
L
update  
laosan_ted 已提交
1900 1901
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1902 1903
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1904 1905 1906
    build() {
      Column() {
        Button('getWebId')
L
laosan_ted 已提交
1907 1908 1909 1910
          .onClick(() => {
            let id = this.controller.getWebId();
            console.log("id: " + id);
          })
Z
zhou-liting125 已提交
1911
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1912
      }
L
update  
laosan_ted 已提交
1913 1914 1915 1916
    }
  }
  ```

1917 1918 1919 1920
### getTitle<sup>9+</sup>
getTitle(): string

获取当前网页的标题。
Z
zhou-liting125 已提交
1921 1922 1923

**返回值:**
  | 类型 | 说明 |
1924 1925 1926
  |----------|------|
  | string | 当前网页的标题。 |

Z
zhou-liting125 已提交
1927
**示例:**
Z
zhou-liting125 已提交
1928 1929
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1930
  @Entry
L
update  
laosan_ted 已提交
1931 1932
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1933 1934
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1935 1936 1937
    build() {
      Column() {
        Button('getTitle')
L
laosan_ted 已提交
1938 1939 1940 1941
          .onClick(() => {
            let title = this.controller.getTitle();
            console.log("title: " + title);
          })
Z
zhou-liting125 已提交
1942
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1943
      }
L
update  
laosan_ted 已提交
1944 1945 1946 1947
    }
  }
  ```

1948 1949 1950 1951
### getPageHeight<sup>9+</sup>
getPageHeight(): number

获取当前网页的页面高度。
Z
zhou-liting125 已提交
1952 1953 1954

**返回值:**
  | 类型 | 说明 |
1955 1956 1957
  |----------|------|
  | number | 当前网页的页面高度。 |

Z
zhou-liting125 已提交
1958
**示例:**
Z
zhou-liting125 已提交
1959 1960
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1961
  @Entry
L
update  
laosan_ted 已提交
1962 1963
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1964 1965
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1966 1967 1968
    build() {
      Column() {
        Button('getPageHeight')
L
laosan_ted 已提交
1969 1970 1971 1972
          .onClick(() => {
            let pageHeight = this.controller.getPageHeight();
            console.log("pageHeight: " + pageHeight);
          })
Z
zhou-liting125 已提交
1973
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1974
      }
L
update  
laosan_ted 已提交
1975 1976 1977
    }
  }
  ```
1978 1979 1980 1981 1982

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

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

Z
zhou-liting125 已提交
1984 1985
**返回值:**
  | 类型 | 说明 |
1986 1987
  |----------|------|
  | string | 默认用户代理。 |
L
lixingchi1 已提交
1988

Z
zhou-liting125 已提交
1989
**示例:**
Z
zhou-liting125 已提交
1990 1991
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1992
  @Entry
L
update  
laosan_ted 已提交
1993 1994
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1995 1996
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1997 1998 1999
    build() {
      Column() {
        Button('getDefaultUserAgent')
L
laosan_ted 已提交
2000 2001 2002 2003
          .onClick(() => {
            let userAgent = this.controller.getDefaultUserAgent();
            console.log("userAgent: " + userAgent);
          })
Z
zhou-liting125 已提交
2004
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2005
      }
L
update  
laosan_ted 已提交
2006 2007 2008 2009
    }
  }
  ```

L
lixingchi1 已提交
2010 2011
### loadData

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

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

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

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

Z
zhou-liting125 已提交
2020
**参数:**
2021 2022 2023 2024 2025 2026 2027
  | 参数名     | 参数类型 | 必填 | 默认值 | 参数描述                                                     |
  | ---------- | -------- | ---- | ------ | ------------------------------------------------------------ |
  | 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 已提交
2028

Z
zhou-liting125 已提交
2029
**示例:**
Z
zhou-liting125 已提交
2030 2031
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2032
  @Entry
L
update  
laosan_ted 已提交
2033 2034
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2035 2036
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2037 2038 2039
    build() {
      Column() {
        Button('loadData')
L
laosan_ted 已提交
2040 2041 2042 2043 2044 2045 2046
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
            });
          })
Z
zhou-liting125 已提交
2047
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2048
      }
L
update  
laosan_ted 已提交
2049 2050 2051 2052
    }
  }
  ```

Z
zengyawen 已提交
2053 2054
### loadUrl

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

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

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

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

Z
zhou-liting125 已提交
2063
**参数:**
2064 2065 2066
  | 参数名  | 参数类型                           | 必填 | 默认值 | 参数描述              |
  | ------- | ---------------------------------- | ---- | ------ | --------------------- |
  | url     | string                             | 是   | -      | 需要加载的 URL。      |
Z
zhou-liting125 已提交
2067
  | headers | Array\<[Header](#header)\> | 否   | []     | URL的附加HTTP请求头。 |
L
lixingchi1 已提交
2068

Z
zhou-liting125 已提交
2069
**示例:**
Z
zhou-liting125 已提交
2070 2071
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2072
  @Entry
L
update  
laosan_ted 已提交
2073 2074
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2075 2076
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2077 2078 2079
    build() {
      Column() {
        Button('loadUrl')
L
laosan_ted 已提交
2080
          .onClick(() => {
Z
zhou-liting125 已提交
2081
            this.controller.loadUrl({ url: 'www.example.com' });
L
laosan_ted 已提交
2082
          })
Z
zhou-liting125 已提交
2083
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2084
      }
L
update  
laosan_ted 已提交
2085 2086 2087 2088
    }
  }
  ```

L
lixingchi1 已提交
2089 2090 2091 2092
### onActive

onActive(): void

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

Z
zhou-liting125 已提交
2095
**示例:**
Z
zhou-liting125 已提交
2096 2097
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2098
  @Entry
L
update  
laosan_ted 已提交
2099 2100
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2101 2102
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2103 2104 2105
    build() {
      Column() {
        Button('onActive')
L
laosan_ted 已提交
2106 2107 2108
          .onClick(() => {
            this.controller.onActive();
          })
Z
zhou-liting125 已提交
2109
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2110
      }
L
update  
laosan_ted 已提交
2111 2112 2113 2114
    }
  }
  ```

L
lixingchi1 已提交
2115 2116 2117 2118
### onInactive

onInactive(): void

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

Z
zhou-liting125 已提交
2121
**示例:**
Z
zhou-liting125 已提交
2122 2123
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2124
  @Entry
L
update  
laosan_ted 已提交
2125 2126
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2127 2128
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2129 2130 2131
    build() {
      Column() {
        Button('onInactive')
L
laosan_ted 已提交
2132 2133 2134
          .onClick(() => {
            this.controller.onInactive();
          })
Z
zhou-liting125 已提交
2135
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2136
      }
L
update  
laosan_ted 已提交
2137 2138 2139 2140
    }
  }
  ```

2141 2142 2143 2144
### zoom
zoom(factor: number): void

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

Z
zhou-liting125 已提交
2146
**参数:**
2147 2148 2149 2150
  | 参数名 | 参数类型 | 必填 | 参数描述 |
  |--------|----------|------|---------|
  | factor | number | 是 | 基于当前网页所需调整的相对缩放比例,正值为放大,负值为缩小。 |

Z
zhou-liting125 已提交
2151
**示例:**
Z
zhou-liting125 已提交
2152 2153
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2154
  @Entry
L
update  
laosan_ted 已提交
2155 2156
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2157
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
2158
    @State factor: number = 1;
L
laosan_ted 已提交
2159
  
L
update  
laosan_ted 已提交
2160 2161 2162
    build() {
      Column() {
        Button('zoom')
L
laosan_ted 已提交
2163 2164 2165
          .onClick(() => {
            this.controller.zoom(this.factor);
          })
Z
zhou-liting125 已提交
2166
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2167
      }
L
update  
laosan_ted 已提交
2168 2169 2170 2171
    }
  }
  ```

2172 2173 2174 2175
### zoomIn<sup>9+</sup>
zoomIn(): boolean

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

**返回值:**
  | 类型 | 说明 |
2179 2180 2181
  |----------|------|
  | boolean | 放大操作是否成功执行。 |

Z
zhou-liting125 已提交
2182
**示例:**
Z
zhou-liting125 已提交
2183 2184
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2185
  @Entry
L
update  
laosan_ted 已提交
2186 2187
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2188 2189
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2190 2191 2192
    build() {
      Column() {
        Button('zoomIn')
L
laosan_ted 已提交
2193 2194 2195 2196
          .onClick(() => {
            let result = this.controller.zoomIn();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2197
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2198
      }
L
update  
laosan_ted 已提交
2199 2200 2201 2202
    }
  }
  ```

2203 2204 2205 2206
### zoomOut<sup>9+</sup>
zoomOut(): boolean

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

**返回值:**
  | 类型 | 说明 |
2210 2211 2212
  |----------|------|
  | boolean | 缩小操作是否成功执行。 |

Z
zhou-liting125 已提交
2213
**示例:**
Z
zhou-liting125 已提交
2214 2215
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2216
  @Entry
L
update  
laosan_ted 已提交
2217 2218
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2219 2220
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2221 2222 2223
    build() {
      Column() {
        Button('zoomOut')
L
laosan_ted 已提交
2224 2225 2226 2227
          .onClick(() => {
            let result = this.controller.zoomOut();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2228
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2229
      }
L
update  
laosan_ted 已提交
2230 2231 2232 2233
    }
  }
  ```

L
lixingchi1 已提交
2234 2235
### refresh

Z
zhou-liting125 已提交
2236
refresh()
L
lixingchi1 已提交
2237

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

Z
zhou-liting125 已提交
2240
**示例:**
Z
zhou-liting125 已提交
2241 2242
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2243
  @Entry
L
update  
laosan_ted 已提交
2244 2245
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2246 2247
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2248 2249 2250
    build() {
      Column() {
        Button('refresh')
L
laosan_ted 已提交
2251 2252 2253
          .onClick(() => {
            this.controller.refresh();
          })
Z
zhou-liting125 已提交
2254
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2255
      }
L
update  
laosan_ted 已提交
2256 2257 2258 2259
    }
  }
  ```

L
lixingchi1 已提交
2260 2261
### registerJavaScriptProxy

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

L
liwenzhen 已提交
2264
注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。注册后,须调用refresh接口生效。
L
lixingchi1 已提交
2265

Z
zhou-liting125 已提交
2266
**参数:**
2267 2268 2269 2270 2271
  | 参数名     | 参数类型        | 必填 | 默认值 | 参数描述                                                     |
  | ---------- | --------------- | ---- | ------ | ------------------------------------------------------------ |
  | object     | object          | 是   | -      | 参与注册的应用侧JavaScript对象。只能声明方法,不能声明属性 。其中方法的参数和返回类型只能为string,number,boolean |
  | name       | string          | 是   | -      | 注册对象的名称,与window中调用的对象名一致。注册后window对象可以通过此名字访问应用侧JavaScript对象。 |
  | methodList | Array\<string\> | 是   | -      | 参与注册的应用侧JavaScript对象的方法。                       |
L
lixingchi1 已提交
2272

Z
zhou-liting125 已提交
2273
**示例:**
Z
zhou-liting125 已提交
2274 2275
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2276
  @Entry
L
update  
laosan_ted 已提交
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
        return "ArkUI Web Component";
      },
      toString: () => {
        console.log('Web Component toString');
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
            });
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```
  
  ```html
L
laosan_ted 已提交
2307
  <!-- index.html -->
L
update  
laosan_ted 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
          str = objName.test("test function");
          console.log('objName.test result:'+ str);
      }
  </script>
  </html>
  
  ```

L
lixingchi1 已提交
2324 2325
### runJavaScript

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

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

Z
zhou-liting125 已提交
2330
**参数:**
2331 2332 2333 2334
  | 参数名   | 参数类型                 | 必填 | 默认值 | 参数描述                                                     |
  | -------- | ------------------------ | ---- | ------ | ------------------------------------------------------------ |
  | script   | string                   | 是   | -      | JavaScript脚本。                                             |
  | callback | (result: string) => void | 否   | -      | 回调执行JavaScript脚本结果。JavaScript脚本若执行失败或无返回值时,返回null。 |
L
lixingchi1 已提交
2335

Z
zhou-liting125 已提交
2336
**示例:**
Z
zhou-liting125 已提交
2337 2338
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2339
  @Entry
L
update  
laosan_ted 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    @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}`)
            }});
          console.info('url: ', e.url);
        })
      }
    }
  }
  ```

  ```html
L
laosan_ted 已提交
2364
  <!-- index.html -->
L
update  
laosan_ted 已提交
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
        console.log('Ark WebComponent');
        return "This value is from index.html"
    }
    </script>
  </html>

  ```

L
lixingchi1 已提交
2381 2382
### stop

Z
zhou-liting125 已提交
2383
stop()
Z
zengyawen 已提交
2384

L
lixingchi1 已提交
2385
停止页面加载。
Z
zengyawen 已提交
2386

Z
zhou-liting125 已提交
2387
**示例:**
Z
zhou-liting125 已提交
2388 2389
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2390
  @Entry
L
update  
laosan_ted 已提交
2391 2392
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2393 2394
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2395 2396 2397
    build() {
      Column() {
        Button('stop')
L
laosan_ted 已提交
2398 2399 2400
          .onClick(() => {
            this.controller.stop();
          })
Z
zhou-liting125 已提交
2401
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2402
      }
L
update  
laosan_ted 已提交
2403 2404 2405 2406
    }
  }
  ```

T
Ted 已提交
2407 2408 2409 2410 2411 2412
### clearHistory

clearHistory(): void

删除所有前进后退记录。

Z
zhou-liting125 已提交
2413
**示例:**
Z
zhou-liting125 已提交
2414 2415
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2416
  @Entry
L
update  
laosan_ted 已提交
2417 2418
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2419 2420
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2421 2422 2423
    build() {
      Column() {
        Button('clearHistory')
L
laosan_ted 已提交
2424 2425 2426
          .onClick(() => {
            this.controller.clearHistory();
          })
Z
zhou-liting125 已提交
2427
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2428
      }
L
update  
laosan_ted 已提交
2429 2430 2431 2432
    }
  }
  ```

Z
zhou-liting125 已提交
2433
### getCookieManager<sup>9+</sup>
T
Ted 已提交
2434 2435 2436 2437

getCookieManager(): WebCookie

获取web组件cookie管理对象。
Z
zhou-liting125 已提交
2438 2439 2440

**返回值:**
  | 类型  | 说明                                                     |
2441
  | --------- | -------------------------------------------------------- |
T
Ted 已提交
2442
  | WebCookie | web组件cookie管理对象,参考[WebCookie](#webcookie)定义。 |
2443

Z
zhou-liting125 已提交
2444
**示例:**
Z
zhou-liting125 已提交
2445 2446
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2447
  @Entry
L
update  
laosan_ted 已提交
2448 2449
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2450 2451
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2452 2453 2454
    build() {
      Column() {
        Button('getCookieManager')
L
laosan_ted 已提交
2455 2456 2457
          .onClick(() => {
            let cookieManager = this.controller.getCookieManager();
          })
Z
zhou-liting125 已提交
2458
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2459
      }
L
update  
laosan_ted 已提交
2460 2461 2462 2463
    }
  }
  ```

2464
## HitTestValue<sup>9+</sup>
Z
zhou-liting125 已提交
2465
提供点击区域的元素信息。示例代码参考[getHitTestValue](#gethittestvalue9)
2466 2467 2468 2469 2470

### getType<sup>9+</sup>
getType(): HitTestType

获取当前被点击区域的元素类型。
Z
zhou-liting125 已提交
2471 2472 2473

**返回值:**
  | 类型 | 说明 |
2474 2475 2476 2477 2478 2479 2480
  |----------|------|
  | [HitTestType](#hittesttype枚举说明) | 当前被点击区域的元素类型。 |

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

若被点击区域为图片或链接,则附加参数信息为其url地址。
Z
zhou-liting125 已提交
2481 2482 2483

**返回值:**
  | 类型 | 说明 |
2484 2485 2486
  |----------|------|
  | string | 点击区域的附加参数信息。 |

T
Ted 已提交
2487
## WebCookie
Z
zhou-liting125 已提交
2488

T
Ted 已提交
2489
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookie。通过controller方法中的getCookieManager方法可以获取WebCookie对象,进行后续的cookie管理操作。
Z
zhou-liting125 已提交
2490 2491

### setCookie<sup>9+</sup>
T
Ted 已提交
2492 2493 2494 2495
setCookie(url: string, value: string): boolean

设置cookie,该方法为同步方法。设置成功返回true,否则返回false。

Z
zhou-liting125 已提交
2496
**参数:**
2497 2498 2499 2500
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                  |
  | ------ | -------- | ---- | ------ | ------------------------- |
  | url    | string   | 是   | -      | 要设置的cookie所属的url。 |
  | value  | string   | 是   | -      | cookie的值。              |
Z
zhou-liting125 已提交
2501

Z
zhou-liting125 已提交
2502 2503
**返回值:** 
  | 类型 | 说明                 |
2504 2505
  | -------- | -------------------- |
  | boolean  | 设置cookie是否成功。 |
T
Ted 已提交
2506

Z
zhou-liting125 已提交
2507
**示例:**
Z
zhou-liting125 已提交
2508 2509
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2510
  @Entry
L
update  
laosan_ted 已提交
2511 2512
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2513 2514
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2515 2516 2517
    build() {
      Column() {
        Button('setCookie')
L
laosan_ted 已提交
2518
          .onClick(() => {
Z
zhou-liting125 已提交
2519
            let result = this.controller.getCookieManager().setCookie("www.example.com", "a=b");
L
laosan_ted 已提交
2520 2521
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2522
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2523
      }
L
update  
laosan_ted 已提交
2524 2525 2526 2527
    }
  }
  ```

Z
zhou-liting125 已提交
2528
### saveCookieSync<sup>9+</sup>
T
Ted 已提交
2529 2530 2531
saveCookieSync(): boolean

将当前存在内存中的cookie同步到磁盘中,该方法为同步方法。
Z
zhou-liting125 已提交
2532 2533 2534

**返回值:**
  | 类型 | 说明                               |
2535 2536
  | -------- | ---------------------------------- |
  | boolean  | 同步内存cookie到磁盘操作是否成功。 |
2537

Z
zhou-liting125 已提交
2538
**示例:**
Z
zhou-liting125 已提交
2539 2540
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2541
  @Entry
L
update  
laosan_ted 已提交
2542 2543
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2544 2545
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2546 2547 2548
    build() {
      Column() {
        Button('saveCookieSync')
L
laosan_ted 已提交
2549 2550 2551 2552
          .onClick(() => {
            let result = this.controller.getCookieManager().saveCookieSync();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2553
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2554
      }
L
update  
laosan_ted 已提交
2555 2556 2557 2558
    }
  }
  ```

2559 2560 2561 2562 2563 2564 2565
## WebDataBase<sup>9+</sup>
web组件数据库管理对象。

### existHttpAuthCredentials<sup>9+</sup>

static existHttpAuthCredentials(): boolean

2566
判断是否存在任何已保存的HTTP身份验证凭据,该方法为同步方法。存在返回true,不存在返回false。
2567

Z
zhou-liting125 已提交
2568 2569
**返回值:** 
  | 类型  | 说明                                |
2570 2571 2572
  | -------- | ----------------------------------- |
  | boolean  | 是否存在任何已保存的HTTP身份验证凭据。存在返回true,不存在返回false |

Z
zhou-liting125 已提交
2573
**示例:**
Z
zhou-liting125 已提交
2574 2575
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2576
  import web from '@ohos.web';
L
laosan_ted 已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
  
    build() {
      Column() {
        Button('existHttpAuthCredentials')
          .onClick(() => {
            let result = web.WebDataBase.existHttpAuthCredentials();
            console.log('result: ' + result);
          })
Z
zhou-liting125 已提交
2589
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2590 2591 2592
      }
    }
  }
L
update  
laosan_ted 已提交
2593 2594
  ```

2595 2596 2597 2598 2599 2600
### deleteHttpAuthCredentials<sup>9+</sup>

static deleteHttpAuthCredentials(): void

清除所有已保存的HTTP身份验证凭据,该方法为同步方法。

Z
zhou-liting125 已提交
2601
**示例:**
Z
zhou-liting125 已提交
2602 2603
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2604
  import web from '@ohos.web';
L
laosan_ted 已提交
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
  
    build() {
      Column() {
        Button('deleteHttpAuthCredentials')
          .onClick(() => {
            web.WebDataBase.deleteHttpAuthCredentials();
          })
Z
zhou-liting125 已提交
2616
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2617 2618 2619
      }
    }
  }
L
update  
laosan_ted 已提交
2620 2621
  ```

2622 2623 2624 2625
### getHttpAuthCredentials<sup>9+</sup>

static getHttpAuthCredentials(host: string, realm: string): Array\<string\>

2626
检索给定主机和域的HTTP身份验证凭据,该方法为同步方法。检索成功返回一个包含用户名和密码的组数,检索不成功返回空数组。
2627

Z
zhou-liting125 已提交
2628
**参数:**
2629 2630 2631
  | 参数名  | 参数类型 | 必填  | 默认值 | 参数描述                    |
  | ------ | -------- | ---- | ------ | -------------------------- |
  | host   | string   | 是   | -      | HTTP身份验证凭据应用的主机。 |
2632
  | realm  | string   | 是   | -      | HTTP身份验证凭据应用的域。 |
Z
zhou-liting125 已提交
2633

Z
zhou-liting125 已提交
2634 2635
**返回值:** 
  | 类型          | 说明                                          |
2636 2637 2638
  | ---------------- | --------------------------------------------- |
  | Array\<string\>  | 包含用户名和密码的组数,检索失败返回空数组。 |

Z
zhou-liting125 已提交
2639
**示例:**
Z
zhou-liting125 已提交
2640 2641
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2642
  import web from '@ohos.web';
L
laosan_ted 已提交
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    host: string = "www.spincast.org";
    realm: string = "protected example";
    username_password: string[];
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
            this.username_password = web.WebDataBase.getHttpAuthCredentials(this.host, this.realm);
            console.log('num: ' + this.username_password.length);
            ForEach(this.username_password, (item) => {
              console.log('username_password: ' + item);
            }, item => item)
          })
Z
zhou-liting125 已提交
2660
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2661 2662 2663
      }
    }
  }
L
update  
laosan_ted 已提交
2664 2665
  ```

2666 2667 2668 2669
### saveHttpAuthCredentials<sup>9+</sup>

static saveHttpAuthCredentials(host: string, realm: string, username: string, password: string): void

2670
保存给定主机和域的HTTP身份验证凭据,该方法为同步方法。
2671

Z
zhou-liting125 已提交
2672
**参数:**
2673 2674 2675
  | 参数名    | 参数类型 | 必填  | 默认值 | 参数描述                    |
  | -------- | -------- | ---- | ------ | -------------------------- |
  | host     | string   | 是   | -      | HTTP身份验证凭据应用的主机。 |
2676
  | realm    | string   | 是   | -      | HTTP身份验证凭据应用的域。 |
2677 2678 2679
  | username | string   | 是   | -      | 用户名。                    |
  | password | string   | 是   | -      | 密码。                      |

Z
zhou-liting125 已提交
2680
**示例:**
Z
zhou-liting125 已提交
2681 2682
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2683
  import web from '@ohos.web';
L
laosan_ted 已提交
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    host: string = "www.spincast.org";
    realm: string = "protected example";
    build() {
      Column() {
        Button('saveHttpAuthCredentials')
          .onClick(() => {
            web.WebDataBase.saveHttpAuthCredentials(this.host, this.realm, "Stromgol", "Laroche");
          })
Z
zhou-liting125 已提交
2696
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2697 2698 2699
      }
    }
  }
L
update  
laosan_ted 已提交
2700 2701
  ```

2702 2703 2704 2705 2706 2707
## WebStorage<sup>9+</sup>
通过WebStorage可管理Web SQL数据库接口和HTML5 Web存储接口,每个应用中的所有Web组件共享一个WebStorage。
### deleteAllData<sup>9+</sup>
static deleteAllData(): void

清除Web SQL数据库当前使用的所有存储。
L
update  
laosan_ted 已提交
2708

Z
zhou-liting125 已提交
2709
**示例:**
Z
zhou-liting125 已提交
2710 2711
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2712
  import web from '@ohos.web';
L
laosan_ted 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('deleteAllData')
          .onClick(() => {
            web.WebStorage.deleteAllData();
          })
Z
zhou-liting125 已提交
2723
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2724 2725 2726 2727
        .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
2728 2729
  ```

2730 2731 2732 2733
### deleteOrigin<sup>9+</sup>
static deleteOrigin(origin : string): void

清除指定源所使用的存储。
Z
zhou-liting125 已提交
2734

Z
zhou-liting125 已提交
2735
**参数:**
2736 2737 2738 2739
  | 参数名 | 参数类型 | 必填 | 说明 |
  |---------|---------|-----|-----|
  | origin | string | 是 | 指定源的字符串索引。 |

Z
zhou-liting125 已提交
2740
**示例:**
Z
zhou-liting125 已提交
2741 2742
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2743
  import web from '@ohos.web';
L
laosan_ted 已提交
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
            web.WebStorage.deleteOrigin(this.origin);
          })
Z
zhou-liting125 已提交
2755
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2756 2757 2758 2759
        .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
2760 2761
  ```

2762
### getOrigins<sup>9+</sup>
Z
zhou-liting125 已提交
2763
static getOrigins(callback: AsyncCallback<Array<[WebStorageOrigin]>>) : void
2764 2765

以回调方式异步获取当前使用Web SQL数据库的所有源的信息。
Z
zhou-liting125 已提交
2766

Z
zhou-liting125 已提交
2767
**参数:**
2768 2769
  | 参数名 | 参数类型 | 必填 | 说明 |
  |---------|---------|-----|----|
Z
zhou-liting125 已提交
2770
  | callback | AsyncCallback<Array<[WebStorageOrigin](#webstorageorigin9)>> | 是 | 以数组方式返回源的信息,信息内容参考WebStorageOrigin。|
2771

Z
zhou-liting125 已提交
2772
**示例:**
Z
zhou-liting125 已提交
2773 2774
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2775
  import web from '@ohos.web';
L
laosan_ted 已提交
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
            web.WebStorage.getOrigins((error, origins) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              for (let i = 0; i < origins.length; i++) {
                console.log('origin: ' + origins[i].origin);
                console.log('usage: ' + origins[i].usage);
                console.log('quota: ' + origins[i].quota);
              }
            })
          })
Z
zhou-liting125 已提交
2797
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2798 2799
        .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
2800
    }
L
laosan_ted 已提交
2801 2802
  }
  ```
L
update  
laosan_ted 已提交
2803

2804
### getOrigins<sup>9+</sup>
Z
zhou-liting125 已提交
2805
static getOrigins() : Promise<Array<[WebStorageOrigin]>>
2806 2807

以Promise方式异步获取当前使用Web SQL数据库的所有源的信息。
Z
zhou-liting125 已提交
2808

Z
zhou-liting125 已提交
2809
**返回值:**
2810 2811
  | 类型 | 说明 |
  |------|------|
Z
zhou-liting125 已提交
2812
  | Promise<Array<[WebStorageOrigin](#webstorageorigin9)>> | Promise实例,用于获取当前所有源的信息,信息内容参考WebStorageOrigin。 |
2813

Z
zhou-liting125 已提交
2814
**示例:**
Z
zhou-liting125 已提交
2815 2816
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2817
  import web from '@ohos.web';
L
laosan_ted 已提交
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
            web.WebStorage.getOrigins()
              .then(origins => {
                for (let i = 0; i < origins.length; i++) {
                  console.log('origin: ' + origins[i].origin);
                  console.log('usage: ' + origins[i].usage);
                  console.log('quota: ' + origins[i].quota);
                }
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
2839
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2840 2841
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
2842
    }
L
laosan_ted 已提交
2843
  }
L
update  
laosan_ted 已提交
2844 2845
  ```

2846 2847 2848 2849
### getOriginQuota<sup>9+</sup>
static getOriginQuota(origin : string, callback : AsyncCallback<number>) : void

使用callback回调异步获取指定源的Web SQL数据库的存储配额,配额以字节为单位。
Z
zhou-liting125 已提交
2850

Z
zhou-liting125 已提交
2851
**参数:**
2852 2853 2854 2855 2856
  | 参数名 |  参数类型 | 必填 | 说明 |
  |----------|-----------|------|------|
  | origin | string | 是 | 指定源的字符串索引 |
  | callback | AsyncCallback<number> | 是 | 指定源的存储配额。 |

Z
zhou-liting125 已提交
2857
**示例:**
Z
zhou-liting125 已提交
2858 2859
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2860
  import web from '@ohos.web';
L
laosan_ted 已提交
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginQuota(this.origin, (error, quota) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              console.log('quota: ' + quota);
            })
          })
Z
zhou-liting125 已提交
2878
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2879 2880
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
2881
    }
L
laosan_ted 已提交
2882
  }
L
update  
laosan_ted 已提交
2883 2884
  ```

2885 2886 2887 2888
### getOriginQuota<sup>9+</sup>
static getOriginQuota(origin : string) : Promise<number>

以Promise方式异步获取指定源的Web SQL数据库的存储配额,配额以字节为单位。
Z
zhou-liting125 已提交
2889

Z
zhou-liting125 已提交
2890
**参数:**
2891 2892 2893 2894
  | 参数名 | 参数类型 | 必填 | 说明 |
  |----------|---------|------|-------|
  | origin | string | 是 | 指定源的字符串索引。 |

Z
zhou-liting125 已提交
2895
**返回值:**
2896 2897 2898 2899
  | 类型 | 说明 |
  |------|------|
  | Promise<number> | Promise实例,用于获取指定源的存储配额。 |

Z
zhou-liting125 已提交
2900
**示例:**
Z
zhou-liting125 已提交
2901 2902
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2903
  import web from '@ohos.web';
L
laosan_ted 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginQuota(this.origin)
              .then(quota => {
                console.log('quota: ' + quota);
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
2921
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2922 2923 2924 2925
          .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
2926 2927
  ```

2928 2929 2930 2931
### getOriginUsage<sup>9+</sup>
static getOriginUsage(origin : string, callback : AsyncCallback<number>) : void

以回调方式异步获取指定源的Web SQL数据库的存储量,存储量以字节为单位。
Z
zhou-liting125 已提交
2932

Z
zhou-liting125 已提交
2933
**参数:**
2934 2935 2936 2937 2938
  | 参数名 | 参数类型 | 必填 | 说明 |
  |----------|----------|------|------|
  | origin | string | 是 | 指定源的字符串索引。 |
  | callback | AsyncCallback<number> | 是 | 指定源的存储量。 |

Z
zhou-liting125 已提交
2939
**示例:**
Z
zhou-liting125 已提交
2940 2941
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2942
  import web from '@ohos.web';
L
laosan_ted 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginUsage')
          .onClick(() => {
            web.WebStorage.getOriginUsage(this.origin, (error, usage) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              consloe.log('usage: ' + usage);
            })
          })
Z
zhou-liting125 已提交
2960
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2961 2962
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
2963
    }
L
laosan_ted 已提交
2964
  }
L
update  
laosan_ted 已提交
2965 2966
  ```

2967 2968 2969 2970
### getOriginUsage<sup>9+</sup>
static getOriginUsage(origin : string) : Promise<number>

以Promise方式异步获取指定源的Web SQL数据库的存储量,存储量以字节为单位。
Z
zhou-liting125 已提交
2971

Z
zhou-liting125 已提交
2972
**参数:**
2973 2974 2975 2976
  | 参数名 | 参数类型 | 必填 | 说明 |
  |----------|----------|------|------|
  | origin | string | 是 | 指定源的字符串索引。 |

Z
zhou-liting125 已提交
2977
**返回值:**
2978 2979 2980 2981
  | 类型 | 说明 |
  |------|------|
  | Promise<number> | Promise实例,用于获取指定源的存储量。 |

Z
zhou-liting125 已提交
2982
**示例:**
Z
zhou-liting125 已提交
2983 2984
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2985
  import web from '@ohos.web';
L
laosan_ted 已提交
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginUsage(this.origin)
              .then(usage => {
                console.log('usage: ' + usage);
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
3003
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3004 3005 3006 3007
          .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
3008 3009
  ```

3010
## WebStorageOrigin<sup>9+</sup>
Z
zhou-liting125 已提交
3011

3012
提供Web SQL数据库的使用信息。
Z
zhou-liting125 已提交
3013

Z
zhou-liting125 已提交
3014
**参数:**
3015 3016 3017 3018 3019
  | 参数名 | 参数类型 | 必填 | 说明 |
  |----------|----------|------|------|
  | origin | string | 是 | 指定源的字符串索引。 |
  | usage | number | 是 | 指定源的存储量。 |
  | quota | number | 是 | 指定源的存储配额。 |
Z
zhou-liting125 已提交
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077

## MessageLevel枚举说明

| 名称  | 描述       |
| ----- | :--------- |
| Debug | 调试级别。 |
| Error | 错误级别。 |
| Info  | 消息级别。 |
| Log   | 日志级别。 |
| Warn  | 警告级别。 |

## RenderExitReason枚举说明

onRenderExited接口返回的渲染进程退出的具体原因。

| 名称                       | 描述                         |
| -------------------------- | ---------------------------- |
| ProcessAbnormalTermination | 渲染进程异常退出。           |
| ProcessWasKilled           | 收到SIGKILL,或被手动终止。  |
| ProcessCrashed             | 渲染进程崩溃退出,如段错误。 |
| ProcessOom                 | 程序内存不足。               |
| ProcessExitUnknown         | 其他原因。                   |

## MixedMode枚举说明

| 名称       | 描述                                                        |
| ---------- | ----------------------------------------------------------- |
| All        | 允许加载HTTP和HTTPS混合内容。所有不安全的内容都可以被加载。 |
| Compatible | 混合内容兼容性模式,部分不安全的内容可能被加载。            |
| None       | 不允许加载HTTP和HTTPS混合内容。                             |

## CacheMode枚举说明
| 名称    | 描述                                                         |
| ------- | ------------------------------------------------------------ |
| Default | 使用未过期的cache加载资源,如果cache中无该资源则从网络中获取。 |
| None    | 加载资源使用cache,如果cache中无该资源则从网络中获取。       |
| Online  | 加载资源不使用cache,全部从网络中获取。                      |
| Only    | 只从cache中加载资源。                                        |

## FileSelectorMode枚举说明
| 名称                 | 描述                 |
| -------------------- | -------------------- |
| FileOpenMode         | 打开上传单个文件。   |
| FileOpenMultipleMode | 打开上传多个文件。   |
| FileOpenFolderMode   | 打开上传文件夹模式。 |
| FileSaveMode         | 文件保存模式。       |

 ## HitTestType枚举说明

  | 名称          | 描述                                      |
  | ------------- | ----------------------------------------- |
  | EditText      | 可编辑的区域。                            |
  | Email         | 电子邮件地址。                            |
  | HttpAnchor    | 超链接,其src为http。                     |
  | HttpAnchorImg | 带有超链接的图片,其中超链接的src为http。 |
  | Img           | HTML::img标签。                           |
  | Map           | 地理地址。                                |
  | Unknown       | 未知内容。                                |