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

3
提供具有网页显示能力的Web组件,[@ohos.web.webview](../apis/js-apis-webview.md)提供web控制能力。
H
HelloCrease 已提交
4

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

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

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



## 接口

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

L
laosan_ted 已提交
21 22 23
> **说明:**
>
> 不支持转场动画。
L
lixiang 已提交
24
> 同一页面的多个web组件,必须绑定不同的WebviewController。
L
laosan_ted 已提交
25

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

H
HelloCrease 已提交
28 29
| 参数名        | 参数类型                                     | 必填   | 参数描述    |
| ---------- | ---------------------------------------- | ---- | ------- |
30
| src        | [ResourceStr](ts-types.md)               | 是    | 网页资源地址。如果访问本地资源文件,请使用$rawfile或者resource协议。如果加载应用包外沙箱路径的本地资源文件,请使用file://沙箱文件路径。 |
L
lixiang 已提交
31
| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | 是    | 控制器。从API Version 9开始,WebController不在维护,建议使用WebviewController替代。 |
Z
zengyawen 已提交
32

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

L
laosan_ted 已提交
35
  加载在线网页
Z
zhou-liting125 已提交
36 37
  ```ts
  // xxx.ets
L
lixiang 已提交
38 39
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
40
  @Entry
L
update  
laosan_ted 已提交
41 42
  @Component
  struct WebComponent {
L
lixiang 已提交
43
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
44 45
    build() {
      Column() {
46
        Web({ src: 'www.example.com', controller: this.controller })
L
update  
laosan_ted 已提交
47 48 49 50
      }
    }
  }
  ```
L
lixiang 已提交
51 52

  加载本地网页
Y
yuhaoge 已提交
53 54 55 56 57 58 59
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
60
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yuhaoge 已提交
61 62
    build() {
      Column() {
L
lixiang 已提交
63 64
        //通过$rawfile加载本地资源文件
        Web({ src: $rawfile("index.html"), controller: this.controller })
Y
yuhaoge 已提交
65 66 67 68
      }
    }
  }
  ```
Z
zengyawen 已提交
69

L
laosan_ted 已提交
70 71
  ```ts
  // xxx.ets
L
lixiang 已提交
72 73
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
74 75 76
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
77
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
78 79
    build() {
      Column() {
L
lixiang 已提交
80
        //通过resource协议加载本地资源文件
L
lixiang 已提交
81
        Web({ src: "resource://rawfile/index.html", controller: this.controller })
L
laosan_ted 已提交
82 83 84 85 86
      }
    }
  }
  ```

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  加载沙箱路径下的本地资源文件

  1.通过[globalthis](../../application-models/uiability-data-sync-with-ui.md#uiability和page之间使用globalthis)获取沙箱路径。
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  let url = 'file://' + globalThis.filesDir + '/xxx.html'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        // 加载沙箱路径文件。
        Web({ src: url, controller: this.controller })
      }
    }
  }
  ```

  2.修改MainAbility.ts。
  以filesDir为例,获取沙箱路径。若想获取其他路径,请参考[应用开发路径](../../application-models/application-context-stage.md#获取应用开发路径)
  ```ts
  // xxx.ts
  import UIAbility from '@ohos.app.ability.UIAbility';
  import web_webview from '@ohos.web.webview';

  export default class EntryAbility extends UIAbility {
      onCreate(want, launchParam) {
          // 通过在globalThis对象上绑定filesDir,可以实现UIAbility组件与UI之间的数据同步。
          globalThis.filesDir = this.context.filesDir
          console.log("Sandbox path is " + globalThis.filesDir)
      }
  }
  ```

L
laosan_ted 已提交
124 125 126 127 128 129 130 131 132
  ```html
  <!-- index.html -->
  <!DOCTYPE html>
  <html>
      <body>
          <p>Hello World</p>
      </body>
  </html>
  ```
Z
zengyawen 已提交
133

L
update  
laosan_ted 已提交
134
## 属性
Z
zhou-liting125 已提交
135

H
HelloCrease 已提交
136
通用属性仅支持[width](ts-universal-attributes-size.md#属性)[height](ts-universal-attributes-size.md#属性)[padding](ts-universal-attributes-size.md#属性)[margin](ts-universal-attributes-size.md#属性)[border](ts-universal-attributes-border.md#属性)
Z
zhou-liting125 已提交
137 138 139 140 141 142 143

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

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

Z
zhou-liting125 已提交
144
**参数:**
L
laosan_ted 已提交
145

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

Z
zhou-liting125 已提交
150
**示例:**
L
laosan_ted 已提交
151

Z
zhou-liting125 已提交
152 153
  ```ts
  // xxx.ets
L
lixiang 已提交
154 155
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
156
  @Entry
L
update  
laosan_ted 已提交
157 158
  @Component
  struct WebComponent {
L
lixiang 已提交
159
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
160 161
    build() {
      Column() {
162 163
        Web({ src: 'www.example.com', controller: this.controller })
          .domStorageAccess(true)
L
update  
laosan_ted 已提交
164 165 166 167 168
      }
    }
  }
  ```

Z
zhou-liting125 已提交
169 170 171 172
### fileAccess

fileAccess(fileAccess: boolean)

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

Z
zhou-liting125 已提交
175
**参数:**
L
laosan_ted 已提交
176

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

Z
zhou-liting125 已提交
181
**示例:**
L
laosan_ted 已提交
182

Z
zhou-liting125 已提交
183 184
  ```ts
  // xxx.ets
L
lixiang 已提交
185 186
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
187
  @Entry
L
update  
laosan_ted 已提交
188 189
  @Component
  struct WebComponent {
L
lixiang 已提交
190
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
191 192
    build() {
      Column() {
193
        Web({ src: 'www.example.com', controller: this.controller })
194
          .fileAccess(true)
L
update  
laosan_ted 已提交
195 196 197 198 199
      }
    }
  }
  ```

Z
zhou-liting125 已提交
200 201 202 203 204 205
### imageAccess

imageAccess(imageAccess: boolean)

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

Z
zhou-liting125 已提交
206
**参数:**
L
laosan_ted 已提交
207

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

Z
zhou-liting125 已提交
212
**示例:**
Z
zhou-liting125 已提交
213 214
  ```ts
  // xxx.ets
L
lixiang 已提交
215 216
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
217
  @Entry
L
update  
laosan_ted 已提交
218 219
  @Component
  struct WebComponent {
L
lixiang 已提交
220
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
221 222
    build() {
      Column() {
223 224
        Web({ src: 'www.example.com', controller: this.controller })
          .imageAccess(true)
L
update  
laosan_ted 已提交
225 226 227 228 229
      }
    }
  }
  ```

Z
zhou-liting125 已提交
230 231 232
### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
L
lixiang 已提交
233
    controller: WebviewController | WebController})
Z
zhou-liting125 已提交
234 235 236

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

Z
zhou-liting125 已提交
237
**参数:**
L
laosan_ted 已提交
238

H
HelloCrease 已提交
239 240 241 242 243
| 参数名        | 参数类型                                     | 必填   | 默认值  | 参数描述                      |
| ---------- | ---------------------------------------- | ---- | ---- | ------------------------- |
| object     | object                                   | 是    | -    | 参与注册的对象。只能声明方法,不能声明属性。    |
| name       | string                                   | 是    | -    | 注册对象的名称,与window中调用的对象名一致。 |
| methodList | Array\<string\>                          | 是    | -    | 参与注册的应用侧JavaScript对象的方法。  |
L
lixiang 已提交
244
| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | 是    | -    | 控制器。从API Version 9开始,WebController不在维护,建议使用WebviewController替代。 |
245

Z
zhou-liting125 已提交
246
**示例:**
L
laosan_ted 已提交
247

Y
yuhaoge 已提交
248 249 250 251 252 253 254
  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
255
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yuhaoge 已提交
256 257
    testObj = {
      test: (data1, data2, data3) => {
Y
yamila 已提交
258 259 260 261
        console.log("data1:" + data1)
        console.log("data2:" + data2)
        console.log("data3:" + data3)
        return "AceString"
Y
yuhaoge 已提交
262 263
      },
      toString: () => {
Y
yamila 已提交
264
        console.log('toString' + "interface instead.")
Y
yuhaoge 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      }
    }
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,
            name: "objName",
            methodList: ["test", "toString"],
            controller: this.controller,
        })
      }
    }
  }
  ```
L
update  
laosan_ted 已提交
281

Z
zhou-liting125 已提交
282 283 284 285 286 287
### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

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

Z
zhou-liting125 已提交
288
**参数:**
L
laosan_ted 已提交
289

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

Z
zhou-liting125 已提交
294
**示例:**
L
laosan_ted 已提交
295

Z
zhou-liting125 已提交
296 297
  ```ts
  // xxx.ets
L
lixiang 已提交
298 299
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
300
  @Entry
L
update  
laosan_ted 已提交
301 302
  @Component
  struct WebComponent {
L
lixiang 已提交
303
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
304 305
    build() {
      Column() {
306 307
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
L
update  
laosan_ted 已提交
308 309 310 311 312
      }
    }
  }
  ```

Z
zhou-liting125 已提交
313 314 315 316 317 318
### mixedMode

mixedMode(mixedMode: MixedMode)

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

Z
zhou-liting125 已提交
319
**参数:**
L
laosan_ted 已提交
320

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

Z
zhou-liting125 已提交
325
**示例:**
L
laosan_ted 已提交
326

Z
zhou-liting125 已提交
327 328
  ```ts
  // xxx.ets
L
lixiang 已提交
329 330
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
331
  @Entry
L
update  
laosan_ted 已提交
332 333
  @Component
  struct WebComponent {
L
lixiang 已提交
334
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
335
    @State mode: MixedMode = MixedMode.All
L
update  
laosan_ted 已提交
336 337
    build() {
      Column() {
338 339
        Web({ src: 'www.example.com', controller: this.controller })
          .mixedMode(this.mode)
L
update  
laosan_ted 已提交
340 341 342 343 344
      }
    }
  }
  ```

Z
zhou-liting125 已提交
345 346 347 348 349 350
### onlineImageAccess

onlineImageAccess(onlineImageAccess: boolean)

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

Z
zhou-liting125 已提交
351
**参数:**
L
laosan_ted 已提交
352

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

Z
zhou-liting125 已提交
357
**示例:**
L
laosan_ted 已提交
358

Z
zhou-liting125 已提交
359 360
  ```ts
  // xxx.ets
L
lixiang 已提交
361 362
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
363
  @Entry
L
update  
laosan_ted 已提交
364 365
  @Component
  struct WebComponent {
L
lixiang 已提交
366
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
367 368
    build() {
      Column() {
369 370
        Web({ src: 'www.example.com', controller: this.controller })
          .onlineImageAccess(true)
L
update  
laosan_ted 已提交
371 372 373 374 375
      }
    }
  }
  ```

Z
zhou-liting125 已提交
376 377 378 379 380 381
### zoomAccess

zoomAccess(zoomAccess: boolean)

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

Z
zhou-liting125 已提交
382
**参数:**
L
laosan_ted 已提交
383

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

Z
zhou-liting125 已提交
388
**示例:**
L
laosan_ted 已提交
389

Z
zhou-liting125 已提交
390 391
  ```ts
  // xxx.ets
L
lixiang 已提交
392 393
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
394
  @Entry
L
update  
laosan_ted 已提交
395 396
  @Component
  struct WebComponent {
L
lixiang 已提交
397
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
398 399
    build() {
      Column() {
400 401
        Web({ src: 'www.example.com', controller: this.controller })
          .zoomAccess(true)
L
update  
laosan_ted 已提交
402 403 404 405 406
      }
    }
  }
  ```

Z
zhou-liting125 已提交
407 408 409 410 411 412
### overviewModeAccess

overviewModeAccess(overviewModeAccess: boolean)

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

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

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

Z
zhou-liting125 已提交
419
**示例:**
L
laosan_ted 已提交
420

Z
zhou-liting125 已提交
421 422
  ```ts
  // xxx.ets
L
lixiang 已提交
423 424
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
425
  @Entry
L
update  
laosan_ted 已提交
426 427
  @Component
  struct WebComponent {
L
lixiang 已提交
428
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
429 430
    build() {
      Column() {
431 432
        Web({ src: 'www.example.com', controller: this.controller })
          .overviewModeAccess(true)
L
update  
laosan_ted 已提交
433 434 435 436 437
      }
    }
  }
  ```

Z
zhou-liting125 已提交
438 439 440 441 442 443
### databaseAccess

databaseAccess(databaseAccess: boolean)

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

Z
zhou-liting125 已提交
444
**参数:**
L
laosan_ted 已提交
445

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

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

Z
zhou-liting125 已提交
452 453
  ```ts
  // xxx.ets
L
lixiang 已提交
454 455
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
456
  @Entry
L
update  
laosan_ted 已提交
457 458
  @Component
  struct WebComponent {
L
lixiang 已提交
459
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
460 461
    build() {
      Column() {
462 463
        Web({ src: 'www.example.com', controller: this.controller })
          .databaseAccess(true)
L
update  
laosan_ted 已提交
464 465 466 467 468
      }
    }
  }
  ```

L
laosan_ted 已提交
469 470 471 472 473 474 475
### geolocationAccess

geolocationAccess(geolocationAccess: boolean)

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

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

H
HelloCrease 已提交
477 478 479
| 参数名               | 参数类型    | 必填   | 默认值  | 参数描述            |
| ----------------- | ------- | ---- | ---- | --------------- |
| geolocationAccess | boolean | 是    | true | 设置是否开启获取地理位置权限。 |
L
laosan_ted 已提交
480 481

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

L
laosan_ted 已提交
483 484
  ```ts
  // xxx.ets
L
lixiang 已提交
485 486
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
487 488 489
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
490
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
491 492
    build() {
      Column() {
493 494
        Web({ src: 'www.example.com', controller: this.controller })
          .geolocationAccess(true)
L
laosan_ted 已提交
495 496 497 498 499
      }
    }
  }
  ```

L
laosan_ted 已提交
500 501 502 503
### mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

L
lixiang 已提交
504
设置有声视频播放是否需要用户手动点击,静音视频播放不受该接口管控,默认需要。
L
laosan_ted 已提交
505 506 507

**参数:**

H
HelloCrease 已提交
508 509
| 参数名    | 参数类型    | 必填   | 默认值  | 参数描述              |
| ------ | ------- | ---- | ---- | ----------------- |
L
laosan_ted 已提交
510
| access | boolean | 是    | true | 设置有声视频播放是否需要用户手动点击。 |
L
laosan_ted 已提交
511 512 513 514 515

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
516 517
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
518 519 520
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
521
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
522
    @State access: boolean = true
L
laosan_ted 已提交
523 524 525 526 527 528 529 530 531
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .mediaPlayGestureAccess(this.access)
      }
    }
  }
  ```

X
xiongjun_gitee 已提交
532 533 534 535 536
### multiWindowAccess<sup>9+</sup>

multiWindowAccess(multiWindow: boolean)

设置是否开启多窗口权限,默认不开启。
537
使能多窗口权限时,需要实现onWindowNew事件,示例代码参考[onWindowNew事件](#onwindownew9)
X
xiongjun_gitee 已提交
538 539 540

**参数:**

H
HelloCrease 已提交
541 542
| 参数名         | 参数类型    | 必填   | 默认值   | 参数描述         |
| ----------- | ------- | ---- | ----- | ------------ |
X
xiongjun_gitee 已提交
543 544 545 546 547 548
| multiWindow | boolean | 是    | false | 设置是否开启多窗口权限。 |

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
549 550
  import web_webview from '@ohos.web.webview'

X
xiongjun_gitee 已提交
551 552 553
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
554
    controller: web_webview.WebviewController = new web_webview.WebviewController()
X
xiongjun_gitee 已提交
555 556 557
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
558
        .multiWindowAccess(false)
X
xiongjun_gitee 已提交
559 560 561 562 563
      }
    }
  }
  ```

L
laosan_ted 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
### horizontalScrollBarAccess<sup>9+</sup>

horizontalScrollBarAccess(horizontalScrollBar: boolean)

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
580 581
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
582 583 584
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
585
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .horizontalScrollBarAccess(true)
      }
    }
  }
  ```

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

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

verticalScrollBarAccess(verticalScrollBar: boolean)

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
633 634
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
635 636 637
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
638
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .verticalScrollBarAccess(true)
      }
    }
  }
  ```

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


Z
zhou-liting125 已提交
671 672 673 674 675 676
### cacheMode

cacheMode(cacheMode: CacheMode)

设置缓存模式。

Z
zhou-liting125 已提交
677
**参数:**
L
laosan_ted 已提交
678

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

Z
zhou-liting125 已提交
683
**示例:**
L
laosan_ted 已提交
684

Z
zhou-liting125 已提交
685 686
  ```ts
  // xxx.ets
L
lixiang 已提交
687 688
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
689
  @Entry
L
update  
laosan_ted 已提交
690 691
  @Component
  struct WebComponent {
L
lixiang 已提交
692
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
693
    @State mode: CacheMode = CacheMode.None
L
update  
laosan_ted 已提交
694 695
    build() {
      Column() {
696 697
        Web({ src: 'www.example.com', controller: this.controller })
          .cacheMode(this.mode)
L
update  
laosan_ted 已提交
698 699 700 701 702
      }
    }
  }
  ```

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

L
laosan_ted 已提交
705
textZoomRatio(textZoomRatio: number)
Z
zhou-liting125 已提交
706 707 708

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

Z
zhou-liting125 已提交
709
**参数:**
L
laosan_ted 已提交
710

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

Z
zhou-liting125 已提交
715
**示例:**
L
laosan_ted 已提交
716

Z
zhou-liting125 已提交
717 718
  ```ts
  // xxx.ets
L
lixiang 已提交
719 720
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
721
  @Entry
L
update  
laosan_ted 已提交
722 723
  @Component
  struct WebComponent {
L
lixiang 已提交
724
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
725
    @State atio: number = 150
L
update  
laosan_ted 已提交
726 727
    build() {
      Column() {
728 729
        Web({ src: 'www.example.com', controller: this.controller })
          .textZoomRatio(this.atio)
L
update  
laosan_ted 已提交
730 731 732 733 734
      }
    }
  }
  ```

L
laosan_ted 已提交
735 736 737 738 739 740 741 742
### initialScale<sup>9+</sup>

initialScale(percent: number)

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

**参数:**

H
HelloCrease 已提交
743 744 745
| 参数名     | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------- | ------ | ---- | ---- | --------------- |
| percent | number | 是    | 100  | 要设置的整体页面的缩放百分比。 |
L
laosan_ted 已提交
746 747 748 749 750

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
751 752
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
753 754 755
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
756
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
757 758 759 760 761 762 763 764 765 766
    @State percent: number = 100
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .initialScale(this.percent)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
767 768 769 770 771 772
### userAgent

userAgent(userAgent: string)

设置用户代理。

Z
zhou-liting125 已提交
773
**参数:**
L
laosan_ted 已提交
774

775 776 777
| 参数名       | 参数类型   | 必填   | 默认值  | 参数描述      |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | 是    | -    | 要设置的用户代理。 |
Z
zengyawen 已提交
778

Z
zhou-liting125 已提交
779
**示例:**
L
laosan_ted 已提交
780

Z
zhou-liting125 已提交
781 782
  ```ts
  // xxx.ets
L
lixiang 已提交
783 784
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
785
  @Entry
L
update  
laosan_ted 已提交
786 787
  @Component
  struct WebComponent {
L
lixiang 已提交
788
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
789
    @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 已提交
790 791
    build() {
      Column() {
792 793
        Web({ src: 'www.example.com', controller: this.controller })
          .userAgent(this.userAgent)
L
update  
laosan_ted 已提交
794 795 796 797 798
      }
    }
  }
  ```

C
chensi10 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
### blockNetwork<sup>9+</sup>

blockNetwork(block: boolean)

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
C
chensi10 已提交
815
  import web_webview from '@ohos.web.webview'
C
chensi10 已提交
816 817 818
  @Entry
  @Component
  struct WebComponent {
C
chensi10 已提交
819
    controller: web_webview.WebviewController = new web_webview.WebviewController()
C
chensi10 已提交
820 821 822 823
    @State block: boolean = true
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
C
chensi10 已提交
824
          .blockNetwork(this.block)
C
chensi10 已提交
825 826 827 828 829 830 831 832 833
      }
    }
  }
  ```

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

defaultFixedFontSize(size: number)

834
设置网页的默认等宽字体大小。
C
chensi10 已提交
835 836 837 838 839

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                     |
| ------ | -------- | ---- | ------ | ---------------------------- |
840
| size   | number   | 是   | 13     | 设置网页的默认等宽字体大小,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
841 842 843 844 845 846 847 848 849 850

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
851
    @State fontSize: number = 16
C
chensi10 已提交
852 853 854
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
855
          .defaultFixedFontSize(this.fontSize)
C
chensi10 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
      }
    }
  }
  ```

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

defaultFontSize(size: number)

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

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                 |
| ------ | -------- | ---- | ------ | ------------------------ |
871
| size   | number   | 是   | 16     | 设置网页的默认字体大小,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
872 873 874 875 876 877 878 879 880 881

**示例:**

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

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

minFontSize(size: number)

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

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述                 |
| ------ | -------- | ---- | ------ | ------------------------ |
902
| size   | number   | 是   | 8      | 设置网页字体大小最小值,单位px。输入值的范围为-2^31到2^31-1,实际渲染时超过72的值按照72进行渲染,低于1的值按照1进行渲染。  |
C
chensi10 已提交
903 904 905 906 907 908 909 910 911 912

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
913
    @State fontSize: number = 13
C
chensi10 已提交
914 915 916
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
917
          .minFontSize(this.fontSize)
C
chensi10 已提交
918 919 920 921 922
      }
    }
  }
  ```

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
### minLogicalFontSize<sup>9+</sup>

minLogicalFontSize(size: number)

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
944
    @State fontSize: number = 13
945 946 947
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
948
          .minLogicalFontSize(this.fontSize)
949 950 951 952 953 954
      }
    }
  }
  ```


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

webFixedFont(family: string)

设置网页的fixed font字体库。

**参数:**

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

**示例:**

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

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

webSansSerifFont(family: string)

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

**参数:**

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

**示例:**

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

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

webSerifFont(family: string)

设置网页的serif font字体库。

**参数:**

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

**示例:**

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

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

webStandardFont(family: string)

设置网页的standard font字体库。

**参数:**

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

**示例:**

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

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

webFantasyFont(family: string)

设置网页的fantasy font字体库。

**参数:**

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

**示例:**

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

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

webCursiveFont(family: string)

设置网页的cursive font字体库。

**参数:**

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

**示例:**

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

Y
yuhaoge 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
### darkMode<sup>9+</sup>

darkMode(mode: WebDarkMode)

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

**参数:**

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

**示例:**

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

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

forceDarkAccess(access: boolean)

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

**参数:**

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

**示例:**

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

1205 1206 1207 1208
### pinchSmooth<sup>9+</sup>

pinchSmooth(isEnabled: boolean)

L
lixiang 已提交
1209
设置网页是否开启捏合流畅模式,默认不开启。
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

**参数:**

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

**示例:**

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

1235
### allowWindowOpenMethod<sup>10+</sup>
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

allowWindowOpenMethod(flag: boolean)

设置网页是否可以通过JavaScript自动打开新窗口。

该属性为true时,可通过JavaScript自动打开新窗口。该属性为false时,用户行为仍可通过JavaScript自动打开新窗口,但非用户行为不能通过JavaScript自动打开新窗口。此处的用户行为是指用户在5秒内请求打开新窗口(window.open)。

该属性仅在[javaScriptAccess](#javascriptaccess)开启时生效。

该属性在[multiWindowAccess](#multiwindowaccess9)开启时打开新窗口,关闭时打开本地窗口。

该属性的默认值与系统属性persist.web.allowWindowOpenMethod.enabled 保持一致,如果未设置系统属性则默认值为false。

检查系统配置项persist.web.allowWindowOpenMethod.enabled 是否开启。

通过`hdc shell param get persist.web.allowWindowOpenMethod.enabled` 查看,若配置项为0或不存在,
可通过命令`hdc shell param set persist.web.allowWindowOpenMethod.enabled 1` 开启配置。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | ------- | ---- | ----- | ------------------ |
| flag | boolean | 是   | 默认值与系统参数关联,当系统参数persist.web.allowWindowOpenMethod.enabled为true时,默认值为true, 否则为false  | 网页是否可以通过JavaScript自动打开窗口。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  //在同一page页有两个web组件。在WebComponent新开窗口时,会跳转到NewWebViewComp。
  @CustomDialog
  struct NewWebViewComp {
  controller: CustomDialogController
  webviewController1: web_webview.WebviewController
  build() {
      Column() {
        Web({ src: "", controller: this.webviewController1 })
          .javaScriptAccess(true)
          .multiWindowAccess(false)
          .onWindowExit(()=> {
            console.info("NewWebViewComp onWindowExit")
            this.controller.close()
          })
        }
    }
  }

1283 1284 1285 1286
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1287
    dialogController: CustomDialogController = null
1288 1289 1290
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
          .javaScriptAccess(true)
          //需要使能multiWindowAccess
          .multiWindowAccess(true)
          .allowWindowOpenMethod(true)
          .onWindowNew((event) => {
            if (this.dialogController) {
              this.dialogController.close()
            }
            let popController:web_webview.WebviewController = new web_webview.WebviewController()
            this.dialogController = new CustomDialogController({
              builder: NewWebViewComp({webviewController1: popController})
            })
            this.dialogController.open()
            //将新窗口对应WebviewController返回给Web内核。
            //如果不需要打开新窗口请调用event.handler.setWebController接口设置成null。
            //若不调用event.handler.setWebController接口,会造成render进程阻塞。
            event.handler.setWebController(popController)
          })
1309 1310 1311 1312 1313
      }
    }
  }
  ```

Y
yuhaoge 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
### mediaOptions<sup>10+</sup>

mediaOptions(options: WebMediaOptions)

设置Web媒体播放的策略,其中包括:Web中的音频在重新获焦后能够自动续播的有效期、应用内多个Web实例的音频是否独占。

> **说明:**
>
> - 同一Web实例中的多个音频均视为同一音频。
> - 该媒体播放策略将同时管控有声视频。
> - 属性参数更新后需重新播放音频方可生效。
> - 建议为所有Web组件设置相同的audioExclusive值。

**参数:**

| 参数名 | 参数类型 | 必填 | 默认值  | 参数描述                       |
| ------ | ----------- | ---- | --------------- | ------------------ |
| options | [WebMediaOptions](#webmediaoptions10) | 是   | {resumeInterval: 0, audioExclusive: true} | 设置Web的媒体策略。其中,resumeInterval的默认值为0表示不自动续播。 |

**示例:**


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

Z
zengyawen 已提交
1353 1354
## 事件

L
liwenzhen 已提交
1355
不支持通用事件。
Z
zengyawen 已提交
1356

L
update  
laosan_ted 已提交
1357 1358 1359 1360 1361 1362
### onAlert

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

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

Z
zhou-liting125 已提交
1363
**参数:**
L
laosan_ted 已提交
1364

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

Z
zhou-liting125 已提交
1371
**返回值:**
L
laosan_ted 已提交
1372

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

Z
zhou-liting125 已提交
1377
**示例:**
L
laosan_ted 已提交
1378

Z
zhou-liting125 已提交
1379 1380
  ```ts
  // xxx.ets
L
lixiang 已提交
1381 1382
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1383
  @Entry
L
update  
laosan_ted 已提交
1384 1385
  @Component
  struct WebComponent {
L
lixiang 已提交
1386
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
1387 1388
    build() {
      Column() {
1389
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1390 1391
          .onAlert((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
1392
              title: 'onAlert',
L
laosan_ted 已提交
1393
              message: 'text',
L
laosan_ted 已提交
1394 1395 1396 1397 1398 1399 1400 1401
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1402 1403 1404 1405 1406 1407 1408 1409
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1410
            return true
L
update  
laosan_ted 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
          })
      }
    }
  }
  ```

### onBeforeUnload

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

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

Z
zhou-liting125 已提交
1423
**参数:**
L
laosan_ted 已提交
1424

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

Z
zhou-liting125 已提交
1431
**返回值:**
L
laosan_ted 已提交
1432

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

Z
zhou-liting125 已提交
1437
**示例:**
L
laosan_ted 已提交
1438

Z
zhou-liting125 已提交
1439 1440
  ```ts
  // xxx.ets
L
lixiang 已提交
1441 1442
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1443
  @Entry
L
update  
laosan_ted 已提交
1444 1445
  @Component
  struct WebComponent {
L
lixiang 已提交
1446
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1447

L
update  
laosan_ted 已提交
1448 1449
    build() {
      Column() {
Z
zhou-liting125 已提交
1450
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1451
          .onBeforeUnload((event) => {
Y
yamila 已提交
1452 1453
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
L
laosan_ted 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
            AlertDialog.show({
              title: 'onBeforeUnload',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1473
            return true
L
laosan_ted 已提交
1474 1475
          })
      }
L
update  
laosan_ted 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
    }
  }
  ```

### onConfirm

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

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

Z
zhou-liting125 已提交
1486
**参数:**
L
laosan_ted 已提交
1487

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

Z
zhou-liting125 已提交
1494
**返回值:**
L
laosan_ted 已提交
1495

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

Z
zhou-liting125 已提交
1500
**示例:**
L
laosan_ted 已提交
1501

Z
zhou-liting125 已提交
1502 1503
  ```ts
  // xxx.ets
L
lixiang 已提交
1504 1505
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1506
  @Entry
L
update  
laosan_ted 已提交
1507 1508
  @Component
  struct WebComponent {
L
lixiang 已提交
1509
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1510

L
update  
laosan_ted 已提交
1511 1512
    build() {
      Column() {
Z
zhou-liting125 已提交
1513
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1514
          .onConfirm((event) => {
Y
yamila 已提交
1515 1516 1517
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            console.log("event.result:" + event.result)
L
laosan_ted 已提交
1518
            AlertDialog.show({
L
laosan_ted 已提交
1519
              title: 'onConfirm',
L
laosan_ted 已提交
1520
              message: 'text',
L
laosan_ted 已提交
1521 1522 1523 1524 1525 1526 1527 1528
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1529 1530 1531 1532 1533 1534 1535 1536
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1537
            return true
L
update  
laosan_ted 已提交
1538
          })
L
laosan_ted 已提交
1539
      }
L
update  
laosan_ted 已提交
1540 1541 1542 1543 1544 1545 1546 1547
    }
  }
  ```

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

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

Z
zhou-liting125 已提交
1548
**参数:**
L
laosan_ted 已提交
1549

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

Z
zhou-liting125 已提交
1556
**返回值:**
L
laosan_ted 已提交
1557

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

Z
zhou-liting125 已提交
1562
**示例:**
L
laosan_ted 已提交
1563

Z
zhou-liting125 已提交
1564 1565
  ```ts
  // xxx.ets
L
lixiang 已提交
1566 1567
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1568
  @Entry
L
update  
laosan_ted 已提交
1569 1570
  @Component
  struct WebComponent {
L
lixiang 已提交
1571
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1572

L
update  
laosan_ted 已提交
1573 1574
    build() {
      Column() {
1575
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1576
          .onPrompt((event) => {
Y
yamila 已提交
1577 1578 1579
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
L
laosan_ted 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
            AlertDialog.show({
              title: 'onPrompt',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1599
            return true
L
laosan_ted 已提交
1600 1601
          })
      }
L
update  
laosan_ted 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
    }
  }
  ```

### onConsole

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

通知宿主应用JavaScript console消息。

Z
zhou-liting125 已提交
1612
**参数:**
L
laosan_ted 已提交
1613

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

Z
zhou-liting125 已提交
1618
**返回值:**
L
laosan_ted 已提交
1619

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

Z
zhou-liting125 已提交
1624
**示例:**
L
laosan_ted 已提交
1625

Z
zhou-liting125 已提交
1626 1627
  ```ts
  // xxx.ets
L
lixiang 已提交
1628 1629
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1630
  @Entry
L
update  
laosan_ted 已提交
1631 1632
  @Component
  struct WebComponent {
L
lixiang 已提交
1633
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1634

L
update  
laosan_ted 已提交
1635 1636
    build() {
      Column() {
Z
zhou-liting125 已提交
1637
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1638
          .onConsole((event) => {
Y
yamila 已提交
1639 1640 1641 1642 1643
            console.log('getMessage:' + event.message.getMessage())
            console.log('getSourceId:' + event.message.getSourceId())
            console.log('getLineNumber:' + event.message.getLineNumber())
            console.log('getMessageLevel:' + event.message.getMessageLevel())
            return false
L
laosan_ted 已提交
1644 1645
          })
      }
L
update  
laosan_ted 已提交
1646 1647 1648 1649 1650 1651 1652 1653
    }
  }
  ```

### onDownloadStart

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

Z
zhou-liting125 已提交
1654
**参数:**
L
laosan_ted 已提交
1655

1656 1657 1658 1659 1660 1661
| 参数名                | 参数类型          | 参数描述                                |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | 文件下载的URL。                           |
| contentDisposition | string        | 服务器返回的 Content-Disposition响应头,可能为空。 |
| mimetype           | string        | 服务器返回内容媒体类型(MIME)信息。                |
| contentLength      | contentLength | 服务器返回文件的长度。                         |
L
update  
laosan_ted 已提交
1662

Z
zhou-liting125 已提交
1663
**示例:**
L
laosan_ted 已提交
1664

Z
zhou-liting125 已提交
1665 1666
  ```ts
  // xxx.ets
L
lixiang 已提交
1667 1668
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1669
  @Entry
L
update  
laosan_ted 已提交
1670 1671
  @Component
  struct WebComponent {
L
lixiang 已提交
1672
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1673

L
update  
laosan_ted 已提交
1674 1675
    build() {
      Column() {
Z
zhou-liting125 已提交
1676
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1677
          .onDownloadStart((event) => {
Y
yamila 已提交
1678 1679 1680 1681 1682
            console.log('url:' + event.url)
            console.log('userAgent:' + event.userAgent)
            console.log('contentDisposition:' + event.contentDisposition)
            console.log('contentLength:' + event.contentLength)
            console.log('mimetype:' + event.mimetype)
L
laosan_ted 已提交
1683 1684
          })
      }
L
update  
laosan_ted 已提交
1685 1686 1687 1688 1689 1690 1691 1692
    }
  }
  ```

### onErrorReceive

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

L
1111  
lixiang 已提交
1693
网页加载遇到错误时触发该回调。出于性能考虑,建议此回调中尽量执行简单逻辑。在无网络的情况下,触发此回调。
L
update  
laosan_ted 已提交
1694

Z
zhou-liting125 已提交
1695
**参数:**
L
laosan_ted 已提交
1696

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

Z
zhou-liting125 已提交
1702
**示例:**
L
laosan_ted 已提交
1703

Z
zhou-liting125 已提交
1704 1705
  ```ts
  // xxx.ets
L
lixiang 已提交
1706 1707
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1708
  @Entry
L
update  
laosan_ted 已提交
1709 1710
  @Component
  struct WebComponent {
L
lixiang 已提交
1711
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1712

L
update  
laosan_ted 已提交
1713 1714
    build() {
      Column() {
Z
zhou-liting125 已提交
1715
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1716
          .onErrorReceive((event) => {
Y
yamila 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725
            console.log('getErrorInfo:' + event.error.getErrorInfo())
            console.log('getErrorCode:' + event.error.getErrorCode())
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
L
laosan_ted 已提交
1726
            for (let i of result) {
Y
yamila 已提交
1727
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
L
laosan_ted 已提交
1728 1729 1730
            }
          })
      }
L
update  
laosan_ted 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
    }
  }
  ```

### onHttpErrorReceive

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

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

Z
zhou-liting125 已提交
1741
**参数:**
L
laosan_ted 已提交
1742

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

Z
zhou-liting125 已提交
1748
**示例:**
L
laosan_ted 已提交
1749

Z
zhou-liting125 已提交
1750 1751
  ```ts
  // xxx.ets
L
lixiang 已提交
1752 1753
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1754
  @Entry
L
update  
laosan_ted 已提交
1755 1756
  @Component
  struct WebComponent {
L
lixiang 已提交
1757
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1758

L
update  
laosan_ted 已提交
1759 1760
    build() {
      Column() {
Z
zhou-liting125 已提交
1761
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1762
          .onHttpErrorReceive((event) => {
Y
yamila 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
            console.log('url:' + event.request.getRequestUrl())
            console.log('isMainFrame:' + event.request.isMainFrame())
            console.log('isRedirect:' + event.request.isRedirect())
            console.log('isRequestGesture:' + event.request.isRequestGesture())
            console.log('getResponseData:' + event.response.getResponseData())
            console.log('getResponseEncoding:' + event.response.getResponseEncoding())
            console.log('getResponseMimeType:' + event.response.getResponseMimeType())
            console.log('getResponseCode:' + event.response.getResponseCode())
            console.log('getReasonMessage:' + event.response.getReasonMessage())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
L
laosan_ted 已提交
1774
            for (let i of result) {
Y
yamila 已提交
1775
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1776
            }
Y
yamila 已提交
1777 1778
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
L
laosan_ted 已提交
1779
            for (let i of resph) {
Y
yamila 已提交
1780
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1781 1782 1783
            }
          })
      }
L
update  
laosan_ted 已提交
1784 1785 1786 1787 1788 1789 1790 1791
    }
  }
  ```

### onPageBegin

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

1792

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

Z
zhou-liting125 已提交
1795
**参数:**
L
laosan_ted 已提交
1796

1797 1798 1799
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1800

Z
zhou-liting125 已提交
1801
**示例:**
L
laosan_ted 已提交
1802

Z
zhou-liting125 已提交
1803 1804
  ```ts
  // xxx.ets
L
lixiang 已提交
1805 1806
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1807
  @Entry
L
update  
laosan_ted 已提交
1808 1809
  @Component
  struct WebComponent {
L
lixiang 已提交
1810
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1811

L
update  
laosan_ted 已提交
1812 1813
    build() {
      Column() {
Z
zhou-liting125 已提交
1814
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1815
          .onPageBegin((event) => {
Y
yamila 已提交
1816
            console.log('url:' + event.url)
L
laosan_ted 已提交
1817 1818
          })
      }
L
update  
laosan_ted 已提交
1819 1820 1821 1822 1823 1824 1825 1826
    }
  }
  ```

### onPageEnd

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

1827

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

Z
zhou-liting125 已提交
1830
**参数:**
L
laosan_ted 已提交
1831

1832 1833 1834
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1835

Z
zhou-liting125 已提交
1836
**示例:**
L
laosan_ted 已提交
1837

Z
zhou-liting125 已提交
1838 1839
  ```ts
  // xxx.ets
L
lixiang 已提交
1840 1841
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1842
  @Entry
L
update  
laosan_ted 已提交
1843 1844
  @Component
  struct WebComponent {
L
lixiang 已提交
1845
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1846

L
update  
laosan_ted 已提交
1847 1848
    build() {
      Column() {
Z
zhou-liting125 已提交
1849
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1850
          .onPageEnd((event) => {
Y
yamila 已提交
1851
            console.log('url:' + event.url)
L
laosan_ted 已提交
1852 1853
          })
      }
L
update  
laosan_ted 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
    }
  }
  ```

### onProgressChange

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

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

Z
zhou-liting125 已提交
1864
**参数:**
L
laosan_ted 已提交
1865

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

L
laosan_ted 已提交
1870 1871
**示例:**

Z
zhou-liting125 已提交
1872 1873
  ```ts
  // xxx.ets
L
lixiang 已提交
1874 1875
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1876
  @Entry
L
update  
laosan_ted 已提交
1877 1878
  @Component
  struct WebComponent {
L
lixiang 已提交
1879
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1880

L
update  
laosan_ted 已提交
1881 1882
    build() {
      Column() {
Z
zhou-liting125 已提交
1883
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1884 1885 1886 1887
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
L
update  
laosan_ted 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
    }
  }
  ```

### onTitleReceive

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

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

Z
zhou-liting125 已提交
1898
**参数:**
L
laosan_ted 已提交
1899

1900 1901 1902
| 参数名   | 参数类型   | 参数描述          |
| ----- | ------ | ------------- |
| title | string | document标题内容。 |
L
update  
laosan_ted 已提交
1903

L
laosan_ted 已提交
1904 1905
**示例:**

Z
zhou-liting125 已提交
1906 1907
  ```ts
  // xxx.ets
L
lixiang 已提交
1908 1909
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1910
  @Entry
L
update  
laosan_ted 已提交
1911 1912
  @Component
  struct WebComponent {
L
lixiang 已提交
1913
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1914

L
update  
laosan_ted 已提交
1915 1916
    build() {
      Column() {
Z
zhou-liting125 已提交
1917
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1918 1919 1920 1921
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
L
update  
laosan_ted 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
    }
  }
  ```

### onRefreshAccessedHistory

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

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

Z
zhou-liting125 已提交
1932
**参数:**
L
laosan_ted 已提交
1933

H
HelloCrease 已提交
1934 1935 1936
| 参数名         | 参数类型    | 参数描述                                     |
| ----------- | ------- | ---------------------------------------- |
| url         | string  | 访问的url。                                  |
1937
| isRefreshed | boolean | true表示该页面是被重新加载的(调用[refresh<sup>9+</sup>](../apis/js-apis-webview.md#refresh)接口),false表示该页面是新加载的。 |
L
update  
laosan_ted 已提交
1938

L
laosan_ted 已提交
1939 1940
**示例:**

Z
zhou-liting125 已提交
1941 1942
  ```ts
  // xxx.ets
L
lixiang 已提交
1943 1944
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1945
  @Entry
L
update  
laosan_ted 已提交
1946 1947
  @Component
  struct WebComponent {
L
lixiang 已提交
1948
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1949

L
update  
laosan_ted 已提交
1950 1951
    build() {
      Column() {
Z
zhou-liting125 已提交
1952
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1953
          .onRefreshAccessedHistory((event) => {
Y
yamila 已提交
1954
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
L
laosan_ted 已提交
1955 1956
          })
      }
L
update  
laosan_ted 已提交
1957 1958 1959 1960
    }
  }
  ```

L
laosan_ted 已提交
1961
### onRenderExited<sup>9+</sup>
L
update  
laosan_ted 已提交
1962 1963 1964 1965 1966

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

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

Z
zhou-liting125 已提交
1967
**参数:**
L
laosan_ted 已提交
1968

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

L
laosan_ted 已提交
1973 1974
**示例:**

Z
zhou-liting125 已提交
1975 1976
  ```ts
  // xxx.ets
L
lixiang 已提交
1977 1978
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1979
  @Entry
L
update  
laosan_ted 已提交
1980 1981
  @Component
  struct WebComponent {
L
lixiang 已提交
1982
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1983

L
update  
laosan_ted 已提交
1984 1985
    build() {
      Column() {
L
laosan_ted 已提交
1986 1987
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
Y
yamila 已提交
1988
            console.log('reason:' + event.renderExitReason)
L
laosan_ted 已提交
1989 1990
          })
      }
L
update  
laosan_ted 已提交
1991 1992 1993 1994 1995 1996
    }
  }
  ```

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

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

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

Z
zhou-liting125 已提交
2001
**参数:**
L
laosan_ted 已提交
2002

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

L
laosan_ted 已提交
2008 2009
**返回值:**

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

L
laosan_ted 已提交
2014 2015
**示例:**

Z
zhou-liting125 已提交
2016 2017
  ```ts
  // xxx.ets
L
lixiang 已提交
2018 2019
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
2020
  @Entry
L
update  
laosan_ted 已提交
2021 2022
  @Component
  struct WebComponent {
L
lixiang 已提交
2023
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2024

L
update  
laosan_ted 已提交
2025 2026
    build() {
      Column() {
Z
zhou-liting125 已提交
2027
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
          .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 已提交
2044
              }
L
laosan_ted 已提交
2045
            })
Y
yamila 已提交
2046
            return true
L
update  
laosan_ted 已提交
2047
          })
L
laosan_ted 已提交
2048
      }
L
update  
laosan_ted 已提交
2049 2050 2051 2052
    }
  }
  ```

L
laosan_ted 已提交
2053 2054 2055 2056 2057 2058 2059 2060
### onResourceLoad<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2061 2062 2063
| 参数名  | 参数类型   | 参数描述           |
| ---- | ------ | -------------- |
| url  | string | 所加载的资源文件url信息。 |
L
laosan_ted 已提交
2064 2065 2066 2067 2068

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2069 2070
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
2071 2072 2073
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2074
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2075

L
laosan_ted 已提交
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onResourceLoad((event) => {
            console.log('onResourceLoad: ' + event.url)
          })
      }
    }
  }
  ```

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

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

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

**参数:**

H
HelloCrease 已提交
2095 2096
| 参数名      | 参数类型   | 参数描述         |
| -------- | ------ | ------------ |
L
laosan_ted 已提交
2097 2098 2099 2100 2101 2102 2103
| oldScale | number | 变化前的显示比例百分比。 |
| newScale | number | 变化后的显示比例百分比。 |

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2104 2105
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
2106 2107 2108
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2109
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2110

L
laosan_ted 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onScaleChange((event) => {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
          })
      }
    }
  }
  ```

2122
### onUrlLoadIntercept<sup>(deprecated)</sup>
L
update  
laosan_ted 已提交
2123 2124 2125

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

L
1111  
lixiang 已提交
2126
当Web组件加载url之前触发该回调,用于判断是否阻止此次访问。默认允许加载。
2127
从API version 10开始不在维护,建议使用[onLoadIntercept<sup>10+</sup>](#onloadintercept10)代替。
L
update  
laosan_ted 已提交
2128

Z
zhou-liting125 已提交
2129
**参数:**
L
laosan_ted 已提交
2130

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

Z
zhou-liting125 已提交
2135
**返回值:**
L
laosan_ted 已提交
2136

2137 2138 2139
| 类型      | 说明                       |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |
L
update  
laosan_ted 已提交
2140

L
laosan_ted 已提交
2141 2142
**示例:**

Z
zhou-liting125 已提交
2143 2144
  ```ts
  // xxx.ets
L
lixiang 已提交
2145 2146
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
2147
  @Entry
L
update  
laosan_ted 已提交
2148 2149
  @Component
  struct WebComponent {
L
lixiang 已提交
2150
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2151

L
update  
laosan_ted 已提交
2152 2153
    build() {
      Column() {
Z
zhou-liting125 已提交
2154
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2155 2156
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
Y
yamila 已提交
2157
            return true
L
laosan_ted 已提交
2158 2159
          })
      }
L
update  
laosan_ted 已提交
2160 2161 2162 2163 2164 2165
    }
  }
  ```

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

L
laosan_ted 已提交
2166
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
L
update  
laosan_ted 已提交
2167 2168 2169

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

Z
zhou-liting125 已提交
2170
**参数:**
L
laosan_ted 已提交
2171

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

Z
zhou-liting125 已提交
2176
**返回值:**
L
laosan_ted 已提交
2177

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

L
laosan_ted 已提交
2182 2183
**示例:**

Z
zhou-liting125 已提交
2184 2185
  ```ts
  // xxx.ets
L
lixiang 已提交
2186 2187
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
2188
  @Entry
L
update  
laosan_ted 已提交
2189 2190
  @Component
  struct WebComponent {
L
lixiang 已提交
2191
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2192 2193
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
L
laosan_ted 已提交
2194 2195 2196 2197 2198 2199 2200 2201 2202
    @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 已提交
2203 2204
    build() {
      Column() {
2205
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2206
          .onInterceptRequest((event) => {
Y
yamila 已提交
2207
            console.log('url:' + event.request.getRequestUrl())
L
laosan_ted 已提交
2208 2209 2210 2211 2212 2213 2214 2215
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
Y
yamila 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224
            var length = this.heads.push(head1)
            length = this.heads.push(head2)
            this.responseweb.setResponseHeader(this.heads)
            this.responseweb.setResponseData(this.webdata)
            this.responseweb.setResponseEncoding('utf-8')
            this.responseweb.setResponseMimeType('text/html')
            this.responseweb.setResponseCode(200)
            this.responseweb.setReasonMessage('OK')
            return this.responseweb
L
laosan_ted 已提交
2225
          })
L
update  
laosan_ted 已提交
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
      }
    }
  }
  ```

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

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

通知收到http auth认证请求。

Z
zhou-liting125 已提交
2237
**参数:**
L
laosan_ted 已提交
2238

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

Z
zhou-liting125 已提交
2245
**返回值:**
L
laosan_ted 已提交
2246

2247 2248 2249
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 返回false表示此次认证失败,否则成功。 |
L
update  
laosan_ted 已提交
2250

L
laosan_ted 已提交
2251 2252
**示例:**

Z
zhou-liting125 已提交
2253 2254
  ```ts
  // xxx.ets
L
laosan_ted 已提交
2255
  import web_webview from '@ohos.web.webview'
Z
zhou-liting125 已提交
2256
  @Entry
L
update  
laosan_ted 已提交
2257 2258
  @Component
  struct WebComponent {
L
lixiang 已提交
2259
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2260
    httpAuth: boolean = false
2261

L
update  
laosan_ted 已提交
2262 2263
    build() {
      Column() {
2264 2265 2266
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
2267
              title: 'onHttpAuthRequest',
2268
              message: 'text',
L
laosan_ted 已提交
2269 2270 2271
              primaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2272
                  event.handler.cancel()
L
laosan_ted 已提交
2273 2274 2275 2276
                }
              },
              secondaryButton: {
                value: 'ok',
2277
                action: () => {
Y
yamila 已提交
2278
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
2279
                  if (this.httpAuth == false) {
L
laosan_ted 已提交
2280
                    web_webview.WebDataBase.saveHttpAuthCredentials(
2281 2282 2283 2284 2285
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
Y
yamila 已提交
2286
                    event.handler.cancel()
2287
                  }
L
update  
laosan_ted 已提交
2288
                }
2289 2290
              },
              cancel: () => {
Y
yamila 已提交
2291
                event.handler.cancel()
L
update  
laosan_ted 已提交
2292
              }
2293
            })
Y
yamila 已提交
2294
            return true
L
update  
laosan_ted 已提交
2295
          })
Y
yu-shihao4 已提交
2296
      }
L
update  
laosan_ted 已提交
2297 2298 2299
    }
  }
  ```
2300 2301
### onSslErrorEventReceive<sup>9+</sup>

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

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

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

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

**示例:**
2314 2315 2316

  ```ts
  // xxx.ets
I
bugfix  
i-am-a-little-bird 已提交
2317
  import web_webview from '@ohos.web.webview'
2318 2319 2320
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2321
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2322

2323 2324 2325
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
I
bugfix  
i-am-a-little-bird 已提交
2326
          .onSslErrorEventReceive((event) => {
2327
            AlertDialog.show({
I
bugfix  
i-am-a-little-bird 已提交
2328
              title: 'onSslErrorEventReceive',
2329
              message: 'text',
I
bugfix  
i-am-a-little-bird 已提交
2330 2331
              primaryButton: {
                value: 'confirm',
2332
                action: () => {
Y
yamila 已提交
2333
                  event.handler.handleConfirm()
I
bugfix  
i-am-a-little-bird 已提交
2334 2335 2336 2337 2338
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2339
                  event.handler.handleCancel()
2340 2341 2342
                }
              },
              cancel: () => {
Y
yamila 已提交
2343
                event.handler.handleCancel()
2344 2345
              }
            })
Y
yamila 已提交
2346
            return true
2347 2348 2349 2350 2351 2352
          })
      }
    }
  }
  ```

I
i-am-a-little-bird 已提交
2353 2354 2355 2356
### onClientAuthenticationRequest<sup>9+</sup>

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

I
bugfix  
i-am-a-little-bird 已提交
2357
通知用户收到SSL客户端证书请求事件。
I
i-am-a-little-bird 已提交
2358 2359 2360

**参数:**

H
HelloCrease 已提交
2361 2362 2363 2364 2365 2366 2367
| 参数名      | 参数类型                                     | 参数描述            |
| -------- | ---------------------------------------- | --------------- |
| handler  | [ClientAuthenticationHandler](#clientauthenticationhandler9) | 通知Web组件用户操作行为。  |
| host     | string                                   | 请求证书服务器的主机名。    |
| port     | number                                   | 请求证书服务器的端口号。    |
| keyTypes | Array<string>                            | 可接受的非对称秘钥类型。    |
| issuers  | Array<string>                            | 与私钥匹配的证书可接受颁发者。 |
I
i-am-a-little-bird 已提交
2368 2369 2370

  **示例:**
  ```ts
2371
  // xxx.ets API9
I
i-am-a-little-bird 已提交
2372 2373 2374 2375
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2376
    controller: web_webview.WebviewController = new web_webview.WebviewController()
I
i-am-a-little-bird 已提交
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'onClientAuthenticationRequest',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
Y
yamila 已提交
2388
                  event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem")
I
i-am-a-little-bird 已提交
2389 2390 2391 2392 2393
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2394
                  event.handler.cancel()
I
i-am-a-little-bird 已提交
2395 2396 2397
                }
              },
              cancel: () => {
Y
yamila 已提交
2398
                event.handler.ignore()
I
i-am-a-little-bird 已提交
2399 2400
              }
            })
Y
yamila 已提交
2401
            return true
I
i-am-a-little-bird 已提交
2402 2403 2404 2405 2406 2407
          })
      }
    }
  }
  ```

2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
  ```ts
  // xxx.ets API10
  import web_webview from '@ohos.web.webview'
  import bundle from '@ohos.bundle'

  let uri = "";

  export default class CertManagerService {
    private static sInstance: CertManagerService;
    private authUri = "";

    public static getInstance(): CertManagerService {
      if (CertManagerService.sInstance == null) {
        CertManagerService.sInstance = new CertManagerService();
      }
      return CertManagerService.sInstance;
    }

    async grantAppPm(callback) {
      let message = '';
      //注:com.example.myapplication需要写实际应用名称
      let bundleInfo = await bundle.getBundleInfo("com.example.myapplication", bundle.BundleFlag.GET_BUNDLE_DEFAULT)
      let clientAppUid = bundleInfo.uid
      let appUid = clientAppUid.toString()

      //注:globalThis.AbilityContext需要在MainAbility.ts文件的onCreate函数里添加globalThis.AbilityContext = this.context
      await globalThis.AbilityContext.startAbilityForResult(
        {
          bundleName: "com.ohos.certmanager",
          abilityName: "MainAbility",
          uri: "requestAuthorize",
          parameters: {
            appUid: appUid, //传入申请应用的appUid
          }
        })
        .then((data) => {
          if (!data.resultCode) {
            this.authUri = data.want.parameters.authUri; //授权成功后获取返回的authUri
          }
        })
      message += "after grantAppPm authUri: " + this.authUri;
      uri = this.authUri;
      callback(message)
    }
  }

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController();
    @State message: string = 'Hello World' //message主要是调试观察使用
    certManager = CertManagerService.getInstance();

    build() {
      Row() {
        Column() {
          Row() {
            //第一步:需要先进行授权,获取到uri
            Button('GrantApp')
              .onClick(() => {
                this.certManager.grantAppPm((data) => {
                  this.message = data;
                });
              })
            //第二步:授权后,双向认证会通过onClientAuthenticationRequest回调将uri传给web进行认证
            Button("ClientCertAuth")
              .onClick(() => {
                this.controller.loadUrl('https://www.example2.com'); //支持双向认证的服务器网站
              })
          }

          Web({ src: 'https://www.example1.com', controller: this.controller })
            .fileAccess(true)
            .javaScriptAccess(true)
            .domStorageAccess(true)
            .onlineImageAccess(true)

          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'ClientAuth',
              message: 'Text',
              confirm: {
                value: 'Confirm',
                action: () => {
                  event.handler.confirm(uri);
                }
              },
              cancel: () => {
                event.handler.cancel();
              }
            })
          })
        }
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

2508 2509 2510 2511 2512 2513 2514
### onPermissionRequest<sup>9+</sup>

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

通知收到获取权限请求。

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

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

L
laosan_ted 已提交
2520 2521
**示例:**

2522 2523
  ```ts
  // xxx.ets
L
lixiang 已提交
2524 2525
  import web_webview from '@ohos.web.webview'

2526 2527 2528
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2529
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2530 2531
    build() {
      Column() {
2532 2533 2534 2535 2536
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
L
laosan_ted 已提交
2537 2538 2539
              primaryButton: {
                value: 'deny',
                action: () => {
Y
yamila 已提交
2540
                  event.request.deny()
L
laosan_ted 已提交
2541 2542 2543
                }
              },
              secondaryButton: {
2544 2545
                value: 'onConfirm',
                action: () => {
Y
yamila 已提交
2546
                  event.request.grant(event.request.getAccessibleResource())
2547 2548 2549
                }
              },
              cancel: () => {
Y
yamila 已提交
2550
                event.request.deny()
2551
              }
2552
            })
2553
          })
Y
yu-shihao4 已提交
2554
      }
2555 2556 2557
    }
  }
  ```
2558

2559 2560 2561 2562
### onContextMenuShow<sup>9+</sup>

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

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

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

H
HelloCrease 已提交
2567 2568 2569 2570
| 参数名    | 参数类型                                     | 参数描述        |
| ------ | ---------------------------------------- | ----------- |
| param  | [WebContextMenuParam](#webcontextmenuparam9) | 菜单相关参数。     |
| result | [WebContextMenuResult](#webcontextmenuresult9) | 菜单相应事件传入内核。 |
2571

Y
yu-shihao4 已提交
2572 2573
**返回值:**

H
HelloCrease 已提交
2574 2575
| 类型      | 说明                       |
| ------- | ------------------------ |
Y
yu-shihao4 已提交
2576 2577
| boolean | 自定义菜单返回true,默认菜单返回false。 |

L
laosan_ted 已提交
2578
**示例:**
Y
yu-shihao4 已提交
2579

2580 2581
  ```ts
  // xxx.ets
L
lixiang 已提交
2582 2583
  import web_webview from '@ohos.web.webview'

2584 2585 2586
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2587
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2588 2589
    build() {
      Column() {
2590
        Web({ src: 'www.example.com', controller: this.controller })
Y
yu-shihao4 已提交
2591
          .onContextMenuShow((event) => {
Y
yamila 已提交
2592 2593 2594
            console.info("x coord = " + event.param.x())
            console.info("link url = " + event.param.getLinkUrl())
            return true
2595 2596
        })
      }
2597 2598 2599
    }
  }
  ```
2600

L
laosan_ted 已提交
2601 2602 2603 2604 2605 2606 2607 2608
### onScroll<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2609 2610
| 参数名     | 参数类型   | 参数描述         |
| ------- | ------ | ------------ |
L
1111  
lixiang 已提交
2611 2612
| xOffset | number | 以网页最左端为基准,水平滚动条滚动所在位置。 |
| yOffset | number | 以网页最上端为基准,竖直滚动条滚动所在位置。 |
L
laosan_ted 已提交
2613 2614 2615 2616 2617

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2618 2619
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
2620 2621 2622
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2623
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2624 2625 2626 2627
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .onScroll((event) => {
Y
yamila 已提交
2628 2629
            console.info("x = " + event.xOffset)
            console.info("y = " + event.yOffset)
L
laosan_ted 已提交
2630 2631 2632 2633 2634 2635
        })
      }
    }
  }
  ```

2636 2637 2638 2639
### onGeolocationShow

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

2640
通知用户收到地理位置信息获取请求。
2641 2642 2643

**参数:**

H
HelloCrease 已提交
2644 2645
| 参数名         | 参数类型                            | 参数描述           |
| ----------- | ------------------------------- | -------------- |
2646
| origin      | string                          | 指定源的字符串索引。     |
H
HelloCrease 已提交
2647
| geolocation | [JsGeolocation](#jsgeolocation) | 通知Web组件用户操作行为。 |
2648 2649

**示例:**
2650

2651 2652
  ```ts
  // xxx.ets
L
lixiang 已提交
2653 2654
  import web_webview from '@ohos.web.webview'

2655 2656 2657
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2658
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationShow((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
Y
yamila 已提交
2670
                event.geolocation.invoke(event.origin, true, true)
2671 2672 2673
              }
            },
            cancel: () => {
Y
yamila 已提交
2674
              event.geolocation.invoke(event.origin, false, true)
2675 2676 2677 2678 2679 2680 2681 2682
            }
          })
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2683 2684 2685 2686 2687 2688 2689 2690
### onGeolocationHide

onGeolocationHide(callback: () => void)

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

**参数:**

H
HelloCrease 已提交
2691 2692 2693
| 参数名      | 参数类型       | 参数描述                 |
| -------- | ---------- | -------------------- |
| callback | () => void | 地理位置信息获取请求已被取消的回调函数。 |
L
laosan_ted 已提交
2694 2695 2696 2697 2698

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2699 2700
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
2701 2702 2703
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2704
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationHide(() => {
          console.log("onGeolocationHide...")
        })
      }
    }
  }
  ```

E
echoorchid 已提交
2717 2718 2719 2720 2721 2722 2723 2724
### onFullScreenEnter<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2725 2726 2727
| 参数名     | 参数类型                                     | 参数描述           |
| ------- | ---------------------------------------- | -------------- |
| handler | [FullScreenExitHandler](#fullscreenexithandler9) | 用于退出全屏模式的函数句柄。 |
E
echoorchid 已提交
2728 2729 2730 2731 2732

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2733 2734
  import web_webview from '@ohos.web.webview'

E
echoorchid 已提交
2735 2736 2737
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2738
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2739
    handler: FullScreenExitHandler = null
E
echoorchid 已提交
2740 2741 2742 2743
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenEnter((event) => {
Y
yamila 已提交
2744 2745
          console.log("onFullScreenEnter...")
          this.handler = event.handler
E
echoorchid 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
        })
      }
    }
  }
  ```

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

onFullScreenExit(callback: () => void)

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

**参数:**

H
HelloCrease 已提交
2760 2761 2762
| 参数名      | 参数类型       | 参数描述          |
| -------- | ---------- | ------------- |
| callback | () => void | 退出全屏模式时的回调函数。 |
E
echoorchid 已提交
2763 2764 2765 2766 2767

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2768 2769
  import web_webview from '@ohos.web.webview'

E
echoorchid 已提交
2770 2771 2772
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2773
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2774
    handler: FullScreenExitHandler = null
E
echoorchid 已提交
2775 2776 2777 2778
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenExit(() => {
Y
yamila 已提交
2779 2780
          console.log("onFullScreenExit...")
          this.handler.exitFullScreen()
E
echoorchid 已提交
2781 2782
        })
        .onFullScreenEnter((event) => {
Y
yamila 已提交
2783
          this.handler = event.handler
E
echoorchid 已提交
2784 2785 2786 2787 2788 2789
        })
      }
    }
  }
  ```

X
xiongjun_gitee 已提交
2790 2791 2792 2793
### onWindowNew<sup>9+</sup>

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

2794 2795 2796
使能multiWindowAccess情况下,通知用户新建窗口请求。
若不调用event.handler.setWebController接口,会造成render进程阻塞。
如果不需要打开新窗口,在调用event.handler.setWebController接口时须设置成null。
X
xiongjun_gitee 已提交
2797 2798 2799

**参数:**

H
HelloCrease 已提交
2800 2801 2802 2803 2804
| 参数名           | 参数类型                                     | 参数描述                       |
| ------------- | ---------------------------------------- | -------------------------- |
| isAlert       | boolean                                  | true代表请求创建对话框,false代表新标签页。 |
| isUserTrigger | boolean                                  | true代表用户触发,false代表非用户触发。   |
| targetUrl     | string                                   | 目标url。                     |
L
lixiang 已提交
2805
| handler       | [ControllerHandler](#controllerhandler9) | 用于设置新建窗口的WebviewController实例。  |
X
xiongjun_gitee 已提交
2806 2807 2808 2809 2810

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
2811
  import web_webview from '@ohos.web.webview'
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
  
  //在同一page页有两个web组件。在WebComponent新开窗口时,会跳转到NewWebViewComp。
  @CustomDialog
  struct NewWebViewComp {
  controller: CustomDialogController
  webviewController1: web_webview.WebviewController
  build() {
      Column() {
        Web({ src: "", controller: this.webviewController1 })
          .javaScriptAccess(true)
          .multiWindowAccess(false)
          .onWindowExit(()=> {
            console.info("NewWebViewComp onWindowExit")
            this.controller.close()
          })
        }
    }
  }

X
xiongjun_gitee 已提交
2831 2832 2833
  @Entry
  @Component
  struct WebComponent {
2834
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2835
    dialogController: CustomDialogController = null
X
xiongjun_gitee 已提交
2836 2837
    build() {
      Column() {
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
        Web({ src: 'www.example.com', controller: this.controller })
          .javaScriptAccess(true)
          //需要使能multiWindowAccess
          .multiWindowAccess(true)
          .allowWindowOpenMethod(true)
          .onWindowNew((event) => {
            if (this.dialogController) {
              this.dialogController.close()
            }
            let popController:web_webview.WebviewController = new web_webview.WebviewController()
            this.dialogController = new CustomDialogController({
              builder: NewWebViewComp({webviewController1: popController})
            })
            this.dialogController.open()
            //将新窗口对应WebviewController返回给Web内核。
            //如果不需要打开新窗口请调用event.handler.setWebController接口设置成null。
            //若不调用event.handler.setWebController接口,会造成render进程阻塞。
            event.handler.setWebController(popController)
          })
X
xiongjun_gitee 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
      }
    }
  }
  ```

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

onWindowExit(callback: () => void)

通知用户窗口关闭请求。

**参数:**

H
HelloCrease 已提交
2870 2871 2872
| 参数名      | 参数类型       | 参数描述         |
| -------- | ---------- | ------------ |
| callback | () => void | 窗口请求关闭的回调函数。 |
X
xiongjun_gitee 已提交
2873 2874 2875 2876 2877

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2878 2879
  import web_webview from '@ohos.web.webview'

X
xiongjun_gitee 已提交
2880 2881 2882
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2883
    controller: web_webview.WebviewController = new web_webview.WebviewController()
X
xiongjun_gitee 已提交
2884 2885 2886
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2887
        .onWindowExit(() => {
Y
yamila 已提交
2888
          console.log("onWindowExit...")
X
xiongjun_gitee 已提交
2889 2890 2891 2892 2893 2894
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2895 2896 2897 2898 2899 2900 2901 2902
### onSearchResultReceive<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2903 2904 2905 2906 2907
| 参数名                | 参数类型    | 参数描述                                     |
| ------------------ | ------- | ---------------------------------------- |
| activeMatchOrdinal | number  | 当前匹配的查找项的序号(从0开始)。                       |
| numberOfMatches    | number  | 所有匹配到的关键词的个数。                            |
| isDoneCounting     | boolean | 当次页内查找操作是否结束。该方法可能会回调多次,直到isDoneCounting为true为止。 |
L
laosan_ted 已提交
2908 2909 2910 2911 2912

**示例:**

  ```ts
  // xxx.ets
L
lixiang 已提交
2913 2914
  import web_webview from '@ohos.web.webview'

L
laosan_ted 已提交
2915 2916 2917
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2918
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931

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

C
chensi10 已提交
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
### onDataResubmitted<sup>9+</sup>

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

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

**参数:**

| 参数名  | 参数类型                                             | 参数描述               |
| ------- | ---------------------------------------------------- | ---------------------- |
C
chensi10 已提交
2942
| handler | [DataResubmissionHandler](#dataresubmissionhandler9) | 表单数据重新提交句柄。 |
C
chensi10 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2956
         .onDataResubmitted((event) => {
C
chensi10 已提交
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

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

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

C
chensi10 已提交
2969
设置旧页面不再呈现,新页面即将可见时触发的回调函数。
C
chensi10 已提交
2970 2971 2972

**参数:**

C
chensi10 已提交
2973 2974 2975
| 参数名 | 参数类型 | 参数描述                                          |
| ------ | -------- | ------------------------------------------------- |
| url    | string   | 旧页面不再呈现,新页面即将可见时新页面的url地址。 |
C
chensi10 已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
2989
         .onPageVisible((event) => {
C
chensi10 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
          console.log('onPageVisible url:' + event.url)
        })
      }
    }
  }
  ```

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

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

L
1111  
lixiang 已提交
3001
设置键盘事件的回调函数,该回调在被Webview使用前触发。
C
chensi10 已提交
3002 3003 3004 3005 3006 3007 3008

**参数:**

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

C
chensi10 已提交
3009 3010 3011 3012 3013 3014
**返回值:**

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

C
chensi10 已提交
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
3027
         .onInterceptKeyEvent((event) => {
3028
          if (event.keyCode == 2017 || event.keyCode == 2018) {
C
chensi10 已提交
3029 3030 3031 3032
            console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`)
            return true;
          }
          return false;
C
chensi10 已提交
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
        })
      }
    }
  }
  ```

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

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

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
C
chensi10 已提交
3063 3064
        Web({ src:'www.baidu.com', controller: this.controller })
         .onTouchIconUrlReceived((event) => {
C
chensi10 已提交
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
          console.log('onTouchIconUrlReceived:' + JSON.stringify(event))
        })
      }
    }
  }
  ```

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

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

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

**参数:**

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

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  import image from "@ohos.multimedia.image"
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State icon: image.PixelMap = undefined;
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
C
chensi10 已提交
3098 3099
         .onFaviconReceived((event) => {
          console.log('onFaviconReceived:' + JSON.stringify(event))
C
chensi10 已提交
3100 3101 3102 3103 3104 3105 3106
          this.icon = event.favicon;
        })
      }
    }
  }
  ```

L
Lei Gao 已提交
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
### onAudioStateChanged<sup>10+</sup>

onAudioStateChanged(callback: (event: { playing: boolean }) => void)

设置网页上的音频播放状态发生改变时的回调函数。

**参数:**

| 参数名  | 参数类型                                       | 参数描述                            |
| ------- | ---------------------------------------------- | ----------------------------------- |
| playing | boolean | 当前页面的音频播放状态,true表示正在播放,false表示未播放。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State playing: boolean = false
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
          .onAudioStateChanged(event => {
            this.playing = event.playing
            console.debug('onAudioStateChanged playing: ' + this.playing)
          })
      }
    }
  }
  ```

3141 3142
### onFirstContentfulPaint<sup>10+</sup>

3143
onFirstContentfulPaint(callback: (event?: { navigationStartTick: number, firstContentfulPaintMs: number }) => void)
3144 3145 3146 3147 3148 3149 3150

设置网页首次内容绘制回调函数。

**参数:**

| 参数名                 |  参数类型  | 参数描述                            |
| -----------------------| -------- | ----------------------------------- |
3151 3152
| navigationStartTick    | number   | navigation开始的时间,单位以微秒表示。|
| firstContentfulPaintMs | number   | 从navigation开始第一次绘制内容的时间,单位是以毫秒表示。|
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
          .onFirstContentfulPaint(event => {
            console.log("onFirstContentfulPaint:" + "[navigationStartTick]:" + 
              event.navigationStartTick + ", [firstContentfulPaintMs]:" + 
              event.firstContentfulPaintMs)
          })
      }
    }
  }
  ```

3177 3178
### onLoadIntercept<sup>10+</sup>

3179
onLoadIntercept(callback: (event?: { data: WebResourceRequest }) => boolean)
3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208

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

**参数:**

| 参数名  | 参数类型                                     | 参数描述      |
| ------- | ---------------------------------------- | --------- |
| request | [Webresourcerequest](#webresourcerequest) | url请求的相关信息。 |

**返回值:**

| 类型      | 说明                       |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
3209 3210 3211 3212 3213
          .onLoadIntercept((event) => {
            console.log('url:' + event.data.getRequestUrl())
            console.log('isMainFrame:' + event.data.isMainFrame())
            console.log('isRedirect:' + event.data.isRedirect())
            console.log('isRequestGesture:' + event.data.isRequestGesture())
3214 3215 3216 3217 3218 3219 3220
            return true
          })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
3221
## ConsoleMessage
3222

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

Z
zhou-liting125 已提交
3225
### getLineNumber
3226

Z
zhou-liting125 已提交
3227 3228 3229 3230
getLineNumber(): number

获取ConsoleMessage的行数。

3231
**返回值:**
L
laosan_ted 已提交
3232

3233 3234 3235
| 类型     | 说明                   |
| ------ | -------------------- |
| number | 返回ConsoleMessage的行数。 |
Z
zhou-liting125 已提交
3236 3237 3238 3239 3240 3241 3242

### getMessage

getMessage(): string

获取ConsoleMessage的日志信息。

3243
**返回值:**
L
laosan_ted 已提交
3244

3245 3246 3247
| 类型     | 说明                     |
| ------ | ---------------------- |
| string | 返回ConsoleMessage的日志信息。 |
Z
zhou-liting125 已提交
3248 3249 3250 3251

### getMessageLevel

getMessageLevel(): MessageLevel
3252

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

3255
**返回值:**
L
laosan_ted 已提交
3256

3257 3258 3259
| 类型                                | 说明                     |
| --------------------------------- | ---------------------- |
| [MessageLevel](#messagelevel枚举说明) | 返回ConsoleMessage的信息级别。 |
Z
zhou-liting125 已提交
3260 3261 3262 3263 3264 3265 3266

### getSourceId

getSourceId(): string

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

3267
**返回值:**
L
laosan_ted 已提交
3268

3269 3270 3271
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回网页源文件路径和名字。 |
Z
zhou-liting125 已提交
3272 3273

## JsResult
L
lixingchi1 已提交
3274

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

Z
zhou-liting125 已提交
3277 3278 3279
### handleCancel

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

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

Z
zhou-liting125 已提交
3283
### handleConfirm
L
add web  
liujinwei 已提交
3284

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

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

Z
zhou-liting125 已提交
3289 3290 3291 3292 3293 3294
### handlePromptConfirm<sup>9+</sup>

handlePromptConfirm(result: string): void

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

Z
zhou-liting125 已提交
3295
**参数:**
L
laosan_ted 已提交
3296

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

E
echoorchid 已提交
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
## FullScreenExitHandler<sup>9+</sup>

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

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

exitFullScreen(): void

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

X
xiongjun_gitee 已提交
3311 3312
## ControllerHandler<sup>9+</sup>

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

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

3317
setWebController(controller: WebviewController): void
X
xiongjun_gitee 已提交
3318

3319
设置WebviewController对象,如果不需要打开新窗口请设置为null。
X
xiongjun_gitee 已提交
3320 3321 3322

**参数:**

H
HelloCrease 已提交
3323 3324
| 参数名        | 参数类型          | 必填   | 默认值  | 参数描述                      |
| ---------- | ------------- | ---- | ---- | ------------------------- |
3325
| controller | [WebviewController](../apis/js-apis-webview.md#webviewcontroller) | 是    | -    | 新建web组件的WebviewController对象,如果不需要打开新窗口请设置为null。 |
X
xiongjun_gitee 已提交
3326

Z
zhou-liting125 已提交
3327 3328
## WebResourceError

L
update  
laosan_ted 已提交
3329
web组件资源管理错误信息对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
3330 3331 3332 3333 3334 3335 3336

### getErrorCode

getErrorCode(): number

获取加载资源的错误码。

3337
**返回值:**
L
laosan_ted 已提交
3338

3339 3340 3341
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回加载资源的错误码。 |
Z
zhou-liting125 已提交
3342 3343 3344 3345 3346 3347 3348

### getErrorInfo

getErrorInfo(): string

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

3349
**返回值:**
L
laosan_ted 已提交
3350

3351 3352 3353
| 类型     | 说明           |
| ------ | ------------ |
| string | 返回加载资源的错误信息。 |
Z
zhou-liting125 已提交
3354 3355 3356

## WebResourceRequest

L
update  
laosan_ted 已提交
3357
web组件获取资源请求对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
3358 3359 3360 3361 3362 3363 3364

### getRequestHeader

getResponseHeader() : Array\<Header\>

获取资源请求头信息。

3365
**返回值:**
L
laosan_ted 已提交
3366

3367 3368 3369
| 类型                         | 说明         |
| -------------------------- | ---------- |
| Array\<[Header](#header)\> | 返回资源请求头信息。 |
Z
zhou-liting125 已提交
3370 3371 3372 3373 3374 3375 3376

### getRequestUrl

getRequestUrl(): string

获取资源请求的URL信息。

3377
**返回值:**
L
laosan_ted 已提交
3378

3379 3380 3381
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源请求的URL信息。 |
Z
zhou-liting125 已提交
3382 3383 3384 3385 3386 3387 3388

### isMainFrame

isMainFrame(): boolean

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

3389
**返回值:**
L
laosan_ted 已提交
3390

3391 3392 3393
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否为主frame。 |
Z
zhou-liting125 已提交
3394 3395 3396 3397 3398 3399 3400

### isRedirect

isRedirect(): boolean

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

3401
**返回值:**
L
laosan_ted 已提交
3402

3403 3404 3405
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否被服务端重定向。 |
Z
zhou-liting125 已提交
3406 3407 3408 3409 3410 3411 3412

### isRequestGesture

isRequestGesture(): boolean

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

3413
**返回值:**
L
laosan_ted 已提交
3414

3415 3416 3417
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 返回资源请求是否与手势(如点击)相关联。 |
Z
zhou-liting125 已提交
3418

3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
### getRequestMethod<sup>9+</sup>

getRequestMethod(): string

获取请求方法。

**返回值:**

| 类型      | 说明                   |
| ------- | -------------------- |
| string | 返回请求方法。 |

Z
zhou-liting125 已提交
3431
## Header
L
liwenzhen 已提交
3432 3433 3434

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

3435 3436 3437 3438
| 名称          | 类型     | 描述            |
| ----------- | ------ | ------------- |
| headerKey   | string | 请求/响应头的key。   |
| headerValue | string | 请求/响应头的value。 |
L
liwenzhen 已提交
3439

L
add web  
liujinwei 已提交
3440

Z
zhou-liting125 已提交
3441
## WebResourceResponse
L
add web  
liujinwei 已提交
3442

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

Z
zhou-liting125 已提交
3445
### getReasonMessage
T
Ted 已提交
3446

Z
zhou-liting125 已提交
3447
getReasonMessage(): string
T
Ted 已提交
3448

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

3451
**返回值:**
L
laosan_ted 已提交
3452

3453 3454 3455
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源响应的状态码描述。 |
T
Ted 已提交
3456

Z
zhou-liting125 已提交
3457
### getResponseCode
T
Ted 已提交
3458

Z
zhou-liting125 已提交
3459 3460 3461 3462
getResponseCode(): number

获取资源响应的状态码。

3463
**返回值:**
L
laosan_ted 已提交
3464

3465 3466 3467
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回资源响应的状态码。 |
Z
zhou-liting125 已提交
3468 3469 3470 3471 3472 3473 3474

### getResponseData

getResponseData(): string

获取资源响应数据。

3475
**返回值:**
L
laosan_ted 已提交
3476

3477 3478 3479
| 类型     | 说明        |
| ------ | --------- |
| string | 返回资源响应数据。 |
Z
zhou-liting125 已提交
3480 3481 3482 3483 3484 3485 3486

### getResponseEncoding

getResponseEncoding(): string

获取资源响应的编码。

3487
**返回值:**
L
laosan_ted 已提交
3488

3489 3490 3491
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回资源响应的编码。 |
Z
zhou-liting125 已提交
3492 3493 3494 3495 3496 3497 3498

### getResponseHeader

getResponseHeader() : Array\<Header\>

获取资源响应头。

3499
**返回值:**
L
laosan_ted 已提交
3500

3501 3502 3503
| 类型                         | 说明       |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | 返回资源响应头。 |
Z
zhou-liting125 已提交
3504 3505 3506 3507 3508 3509 3510

### getResponseMimeType

getResponseMimeType(): string

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

3511
**返回值:**
L
laosan_ted 已提交
3512

3513 3514 3515
| 类型     | 说明                 |
| ------ | ------------------ |
| string | 返回资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3516 3517 3518

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

3519
setResponseData(data: string | number)
Z
zhou-liting125 已提交
3520 3521 3522

设置资源响应数据。

Z
zhou-liting125 已提交
3523
**参数:**
L
laosan_ted 已提交
3524

3525 3526 3527
| 参数名 | 参数类型         | 必填 | 默认值 | 参数描述                                                     |
| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ |
| data   | string \| number | 是   | -      | 要设置的资源响应数据。string表示输入类型是字符串,number表示输入类型是文件句柄。 |
Z
zhou-liting125 已提交
3528 3529 3530 3531 3532 3533 3534

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

setResponseEncoding(encoding: string)

设置资源响应的编码。

Z
zhou-liting125 已提交
3535
**参数:**
L
laosan_ted 已提交
3536

3537 3538 3539
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述         |
| -------- | ------ | ---- | ---- | ------------ |
| encoding | string | 是    | -    | 要设置的资源响应的编码。 |
Z
zhou-liting125 已提交
3540 3541 3542 3543 3544 3545 3546

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

setResponseMimeType(mimeType: string)

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

Z
zhou-liting125 已提交
3547
**参数:**
L
laosan_ted 已提交
3548

3549 3550 3551
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述                 |
| -------- | ------ | ---- | ---- | -------------------- |
| mimeType | string | 是    | -    | 要设置的资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3552 3553 3554 3555 3556 3557 3558

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

setReasonMessage(reason: string)

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

Z
zhou-liting125 已提交
3559
**参数:**
L
laosan_ted 已提交
3560

3561 3562 3563
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------ | ------ | ---- | ---- | --------------- |
| reason | string | 是    | -    | 要设置的资源响应的状态码描述。 |
Z
zhou-liting125 已提交
3564 3565 3566 3567 3568 3569 3570

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

setResponseHeader(header: Array\<Header\>)

设置资源响应头。

Z
zhou-liting125 已提交
3571
**参数:**
L
laosan_ted 已提交
3572

3573 3574 3575
| 参数名    | 参数类型                       | 必填   | 默认值  | 参数描述       |
| ------ | -------------------------- | ---- | ---- | ---------- |
| header | Array\<[Header](#header)\> | 是    | -    | 要设置的资源响应头。 |
Z
zhou-liting125 已提交
3576 3577 3578 3579 3580 3581 3582

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

setResponseCode(code: number)

设置资源响应的状态码。

Z
zhou-liting125 已提交
3583
**参数:**
L
laosan_ted 已提交
3584

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

3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600
### setResponseIsReady<sup>9+</sup>

setResponseIsReady(IsReady: boolean)

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

**参数:**

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

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

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

Z
zhou-liting125 已提交
3605
### handleFileList<sup>9+</sup>
3606

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

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

Z
zhou-liting125 已提交
3611
**参数:**
L
laosan_ted 已提交
3612

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

Z
zhou-liting125 已提交
3617 3618
## FileSelectorParam<sup>9+</sup>

Z
zhou-liting125 已提交
3619
web组件获取文件对象。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
Z
zhou-liting125 已提交
3620

L
lixiang 已提交
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632
### getTitle<sup>9+</sup>

getTitle(): string

获取文件选择器标题。

**返回值:**

| 类型     | 说明       |
| ------ | -------- |
| string | 返回文件选择器标题。 |

Z
zhou-liting125 已提交
3633 3634 3635 3636 3637 3638
### getMode<sup>9+</sup>

getMode(): FileSelectorMode

获取文件选择器的模式。

3639
**返回值:**
L
laosan_ted 已提交
3640

3641 3642 3643
| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode枚举说明) | 返回文件选择器的模式。 |
Z
zhou-liting125 已提交
3644 3645 3646 3647 3648 3649 3650

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

getAcceptType(): Array\<string\>

获取文件过滤类型。

3651
**返回值:**
L
laosan_ted 已提交
3652

3653 3654 3655
| 类型              | 说明        |
| --------------- | --------- |
| Array\<string\> | 返回文件过滤类型。 |
Z
zhou-liting125 已提交
3656 3657 3658 3659 3660 3661 3662

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

isCapture(): boolean

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

3663
**返回值:**
L
laosan_ted 已提交
3664

3665 3666 3667
| 类型      | 说明           |
| ------- | ------------ |
| boolean | 返回是否调用多媒体能力。 |
3668 3669 3670

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

Z
zhou-liting125 已提交
3671
Web组件返回的http auth认证请求确认或取消和使用缓存密码认证功能对象。示例代码参考[onHttpAuthRequest事件](#onhttpauthrequest9)
3672 3673 3674 3675 3676 3677 3678 3679

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

cancel(): void

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

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

3681 3682 3683 3684
confirm(userName: string, pwd: string): boolean

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

Z
zhou-liting125 已提交
3685
**参数:**
3686

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

Z
zhou-liting125 已提交
3692
**返回值:**
L
laosan_ted 已提交
3693

3694 3695 3696
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 认证成功返回true,失败返回false。 |
3697 3698 3699 3700 3701

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

isHttpAuthInfoSaved(): boolean

Z
zengyawen 已提交
3702
通知Web组件用户使用服务器缓存的帐号密码认证。
3703

Z
zhou-liting125 已提交
3704
**返回值:**
L
laosan_ted 已提交
3705

3706 3707 3708
| 类型      | 说明                        |
| ------- | ------------------------- |
| boolean | 存在密码认证成功返回true,其他返回false。 |
3709

3710 3711
## SslErrorHandler<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3712
Web组件返回的SSL错误通知事件用户处理功能对象。示例代码参考[onSslErrorEventReceive事件](#onsslerroreventreceive9)
3713 3714 3715 3716 3717

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

handleCancel(): void

I
i-am-a-little-bird 已提交
3718
通知Web组件取消此请求。
3719 3720 3721 3722 3723

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

handleConfirm(): void

I
i-am-a-little-bird 已提交
3724 3725 3726 3727
通知Web组件继续使用SSL证书。

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

I
bugfix  
i-am-a-little-bird 已提交
3728
Web组件返回的SSL客户端证书请求事件用户处理功能对象。示例代码参考[onClientAuthenticationRequest事件](#onclientauthenticationrequest9)
I
i-am-a-little-bird 已提交
3729 3730 3731 3732 3733 3734

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

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

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

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

H
HelloCrease 已提交
3738 3739 3740 3741
| 参数名           | 参数类型   | 必填   | 参数描述               |
| ------------- | ------ | ---- | ------------------ |
| priKeyFile    | string | 是    | 存放私钥的文件,包含路径和文件名。  |
| certChainFile | string | 是    | 存放证书链的文件,包含路径和文件名。 |
I
i-am-a-little-bird 已提交
3742

3743 3744 3745 3746
### confirm<sup>10+</sup>

confirm(authUri : string): void

3747 3748
**需要权限:** ohos.permission.ACCESS_CERT_MANAGER

3749 3750 3751 3752 3753 3754 3755 3756
通知Web组件使用指定的凭据(从证书管理模块获得)。

**参数:**

| 参数名   | 参数类型  | 必填  | 参数描述  |
| ------- | ------ | ----  | ------------- |
| authUri | string | 是    | 凭据的关键值。  |

I
i-am-a-little-bird 已提交
3757
### cancel<sup>9+</sup>
3758

I
i-am-a-little-bird 已提交
3759 3760
cancel(): void

I
bugfix  
i-am-a-little-bird 已提交
3761
通知Web组件取消相同host和port服务器发送的客户端证书请求事件。同时,相同host和port服务器的请求,不重复上报该事件。
I
i-am-a-little-bird 已提交
3762 3763 3764 3765 3766 3767

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

ignore(): void

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

3769 3770
## PermissionRequest<sup>9+</sup>

X
xiongjun_gitee 已提交
3771
Web组件返回授权或拒绝权限功能的对象。示例代码参考[onPermissionRequest事件](#onpermissionrequest9)
3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786

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

deny(): void

拒绝网页所请求的权限。

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

getOrigin(): string

获取网页来源。

**返回值:**

H
HelloCrease 已提交
3787 3788 3789
| 类型     | 说明           |
| ------ | ------------ |
| string | 当前请求权限网页的来源。 |
3790 3791 3792 3793 3794

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

getAccessibleResource(): Array\<string\>

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

**返回值:**

H
HelloCrease 已提交
3799 3800
| 类型              | 说明            |
| --------------- | ------------- |
3801 3802 3803 3804 3805 3806
| Array\<string\> | 网页所请求的权限资源列表。 |

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

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

X
xiongjun_gitee 已提交
3807
对网页访问的给定权限进行授权。
3808 3809 3810

**参数:**

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

I
i-am-a-little-bird 已提交
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
## ContextMenuSourceType<sup>9+</sup>枚举说明
| 名称                   | 描述         |
| -------------------- | ---------- |
| None        | 其他事件来源。  |
| Mouse       | 鼠标事件。  |
| LongPress   | 长按事件。  |

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

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

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

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

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

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

3850 3851
## WebContextMenuParam<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3852
实现长按页面元素或鼠标右键弹出来的菜单信息。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
3853 3854 3855 3856 3857 3858 3859 3860 3861

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

x(): number

弹出菜单的x坐标。

**返回值:**

H
HelloCrease 已提交
3862 3863
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3864
| number | 显示正常返回非负整数,否则返回-1。 |
3865 3866 3867 3868 3869 3870 3871 3872 3873

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

y(): number

弹出菜单的y坐标。

**返回值:**

H
HelloCrease 已提交
3874 3875
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3876
| number | 显示正常返回非负整数,否则返回-1。 |
3877 3878 3879 3880 3881

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

getLinkUrl(): string

Y
yu-shihao4 已提交
3882
获取链接地址。
3883 3884 3885

**返回值:**

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

3890
### getUnfilteredLinkUrl<sup>9+</sup>
3891

3892
getUnfilteredLinkUrl(): string
3893

Y
yu-shihao4 已提交
3894
获取链接地址。
3895 3896 3897

**返回值:**

H
HelloCrease 已提交
3898 3899
| 类型     | 说明                    |
| ------ | --------------------- |
Y
yu-shihao4 已提交
3900
| string | 如果长按位置是链接,返回原始的url链接。 |
3901 3902 3903 3904 3905 3906 3907 3908 3909

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

getSourceUrl(): string

获取sourceUrl链接。

**返回值:**

H
HelloCrease 已提交
3910 3911
| 类型     | 说明                       |
| ------ | ------------------------ |
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
| string | 如果选中的元素有src属性,返回src的url。 |

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

existsImageContents(): boolean

是否存在图像内容。

**返回值:**

H
HelloCrease 已提交
3922 3923
| 类型      | 说明                        |
| ------- | ------------------------- |
3924 3925
| boolean | 长按位置中有图片返回true,否则返回false。 |

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

I
bugfix  
i-am-a-little-bird 已提交
3928
getMediaType(): ContextMenuMediaType
I
i-am-a-little-bird 已提交
3929 3930 3931 3932 3933 3934 3935

获取网页元素媒体类型。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3940
getSelectionText(): string
I
i-am-a-little-bird 已提交
3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951

获取选中文本。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3952
getSourceType(): ContextMenuSourceType
I
i-am-a-little-bird 已提交
3953 3954 3955 3956 3957 3958 3959

获取菜单事件来源。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3964
getInputFieldType(): ContextMenuInputFieldType
I
i-am-a-little-bird 已提交
3965 3966 3967 3968 3969 3970 3971

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

**返回值:**

| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
I
bugfix  
i-am-a-little-bird 已提交
3972
| [ContextMenuInputFieldType](#contextmenuinputfieldtype9枚举说明) | 输入框类型。 |
I
i-am-a-little-bird 已提交
3973 3974 3975

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

I
bugfix  
i-am-a-little-bird 已提交
3976
isEditable(): boolean
I
i-am-a-little-bird 已提交
3977 3978 3979 3980 3981 3982 3983

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

**返回值:**

| 类型      | 说明                        |
| ------- | ------------------------- |
I
bugfix  
i-am-a-little-bird 已提交
3984
| boolean | 网页元素可编辑返回true,不可编辑返回false。 |
I
i-am-a-little-bird 已提交
3985 3986 3987

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

I
bugfix  
i-am-a-little-bird 已提交
3988
getEditStateFlags(): number
I
i-am-a-little-bird 已提交
3989 3990 3991 3992 3993 3994 3995

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

**返回值:**

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

3998 3999
## WebContextMenuResult<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
4000
实现长按页面元素或鼠标右键弹出来的菜单所执行的响应事件。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013

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

closeContextMenu(): void

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

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

copyImage(): void

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

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

I
bugfix  
i-am-a-little-bird 已提交
4016
copy(): void
I
i-am-a-little-bird 已提交
4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037

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

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

paste(): void

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

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

cut(): void

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

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

selectAll(): void

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

4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
## JsGeolocation

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

### invoke

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

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

**参数:**

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

L
lixiang 已提交
4056
## MessageLevel枚举说明
L
laosan_ted 已提交
4057

L
lixiang 已提交
4058 4059 4060 4061 4062 4063 4064
| 名称    | 描述    |
| ----- | :---- |
| Debug | 调试级别。 |
| Error | 错误级别。 |
| Info  | 消息级别。 |
| Log   | 日志级别。 |
| Warn  | 警告级别。 |
Z
zengyawen 已提交
4065

L
lixiang 已提交
4066
## RenderExitReason枚举说明
Z
zengyawen 已提交
4067

L
lixiang 已提交
4068
onRenderExited接口返回的渲染进程退出的具体原因。
L
laosan_ted 已提交
4069

L
lixiang 已提交
4070 4071 4072 4073 4074 4075 4076
| 名称                         | 描述                |
| -------------------------- | ----------------- |
| ProcessAbnormalTermination | 渲染进程异常退出。         |
| ProcessWasKilled           | 收到SIGKILL,或被手动终止。 |
| ProcessCrashed             | 渲染进程崩溃退出,如段错误。    |
| ProcessOom                 | 程序内存不足。           |
| ProcessExitUnknown         | 其他原因。             |
L
laosan_ted 已提交
4077

L
lixiang 已提交
4078
## MixedMode枚举说明
L
laosan_ted 已提交
4079

L
lixiang 已提交
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
| 名称         | 描述                                 |
| ---------- | ---------------------------------- |
| 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           | 地理地址。                    |
| Phone         | 电话号码。                    |
| Unknown       | 未知内容。                    |

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

onSslErrorEventReceive接口返回的SSL错误的具体原因。

| 名称           | 描述          |
| ------------ | ----------- |
| Invalid      | 一般错误。       |
| HostMismatch | 主机名不匹配。     |
| DateInvalid  | 证书日期无效。     |
| Untrusted    | 证书颁发机构不受信任。 |

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

| 名称        | 描述            | 备注                         |
| --------- | ------------- | -------------------------- |
| MidiSysex | MIDI SYSEX资源。 | 目前仅支持权限事件上报,MIDI设备的使用还未支持。 |

## WebDarkMode<sup>9+</sup>枚举说明
| 名称      | 描述                                   |
| ------- | ------------------------------------ |
| Off     | Web深色模式关闭。                     |
| On      | Web深色模式开启。                     |
| Auto    | Web深色模式跟随系统。                 |

Y
yuhaoge 已提交
4139 4140 4141 4142 4143 4144 4145 4146 4147
## WebMediaOptions<sup>10+</sup>

Web媒体策略的配置。

| 名称           | 类型       | 可读 | 可写 | 必填 | 说明                         |
| -------------- | --------- | ---- | ---- | --- | ---------------------------- |
| resumeInterval |  number   |  是  | 是   |  否  |被暂停的Web音频能够自动续播的有效期,单位:秒。最长有效期为60秒。 |
| audioExclusive |  boolean  |  是  | 是   |  否  | 应用内多个Web实例的音频是否独占。    |

L
lixiang 已提交
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227
## DataResubmissionHandler<sup>9+</sup>

通过DataResubmissionHandler可以重新提交表单数据或取消提交表单数据。

### resend<sup>9+</sup>

resend(): void

重新发送表单数据。

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onDataResubmitted((event) => {
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

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

cancel(): void

取消重新发送表单数据。

**示例:**

  ```ts
  // xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
         .onDataResubmitted((event) => {
          console.log('onDataResubmitted')
          event.handler.cancel();
        })
      }
    }
  }
  ```

  ## WebController

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

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

### 创建对象

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

### getCookieManager<sup>9+</sup>

getCookieManager(): WebCookie

获取web组件cookie管理对象。

**返回值:**

| 类型        | 说明                                       |
| --------- | ---------------------------------------- |
4228
| WebCookie | web组件cookie管理对象,参考[WebCookie](#webcookiedeprecated)定义。 |
L
lixiang 已提交
4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257

**示例:**

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

    build() {
      Column() {
        Button('getCookieManager')
          .onClick(() => {
            let cookieManager = this.controller.getCookieManager()
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### requestFocus<sup>(deprecated)</sup>

requestFocus()

使当前web页面获取焦点。

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

L
laosan_ted 已提交
4259
**示例:**
L
laosan_ted 已提交
4260

L
laosan_ted 已提交
4261 4262 4263 4264 4265
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4266
    controller: WebController = new WebController()
4267

L
laosan_ted 已提交
4268 4269 4270 4271
    build() {
      Column() {
        Button('requestFocus')
          .onClick(() => {
Y
yamila 已提交
4272
            this.controller.requestFocus()
L
laosan_ted 已提交
4273 4274 4275 4276 4277 4278 4279
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

L
laosan_ted 已提交
4280
### accessBackward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4281 4282 4283

accessBackward(): boolean

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

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

Z
zhou-liting125 已提交
4288
**返回值:**
L
laosan_ted 已提交
4289

4290 4291 4292
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以后退返回true,否则返回false。 |
L
update  
laosan_ted 已提交
4293

Z
zhou-liting125 已提交
4294
**示例:**
L
laosan_ted 已提交
4295

Z
zhou-liting125 已提交
4296 4297
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4298
  @Entry
L
update  
laosan_ted 已提交
4299 4300
  @Component
  struct WebComponent {
Y
yamila 已提交
4301
    controller: WebController = new WebController()
4302

L
update  
laosan_ted 已提交
4303 4304 4305
    build() {
      Column() {
        Button('accessBackward')
L
laosan_ted 已提交
4306
          .onClick(() => {
Y
yamila 已提交
4307 4308
            let result = this.controller.accessBackward()
            console.log('result:' + result)
L
laosan_ted 已提交
4309
          })
Z
zhou-liting125 已提交
4310
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4311
      }
L
update  
laosan_ted 已提交
4312 4313 4314 4315
    }
  }
  ```

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

accessForward(): boolean

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

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

Z
zhou-liting125 已提交
4324
**返回值:**
L
laosan_ted 已提交
4325

4326 4327 4328
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以前进返回true,否则返回false。 |
L
update  
laosan_ted 已提交
4329

Z
zhou-liting125 已提交
4330
**示例:**
L
laosan_ted 已提交
4331

Z
zhou-liting125 已提交
4332 4333
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4334
  @Entry
L
update  
laosan_ted 已提交
4335 4336
  @Component
  struct WebComponent {
Y
yamila 已提交
4337
    controller: WebController = new WebController()
4338

L
update  
laosan_ted 已提交
4339 4340 4341
    build() {
      Column() {
        Button('accessForward')
L
laosan_ted 已提交
4342
          .onClick(() => {
Y
yamila 已提交
4343 4344
            let result = this.controller.accessForward()
            console.log('result:' + result)
L
laosan_ted 已提交
4345
          })
Z
zhou-liting125 已提交
4346
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4347
      }
L
update  
laosan_ted 已提交
4348 4349 4350 4351
    }
  }
  ```

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

L
lixingchi1 已提交
4354
accessStep(step: number): boolean
Z
zengyawen 已提交
4355

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

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

Z
zhou-liting125 已提交
4360
**参数:**
Z
zengyawen 已提交
4361

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

Z
zhou-liting125 已提交
4366
**返回值:**
L
laosan_ted 已提交
4367

4368 4369 4370
| 类型      | 说明        |
| ------- | --------- |
| boolean | 页面是否前进或后退 |
4371

Z
zhou-liting125 已提交
4372
**示例:**
L
laosan_ted 已提交
4373

Z
zhou-liting125 已提交
4374 4375
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4376
  @Entry
L
update  
laosan_ted 已提交
4377 4378
  @Component
  struct WebComponent {
Y
yamila 已提交
4379 4380
    controller: WebController = new WebController()
    @State steps: number = 2
4381

L
update  
laosan_ted 已提交
4382 4383 4384
    build() {
      Column() {
        Button('accessStep')
L
laosan_ted 已提交
4385
          .onClick(() => {
Y
yamila 已提交
4386 4387
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
L
laosan_ted 已提交
4388
          })
Z
zhou-liting125 已提交
4389
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4390
      }
L
update  
laosan_ted 已提交
4391 4392 4393 4394
    }
  }
  ```

L
laosan_ted 已提交
4395
### backward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4396

L
update  
laosan_ted 已提交
4397
backward(): void
L
lixingchi1 已提交
4398

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

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

Z
zhou-liting125 已提交
4403
**示例:**
L
laosan_ted 已提交
4404

Z
zhou-liting125 已提交
4405 4406
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4407
  @Entry
L
update  
laosan_ted 已提交
4408 4409
  @Component
  struct WebComponent {
Y
yamila 已提交
4410
    controller: WebController = new WebController()
4411

L
update  
laosan_ted 已提交
4412 4413 4414
    build() {
      Column() {
        Button('backward')
L
laosan_ted 已提交
4415
          .onClick(() => {
Y
yamila 已提交
4416
            this.controller.backward()
L
laosan_ted 已提交
4417
          })
Z
zhou-liting125 已提交
4418
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4419
      }
L
update  
laosan_ted 已提交
4420 4421 4422
    }
  }
  ```
L
lixingchi1 已提交
4423

L
laosan_ted 已提交
4424
### forward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4425

L
update  
laosan_ted 已提交
4426
forward(): void
L
lixingchi1 已提交
4427

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

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

Z
zhou-liting125 已提交
4432
**示例:**
L
laosan_ted 已提交
4433

Z
zhou-liting125 已提交
4434 4435
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4436
  @Entry
L
update  
laosan_ted 已提交
4437 4438
  @Component
  struct WebComponent {
Y
yamila 已提交
4439
    controller: WebController = new WebController()
4440

L
update  
laosan_ted 已提交
4441 4442 4443
    build() {
      Column() {
        Button('forward')
L
laosan_ted 已提交
4444
          .onClick(() => {
Y
yamila 已提交
4445
            this.controller.forward()
L
laosan_ted 已提交
4446
          })
Z
zhou-liting125 已提交
4447
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4448
      }
L
update  
laosan_ted 已提交
4449 4450 4451 4452
    }
  }
  ```

L
laosan_ted 已提交
4453
### deleteJavaScriptRegister<sup>(deprecated)</sup>
L
update  
laosan_ted 已提交
4454 4455 4456

deleteJavaScriptRegister(name: string)

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

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

Z
zhou-liting125 已提交
4461
**参数:**
L
laosan_ted 已提交
4462

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

Z
zhou-liting125 已提交
4467
**示例:**
L
laosan_ted 已提交
4468

Z
zhou-liting125 已提交
4469 4470
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4471
  @Entry
L
update  
laosan_ted 已提交
4472 4473
  @Component
  struct WebComponent {
Y
yamila 已提交
4474 4475
    controller: WebController = new WebController()
    @State name: string = 'Object'
4476

L
update  
laosan_ted 已提交
4477 4478 4479
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
L
laosan_ted 已提交
4480
          .onClick(() => {
Y
yamila 已提交
4481
            this.controller.deleteJavaScriptRegister(this.name)
L
laosan_ted 已提交
4482
          })
Z
zhou-liting125 已提交
4483
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4484
      }
L
update  
laosan_ted 已提交
4485 4486 4487 4488
    }
  }
  ```

L
laosan_ted 已提交
4489
### getHitTest<sup>(deprecated)</sup>
L
lixingchi1 已提交
4490 4491 4492

getHitTest(): HitTestType

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

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

Z
zhou-liting125 已提交
4497
**返回值:**
L
laosan_ted 已提交
4498

4499 4500 4501
| 类型                              | 说明          |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype枚举说明) | 被点击区域的元素类型。 |
L
lixingchi1 已提交
4502

Z
zhou-liting125 已提交
4503
**示例:**
L
laosan_ted 已提交
4504

Z
zhou-liting125 已提交
4505 4506
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4507
  @Entry
L
update  
laosan_ted 已提交
4508 4509
  @Component
  struct WebComponent {
Y
yamila 已提交
4510
    controller: WebController = new WebController()
4511

L
update  
laosan_ted 已提交
4512 4513 4514
    build() {
      Column() {
        Button('getHitTest')
L
laosan_ted 已提交
4515
          .onClick(() => {
Y
yamila 已提交
4516 4517
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
L
laosan_ted 已提交
4518
          })
Z
zhou-liting125 已提交
4519
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4520
      }
L
update  
laosan_ted 已提交
4521 4522 4523 4524
    }
  }
  ```

L
laosan_ted 已提交
4525
### loadData<sup>(deprecated)</sup>
L
lixingchi1 已提交
4526

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

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

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

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

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

Z
zhou-liting125 已提交
4537
**参数:**
L
laosan_ted 已提交
4538

4539 4540 4541 4542 4543 4544 4545
| 参数名        | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---------- | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
4546

Z
zhou-liting125 已提交
4547
**示例:**
L
laosan_ted 已提交
4548

Z
zhou-liting125 已提交
4549 4550
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4551
  @Entry
L
update  
laosan_ted 已提交
4552 4553
  @Component
  struct WebComponent {
Y
yamila 已提交
4554
    controller: WebController = new WebController()
4555

L
update  
laosan_ted 已提交
4556 4557 4558
    build() {
      Column() {
        Button('loadData')
L
laosan_ted 已提交
4559 4560 4561 4562 4563
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
Y
yamila 已提交
4564
            })
L
laosan_ted 已提交
4565
          })
Z
zhou-liting125 已提交
4566
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4567
      }
L
update  
laosan_ted 已提交
4568 4569 4570 4571
    }
  }
  ```

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

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

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

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

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

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

Z
zhou-liting125 已提交
4584
**参数:**
L
laosan_ted 已提交
4585

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

Z
zhou-liting125 已提交
4591
**示例:**
L
laosan_ted 已提交
4592

Z
zhou-liting125 已提交
4593 4594
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4595
  @Entry
L
update  
laosan_ted 已提交
4596 4597
  @Component
  struct WebComponent {
Y
yamila 已提交
4598
    controller: WebController = new WebController()
4599

L
update  
laosan_ted 已提交
4600 4601 4602
    build() {
      Column() {
        Button('loadUrl')
L
laosan_ted 已提交
4603
          .onClick(() => {
Y
yamila 已提交
4604
            this.controller.loadUrl({ url: 'www.example.com' })
L
laosan_ted 已提交
4605
          })
Z
zhou-liting125 已提交
4606
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4607
      }
L
update  
laosan_ted 已提交
4608 4609 4610 4611
    }
  }
  ```

L
laosan_ted 已提交
4612
### onActive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4613 4614 4615

onActive(): void

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

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

Z
zhou-liting125 已提交
4620
**示例:**
L
laosan_ted 已提交
4621

Z
zhou-liting125 已提交
4622 4623
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4624
  @Entry
L
update  
laosan_ted 已提交
4625 4626
  @Component
  struct WebComponent {
Y
yamila 已提交
4627
    controller: WebController = new WebController()
4628

L
update  
laosan_ted 已提交
4629 4630 4631
    build() {
      Column() {
        Button('onActive')
L
laosan_ted 已提交
4632
          .onClick(() => {
Y
yamila 已提交
4633
            this.controller.onActive()
L
laosan_ted 已提交
4634
          })
Z
zhou-liting125 已提交
4635
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4636
      }
L
update  
laosan_ted 已提交
4637 4638 4639 4640
    }
  }
  ```

L
laosan_ted 已提交
4641
### onInactive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4642 4643 4644

onInactive(): void

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

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

Z
zhou-liting125 已提交
4649
**示例:**
L
laosan_ted 已提交
4650

Z
zhou-liting125 已提交
4651 4652
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4653
  @Entry
L
update  
laosan_ted 已提交
4654 4655
  @Component
  struct WebComponent {
Y
yamila 已提交
4656
    controller: WebController = new WebController()
4657

L
update  
laosan_ted 已提交
4658 4659 4660
    build() {
      Column() {
        Button('onInactive')
L
laosan_ted 已提交
4661
          .onClick(() => {
Y
yamila 已提交
4662
            this.controller.onInactive()
L
laosan_ted 已提交
4663
          })
Z
zhou-liting125 已提交
4664
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4665
      }
L
update  
laosan_ted 已提交
4666 4667 4668 4669
    }
  }
  ```

L
laosan_ted 已提交
4670
### zoom<sup>(deprecated)</sup>
4671 4672 4673
zoom(factor: number): void

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

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

Z
zhou-liting125 已提交
4677
**参数:**
L
laosan_ted 已提交
4678

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

Z
zhou-liting125 已提交
4683
**示例:**
L
laosan_ted 已提交
4684

Z
zhou-liting125 已提交
4685 4686
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4687
  @Entry
L
update  
laosan_ted 已提交
4688 4689
  @Component
  struct WebComponent {
Y
yamila 已提交
4690 4691
    controller: WebController = new WebController()
    @State factor: number = 1
4692

L
update  
laosan_ted 已提交
4693 4694 4695
    build() {
      Column() {
        Button('zoom')
L
laosan_ted 已提交
4696
          .onClick(() => {
Y
yamila 已提交
4697
            this.controller.zoom(this.factor)
L
laosan_ted 已提交
4698
          })
Z
zhou-liting125 已提交
4699
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4700
      }
L
update  
laosan_ted 已提交
4701 4702 4703 4704
    }
  }
  ```

L
laosan_ted 已提交
4705
### refresh<sup>(deprecated)</sup>
L
lixingchi1 已提交
4706

Z
zhou-liting125 已提交
4707
refresh()
L
lixingchi1 已提交
4708

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

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

Z
zhou-liting125 已提交
4713
**示例:**
L
laosan_ted 已提交
4714

Z
zhou-liting125 已提交
4715 4716
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4717
  @Entry
L
update  
laosan_ted 已提交
4718 4719
  @Component
  struct WebComponent {
Y
yamila 已提交
4720
    controller: WebController = new WebController()
4721

L
update  
laosan_ted 已提交
4722 4723 4724
    build() {
      Column() {
        Button('refresh')
L
laosan_ted 已提交
4725
          .onClick(() => {
Y
yamila 已提交
4726
            this.controller.refresh()
L
laosan_ted 已提交
4727
          })
Z
zhou-liting125 已提交
4728
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4729
      }
L
update  
laosan_ted 已提交
4730 4731 4732 4733
    }
  }
  ```

L
laosan_ted 已提交
4734
### registerJavaScriptProxy<sup>(deprecated)</sup>
L
lixingchi1 已提交
4735

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

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

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

Z
zhou-liting125 已提交
4742
**参数:**
L
laosan_ted 已提交
4743

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

Z
zhou-liting125 已提交
4750
**示例:**
L
laosan_ted 已提交
4751

Z
zhou-liting125 已提交
4752 4753
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4754
  @Entry
L
update  
laosan_ted 已提交
4755 4756 4757 4758 4759
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
Y
yamila 已提交
4760
        return "ArkUI Web Component"
L
update  
laosan_ted 已提交
4761 4762
      },
      toString: () => {
Y
yamila 已提交
4763
        console.log('Web Component toString')
L
update  
laosan_ted 已提交
4764 4765 4766 4767 4768 4769 4770 4771 4772 4773
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
Y
yamila 已提交
4774
            })
L
update  
laosan_ted 已提交
4775 4776 4777 4778 4779 4780 4781 4782
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```
4783

L
update  
laosan_ted 已提交
4784
  ```html
L
laosan_ted 已提交
4785
  <!-- index.html -->
L
update  
laosan_ted 已提交
4786 4787 4788 4789 4790 4791 4792 4793
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
Y
yamila 已提交
4794 4795
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
L
update  
laosan_ted 已提交
4796 4797 4798
      }
  </script>
  </html>
4799

L
update  
laosan_ted 已提交
4800 4801
  ```

L
laosan_ted 已提交
4802
### runJavaScript<sup>(deprecated)</sup>
L
lixingchi1 已提交
4803

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

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

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

Z
zhou-liting125 已提交
4810
**参数:**
L
laosan_ted 已提交
4811

4812 4813 4814 4815
| 参数名      | 参数类型                     | 必填   | 默认值  | 参数描述                                     |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | 是    | -    | JavaScript脚本。                            |
| callback | (result: string) => void | 否    | -    | 回调执行JavaScript脚本结果。JavaScript脚本若执行失败或无返回值时,返回null。 |
L
lixingchi1 已提交
4816

Z
zhou-liting125 已提交
4817
**示例:**
L
laosan_ted 已提交
4818

Z
zhou-liting125 已提交
4819 4820
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4821
  @Entry
L
update  
laosan_ted 已提交
4822 4823
  @Component
  struct WebComponent {
Y
yamila 已提交
4824
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836
    @State webResult: string = ''
    build() {
      Column() {
        Text(this.webResult).fontSize(20)
        Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          this.controller.runJavaScript({
            script: 'test()',
            callback: (result: string)=> {
              this.webResult = result
              console.info(`The test() return value is: ${result}`)
Y
yamila 已提交
4837 4838
            }})
          console.info('url: ', e.url)
L
update  
laosan_ted 已提交
4839 4840 4841 4842 4843 4844 4845
        })
      }
    }
  }
  ```

  ```html
L
laosan_ted 已提交
4846
  <!-- index.html -->
L
update  
laosan_ted 已提交
4847 4848 4849 4850 4851 4852 4853 4854
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
Y
yamila 已提交
4855
        console.log('Ark WebComponent')
L
update  
laosan_ted 已提交
4856 4857 4858 4859 4860 4861 4862
        return "This value is from index.html"
    }
    </script>
  </html>

  ```

L
laosan_ted 已提交
4863
### stop<sup>(deprecated)</sup>
L
lixingchi1 已提交
4864

Z
zhou-liting125 已提交
4865
stop()
Z
zengyawen 已提交
4866

L
lixingchi1 已提交
4867
停止页面加载。
Z
zengyawen 已提交
4868

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

Z
zhou-liting125 已提交
4871
**示例:**
L
laosan_ted 已提交
4872

Z
zhou-liting125 已提交
4873 4874
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4875
  @Entry
L
update  
laosan_ted 已提交
4876 4877
  @Component
  struct WebComponent {
Y
yamila 已提交
4878
    controller: WebController = new WebController()
4879

L
update  
laosan_ted 已提交
4880 4881 4882
    build() {
      Column() {
        Button('stop')
L
laosan_ted 已提交
4883
          .onClick(() => {
Y
yamila 已提交
4884
            this.controller.stop()
L
laosan_ted 已提交
4885
          })
Z
zhou-liting125 已提交
4886
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4887
      }
L
update  
laosan_ted 已提交
4888 4889 4890 4891
    }
  }
  ```

L
laosan_ted 已提交
4892
### clearHistory<sup>(deprecated)</sup>
T
Ted 已提交
4893 4894 4895 4896 4897

clearHistory(): void

删除所有前进后退记录。

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

Z
zhou-liting125 已提交
4900
**示例:**
L
laosan_ted 已提交
4901

Z
zhou-liting125 已提交
4902 4903
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4904
  @Entry
L
update  
laosan_ted 已提交
4905 4906
  @Component
  struct WebComponent {
Y
yamila 已提交
4907
    controller: WebController = new WebController()
4908

L
update  
laosan_ted 已提交
4909 4910 4911
    build() {
      Column() {
        Button('clearHistory')
L
laosan_ted 已提交
4912
          .onClick(() => {
Y
yamila 已提交
4913
            this.controller.clearHistory()
L
laosan_ted 已提交
4914
          })
Z
zhou-liting125 已提交
4915
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4916
      }
L
update  
laosan_ted 已提交
4917 4918 4919 4920
    }
  }
  ```

L
lixiang 已提交
4921
## WebCookie<sup>(deprecated)</sup>
I
i-am-a-little-bird 已提交
4922

4923
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookie。通过controller方法中的getCookieManager方法可以获取WebCookie对象,进行后续的cookie管理操作。
I
i-am-a-little-bird 已提交
4924

L
lixiang 已提交
4925
### setCookie<sup>(deprecated)</sup>
4926
setCookie(url: string, value: string): boolean
I
i-am-a-little-bird 已提交
4927

4928
设置cookie,该方法为同步方法。设置成功返回true,否则返回false。
L
lixiang 已提交
4929
从API version 9开始不在维护,建议使用[setCookie<sup>9+</sup>](../apis/js-apis-webview.md#setcookie)代替。
T
Ted 已提交
4930

4931
**参数:**
T
Ted 已提交
4932

4933 4934
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
4935
| url   | string | 是    | -    | 要设置的cookie所属的url,建议使用完整的url。 |
4936
| value | string | 是    | -    | cookie的值。         |
Z
zhou-liting125 已提交
4937 4938

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

4940 4941 4942
| 类型      | 说明            |
| ------- | ------------- |
| boolean | 设置cookie是否成功。 |
4943

Z
zhou-liting125 已提交
4944
**示例:**
L
laosan_ted 已提交
4945

Z
zhou-liting125 已提交
4946 4947
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4948
  @Entry
L
update  
laosan_ted 已提交
4949 4950
  @Component
  struct WebComponent {
Y
yamila 已提交
4951
    controller: WebController = new WebController()
4952

L
update  
laosan_ted 已提交
4953 4954
    build() {
      Column() {
4955
        Button('setCookie')
L
laosan_ted 已提交
4956
          .onClick(() => {
L
lixiang 已提交
4957
            let result = this.controller.getCookieManager().setCookie("https://www.example.com", "a=b")
4958
            console.log("result: " + result)
L
laosan_ted 已提交
4959
          })
Z
zhou-liting125 已提交
4960
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4961
      }
L
update  
laosan_ted 已提交
4962 4963 4964
    }
  }
  ```
L
lixiang 已提交
4965 4966
### saveCookie<sup>(deprecated)</sup>
saveCookie(): boolean
L
update  
laosan_ted 已提交
4967

4968
将当前存在内存中的cookie同步到磁盘中,该方法为同步方法。
L
lixiang 已提交
4969
从API version 9开始不在维护,建议使用[saveCookieAsync<sup>9+</sup>](../apis/js-apis-webview.md#savecookieasync)代替。
4970 4971

**返回值:**
4972

4973 4974 4975
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 同步内存cookie到磁盘操作是否成功。 |
4976 4977

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

4979 4980 4981 4982 4983
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4984
    controller: WebController = new WebController()
4985

4986 4987
    build() {
      Column() {
L
lixiang 已提交
4988
        Button('saveCookie')
4989
          .onClick(() => {
L
lixiang 已提交
4990
            let result = this.controller.getCookieManager().saveCookie()
4991
            console.log("result: " + result)
4992 4993 4994 4995 4996
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
C
chensi10 已提交
4997
  ```