ts-basic-components-web.md 139.1 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
      }
    }
  }
  ```

Z
zengyawen 已提交
1314 1315
## 事件

L
liwenzhen 已提交
1316
不支持通用事件。
Z
zengyawen 已提交
1317

L
update  
laosan_ted 已提交
1318 1319 1320 1321 1322 1323
### onAlert

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

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

Z
zhou-liting125 已提交
1324
**参数:**
L
laosan_ted 已提交
1325

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

Z
zhou-liting125 已提交
1332
**返回值:**
L
laosan_ted 已提交
1333

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

Z
zhou-liting125 已提交
1338
**示例:**
L
laosan_ted 已提交
1339

Z
zhou-liting125 已提交
1340 1341
  ```ts
  // xxx.ets
L
lixiang 已提交
1342 1343
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1344
  @Entry
L
update  
laosan_ted 已提交
1345 1346
  @Component
  struct WebComponent {
L
lixiang 已提交
1347
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
update  
laosan_ted 已提交
1348 1349
    build() {
      Column() {
1350
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1351 1352
          .onAlert((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
1353
              title: 'onAlert',
L
laosan_ted 已提交
1354
              message: 'text',
L
laosan_ted 已提交
1355 1356 1357 1358 1359 1360 1361 1362
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1363 1364 1365 1366 1367 1368 1369 1370
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1371
            return true
L
update  
laosan_ted 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
          })
      }
    }
  }
  ```

### onBeforeUnload

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

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

Z
zhou-liting125 已提交
1384
**参数:**
L
laosan_ted 已提交
1385

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

Z
zhou-liting125 已提交
1392
**返回值:**
L
laosan_ted 已提交
1393

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

Z
zhou-liting125 已提交
1398
**示例:**
L
laosan_ted 已提交
1399

Z
zhou-liting125 已提交
1400 1401
  ```ts
  // xxx.ets
L
lixiang 已提交
1402 1403
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1404
  @Entry
L
update  
laosan_ted 已提交
1405 1406
  @Component
  struct WebComponent {
L
lixiang 已提交
1407
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1408

L
update  
laosan_ted 已提交
1409 1410
    build() {
      Column() {
Z
zhou-liting125 已提交
1411
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1412
          .onBeforeUnload((event) => {
Y
yamila 已提交
1413 1414
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
L
laosan_ted 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
            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 已提交
1434
            return true
L
laosan_ted 已提交
1435 1436
          })
      }
L
update  
laosan_ted 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
    }
  }
  ```

### onConfirm

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

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

Z
zhou-liting125 已提交
1447
**参数:**
L
laosan_ted 已提交
1448

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

Z
zhou-liting125 已提交
1455
**返回值:**
L
laosan_ted 已提交
1456

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

Z
zhou-liting125 已提交
1461
**示例:**
L
laosan_ted 已提交
1462

Z
zhou-liting125 已提交
1463 1464
  ```ts
  // xxx.ets
L
lixiang 已提交
1465 1466
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1467
  @Entry
L
update  
laosan_ted 已提交
1468 1469
  @Component
  struct WebComponent {
L
lixiang 已提交
1470
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1471

L
update  
laosan_ted 已提交
1472 1473
    build() {
      Column() {
Z
zhou-liting125 已提交
1474
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1475
          .onConfirm((event) => {
Y
yamila 已提交
1476 1477 1478
            console.log("event.url:" + event.url)
            console.log("event.message:" + event.message)
            console.log("event.result:" + event.result)
L
laosan_ted 已提交
1479
            AlertDialog.show({
L
laosan_ted 已提交
1480
              title: 'onConfirm',
L
laosan_ted 已提交
1481
              message: 'text',
L
laosan_ted 已提交
1482 1483 1484 1485 1486 1487 1488 1489
              primaryButton: {
                value: 'cancel',
                action: () => {
                  event.result.handleCancel()
                }
              },
              secondaryButton: {
                value: 'ok',
L
laosan_ted 已提交
1490 1491 1492 1493 1494 1495 1496 1497
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
Y
yamila 已提交
1498
            return true
L
update  
laosan_ted 已提交
1499
          })
L
laosan_ted 已提交
1500
      }
L
update  
laosan_ted 已提交
1501 1502 1503 1504 1505 1506 1507 1508
    }
  }
  ```

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

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

Z
zhou-liting125 已提交
1509
**参数:**
L
laosan_ted 已提交
1510

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

Z
zhou-liting125 已提交
1517
**返回值:**
L
laosan_ted 已提交
1518

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

Z
zhou-liting125 已提交
1523
**示例:**
L
laosan_ted 已提交
1524

Z
zhou-liting125 已提交
1525 1526
  ```ts
  // xxx.ets
L
lixiang 已提交
1527 1528
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1529
  @Entry
L
update  
laosan_ted 已提交
1530 1531
  @Component
  struct WebComponent {
L
lixiang 已提交
1532
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1533

L
update  
laosan_ted 已提交
1534 1535
    build() {
      Column() {
1536
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1537
          .onPrompt((event) => {
Y
yamila 已提交
1538 1539 1540
            console.log("url:" + event.url)
            console.log("message:" + event.message)
            console.log("value:" + event.value)
L
laosan_ted 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
            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 已提交
1560
            return true
L
laosan_ted 已提交
1561 1562
          })
      }
L
update  
laosan_ted 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    }
  }
  ```

### onConsole

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

通知宿主应用JavaScript console消息。

Z
zhou-liting125 已提交
1573
**参数:**
L
laosan_ted 已提交
1574

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

Z
zhou-liting125 已提交
1579
**返回值:**
L
laosan_ted 已提交
1580

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

Z
zhou-liting125 已提交
1585
**示例:**
L
laosan_ted 已提交
1586

Z
zhou-liting125 已提交
1587 1588
  ```ts
  // xxx.ets
L
lixiang 已提交
1589 1590
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1591
  @Entry
L
update  
laosan_ted 已提交
1592 1593
  @Component
  struct WebComponent {
L
lixiang 已提交
1594
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1595

L
update  
laosan_ted 已提交
1596 1597
    build() {
      Column() {
Z
zhou-liting125 已提交
1598
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1599
          .onConsole((event) => {
Y
yamila 已提交
1600 1601 1602 1603 1604
            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 已提交
1605 1606
          })
      }
L
update  
laosan_ted 已提交
1607 1608 1609 1610 1611 1612 1613 1614
    }
  }
  ```

### onDownloadStart

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

Z
zhou-liting125 已提交
1615
**参数:**
L
laosan_ted 已提交
1616

1617 1618 1619 1620 1621 1622
| 参数名                | 参数类型          | 参数描述                                |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | 文件下载的URL。                           |
| contentDisposition | string        | 服务器返回的 Content-Disposition响应头,可能为空。 |
| mimetype           | string        | 服务器返回内容媒体类型(MIME)信息。                |
| contentLength      | contentLength | 服务器返回文件的长度。                         |
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
          .onDownloadStart((event) => {
Y
yamila 已提交
1639 1640 1641 1642 1643
            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 已提交
1644 1645
          })
      }
L
update  
laosan_ted 已提交
1646 1647 1648 1649 1650 1651 1652 1653
    }
  }
  ```

### onErrorReceive

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

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

Z
zhou-liting125 已提交
1656
**参数:**
L
laosan_ted 已提交
1657

1658 1659 1660 1661
| 参数名     | 参数类型                                     | 参数描述            |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。      |
| error   | [WebResourceError](#webresourceerror)    | 网页加载资源错误的封装信息 。 |
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
          .onErrorReceive((event) => {
Y
yamila 已提交
1678 1679 1680 1681 1682 1683 1684 1685 1686
            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 已提交
1687
            for (let i of result) {
Y
yamila 已提交
1688
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue)
L
laosan_ted 已提交
1689 1690 1691
            }
          })
      }
L
update  
laosan_ted 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    }
  }
  ```

### onHttpErrorReceive

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

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

Z
zhou-liting125 已提交
1702
**参数:**
L
laosan_ted 已提交
1703

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

Z
zhou-liting125 已提交
1709
**示例:**
L
laosan_ted 已提交
1710

Z
zhou-liting125 已提交
1711 1712
  ```ts
  // xxx.ets
L
lixiang 已提交
1713 1714
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1715
  @Entry
L
update  
laosan_ted 已提交
1716 1717
  @Component
  struct WebComponent {
L
lixiang 已提交
1718
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1719

L
update  
laosan_ted 已提交
1720 1721
    build() {
      Column() {
Z
zhou-liting125 已提交
1722
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1723
          .onHttpErrorReceive((event) => {
Y
yamila 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
            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 已提交
1735
            for (let i of result) {
Y
yamila 已提交
1736
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1737
            }
Y
yamila 已提交
1738 1739
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
L
laosan_ted 已提交
1740
            for (let i of resph) {
Y
yamila 已提交
1741
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
L
laosan_ted 已提交
1742 1743 1744
            }
          })
      }
L
update  
laosan_ted 已提交
1745 1746 1747 1748 1749 1750 1751 1752
    }
  }
  ```

### onPageBegin

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

1753

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

Z
zhou-liting125 已提交
1756
**参数:**
L
laosan_ted 已提交
1757

1758 1759 1760
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1761

Z
zhou-liting125 已提交
1762
**示例:**
L
laosan_ted 已提交
1763

Z
zhou-liting125 已提交
1764 1765
  ```ts
  // xxx.ets
L
lixiang 已提交
1766 1767
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1768
  @Entry
L
update  
laosan_ted 已提交
1769 1770
  @Component
  struct WebComponent {
L
lixiang 已提交
1771
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1772

L
update  
laosan_ted 已提交
1773 1774
    build() {
      Column() {
Z
zhou-liting125 已提交
1775
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1776
          .onPageBegin((event) => {
Y
yamila 已提交
1777
            console.log('url:' + event.url)
L
laosan_ted 已提交
1778 1779
          })
      }
L
update  
laosan_ted 已提交
1780 1781 1782 1783 1784 1785 1786 1787
    }
  }
  ```

### onPageEnd

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

1788

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

Z
zhou-liting125 已提交
1791
**参数:**
L
laosan_ted 已提交
1792

1793 1794 1795
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
1796

Z
zhou-liting125 已提交
1797
**示例:**
L
laosan_ted 已提交
1798

Z
zhou-liting125 已提交
1799 1800
  ```ts
  // xxx.ets
L
lixiang 已提交
1801 1802
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1803
  @Entry
L
update  
laosan_ted 已提交
1804 1805
  @Component
  struct WebComponent {
L
lixiang 已提交
1806
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1807

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

### onProgressChange

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

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

Z
zhou-liting125 已提交
1825
**参数:**
L
laosan_ted 已提交
1826

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

L
laosan_ted 已提交
1831 1832
**示例:**

Z
zhou-liting125 已提交
1833 1834
  ```ts
  // xxx.ets
L
lixiang 已提交
1835 1836
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1837
  @Entry
L
update  
laosan_ted 已提交
1838 1839
  @Component
  struct WebComponent {
L
lixiang 已提交
1840
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1841

L
update  
laosan_ted 已提交
1842 1843
    build() {
      Column() {
Z
zhou-liting125 已提交
1844
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1845 1846 1847 1848
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
L
update  
laosan_ted 已提交
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
    }
  }
  ```

### onTitleReceive

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

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

Z
zhou-liting125 已提交
1859
**参数:**
L
laosan_ted 已提交
1860

1861 1862 1863
| 参数名   | 参数类型   | 参数描述          |
| ----- | ------ | ------------- |
| title | string | document标题内容。 |
L
update  
laosan_ted 已提交
1864

L
laosan_ted 已提交
1865 1866
**示例:**

Z
zhou-liting125 已提交
1867 1868
  ```ts
  // xxx.ets
L
lixiang 已提交
1869 1870
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1871
  @Entry
L
update  
laosan_ted 已提交
1872 1873
  @Component
  struct WebComponent {
L
lixiang 已提交
1874
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1875

L
update  
laosan_ted 已提交
1876 1877
    build() {
      Column() {
Z
zhou-liting125 已提交
1878
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1879 1880 1881 1882
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
L
update  
laosan_ted 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
    }
  }
  ```

### onRefreshAccessedHistory

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

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

Z
zhou-liting125 已提交
1893
**参数:**
L
laosan_ted 已提交
1894

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

L
laosan_ted 已提交
1900 1901
**示例:**

Z
zhou-liting125 已提交
1902 1903
  ```ts
  // xxx.ets
L
lixiang 已提交
1904 1905
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1906
  @Entry
L
update  
laosan_ted 已提交
1907 1908
  @Component
  struct WebComponent {
L
lixiang 已提交
1909
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1910

L
update  
laosan_ted 已提交
1911 1912
    build() {
      Column() {
Z
zhou-liting125 已提交
1913
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1914
          .onRefreshAccessedHistory((event) => {
Y
yamila 已提交
1915
            console.log('url:' + event.url + ' isReload:' + event.isRefreshed)
L
laosan_ted 已提交
1916 1917
          })
      }
L
update  
laosan_ted 已提交
1918 1919 1920 1921
    }
  }
  ```

L
laosan_ted 已提交
1922
### onRenderExited<sup>9+</sup>
L
update  
laosan_ted 已提交
1923 1924 1925 1926 1927

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

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

Z
zhou-liting125 已提交
1928
**参数:**
L
laosan_ted 已提交
1929

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

L
laosan_ted 已提交
1934 1935
**示例:**

Z
zhou-liting125 已提交
1936 1937
  ```ts
  // xxx.ets
L
lixiang 已提交
1938 1939
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
1940
  @Entry
L
update  
laosan_ted 已提交
1941 1942
  @Component
  struct WebComponent {
L
lixiang 已提交
1943
    controller: web_webview.WebviewController = new web_webview.WebviewController()
1944

L
update  
laosan_ted 已提交
1945 1946
    build() {
      Column() {
L
laosan_ted 已提交
1947 1948
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
Y
yamila 已提交
1949
            console.log('reason:' + event.renderExitReason)
L
laosan_ted 已提交
1950 1951
          })
      }
L
update  
laosan_ted 已提交
1952 1953 1954 1955 1956 1957
    }
  }
  ```

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

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

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

Z
zhou-liting125 已提交
1962
**参数:**
L
laosan_ted 已提交
1963

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

L
laosan_ted 已提交
1969 1970
**返回值:**

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

L
laosan_ted 已提交
1975 1976
**示例:**

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

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

L
update  
laosan_ted 已提交
1986 1987
    build() {
      Column() {
Z
zhou-liting125 已提交
1988
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
          .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 已提交
2005
              }
L
laosan_ted 已提交
2006
            })
Y
yamila 已提交
2007
            return true
L
update  
laosan_ted 已提交
2008
          })
L
laosan_ted 已提交
2009
      }
L
update  
laosan_ted 已提交
2010 2011 2012 2013
    }
  }
  ```

L
laosan_ted 已提交
2014 2015 2016 2017 2018 2019 2020 2021
### onResourceLoad<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2022 2023 2024
| 参数名  | 参数类型   | 参数描述           |
| ---- | ------ | -------------- |
| url  | string | 所加载的资源文件url信息。 |
L
laosan_ted 已提交
2025 2026 2027 2028 2029

**示例:**

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

L
laosan_ted 已提交
2032 2033 2034
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2035
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2036

L
laosan_ted 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
    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 已提交
2056 2057
| 参数名      | 参数类型   | 参数描述         |
| -------- | ------ | ------------ |
L
laosan_ted 已提交
2058 2059 2060 2061 2062 2063 2064
| oldScale | number | 变化前的显示比例百分比。 |
| newScale | number | 变化后的显示比例百分比。 |

**示例:**

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

L
laosan_ted 已提交
2067 2068 2069
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2070
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2071

L
laosan_ted 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onScaleChange((event) => {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
          })
      }
    }
  }
  ```

2083
### onUrlLoadIntercept<sup>(deprecated)</sup>
L
update  
laosan_ted 已提交
2084 2085 2086

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

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

Z
zhou-liting125 已提交
2090
**参数:**
L
laosan_ted 已提交
2091

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

Z
zhou-liting125 已提交
2096
**返回值:**
L
laosan_ted 已提交
2097

2098 2099 2100
| 类型      | 说明                       |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |
L
update  
laosan_ted 已提交
2101

L
laosan_ted 已提交
2102 2103
**示例:**

Z
zhou-liting125 已提交
2104 2105
  ```ts
  // xxx.ets
L
lixiang 已提交
2106 2107
  import web_webview from '@ohos.web.webview'

Z
zhou-liting125 已提交
2108
  @Entry
L
update  
laosan_ted 已提交
2109 2110
  @Component
  struct WebComponent {
L
lixiang 已提交
2111
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2112

L
update  
laosan_ted 已提交
2113 2114
    build() {
      Column() {
Z
zhou-liting125 已提交
2115
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2116 2117
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
Y
yamila 已提交
2118
            return true
L
laosan_ted 已提交
2119 2120
          })
      }
L
update  
laosan_ted 已提交
2121 2122 2123 2124 2125 2126
    }
  }
  ```

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

L
laosan_ted 已提交
2127
onInterceptRequest(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)
L
update  
laosan_ted 已提交
2128 2129 2130

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

Z
zhou-liting125 已提交
2131
**参数:**
L
laosan_ted 已提交
2132

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

Z
zhou-liting125 已提交
2137
**返回值:**
L
laosan_ted 已提交
2138

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

L
laosan_ted 已提交
2143 2144
**示例:**

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

Z
zhou-liting125 已提交
2149
  @Entry
L
update  
laosan_ted 已提交
2150 2151
  @Component
  struct WebComponent {
L
lixiang 已提交
2152
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2153 2154
    responseweb: WebResourceResponse = new WebResourceResponse()
    heads:Header[] = new Array()
L
laosan_ted 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163
    @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 已提交
2164 2165
    build() {
      Column() {
2166
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2167
          .onInterceptRequest((event) => {
Y
yamila 已提交
2168
            console.log('url:' + event.request.getRequestUrl())
L
laosan_ted 已提交
2169 2170 2171 2172 2173 2174 2175 2176
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
Y
yamila 已提交
2177 2178 2179 2180 2181 2182 2183 2184 2185
            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 已提交
2186
          })
L
update  
laosan_ted 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
      }
    }
  }
  ```

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

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

通知收到http auth认证请求。

Z
zhou-liting125 已提交
2198
**参数:**
L
laosan_ted 已提交
2199

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

Z
zhou-liting125 已提交
2206
**返回值:**
L
laosan_ted 已提交
2207

2208 2209 2210
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 返回false表示此次认证失败,否则成功。 |
L
update  
laosan_ted 已提交
2211

L
laosan_ted 已提交
2212 2213
**示例:**

Z
zhou-liting125 已提交
2214 2215
  ```ts
  // xxx.ets
L
laosan_ted 已提交
2216
  import web_webview from '@ohos.web.webview'
Z
zhou-liting125 已提交
2217
  @Entry
L
update  
laosan_ted 已提交
2218 2219
  @Component
  struct WebComponent {
L
lixiang 已提交
2220
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2221
    httpAuth: boolean = false
2222

L
update  
laosan_ted 已提交
2223 2224
    build() {
      Column() {
2225 2226 2227
        Web({ src: 'www.example.com', controller: this.controller })
          .onHttpAuthRequest((event) => {
            AlertDialog.show({
L
laosan_ted 已提交
2228
              title: 'onHttpAuthRequest',
2229
              message: 'text',
L
laosan_ted 已提交
2230 2231 2232
              primaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2233
                  event.handler.cancel()
L
laosan_ted 已提交
2234 2235 2236 2237
                }
              },
              secondaryButton: {
                value: 'ok',
2238
                action: () => {
Y
yamila 已提交
2239
                  this.httpAuth = event.handler.isHttpAuthInfoSaved()
2240
                  if (this.httpAuth == false) {
L
laosan_ted 已提交
2241
                    web_webview.WebDataBase.saveHttpAuthCredentials(
2242 2243 2244 2245 2246
                      event.host,
                      event.realm,
                      "2222",
                      "2222"
                    )
Y
yamila 已提交
2247
                    event.handler.cancel()
2248
                  }
L
update  
laosan_ted 已提交
2249
                }
2250 2251
              },
              cancel: () => {
Y
yamila 已提交
2252
                event.handler.cancel()
L
update  
laosan_ted 已提交
2253
              }
2254
            })
Y
yamila 已提交
2255
            return true
L
update  
laosan_ted 已提交
2256
          })
Y
yu-shihao4 已提交
2257
      }
L
update  
laosan_ted 已提交
2258 2259 2260
    }
  }
  ```
2261 2262
### onSslErrorEventReceive<sup>9+</sup>

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

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

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

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

**示例:**
2275 2276 2277

  ```ts
  // xxx.ets
I
bugfix  
i-am-a-little-bird 已提交
2278
  import web_webview from '@ohos.web.webview'
2279 2280 2281
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2282
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2283

2284 2285 2286
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
I
bugfix  
i-am-a-little-bird 已提交
2287
          .onSslErrorEventReceive((event) => {
2288
            AlertDialog.show({
I
bugfix  
i-am-a-little-bird 已提交
2289
              title: 'onSslErrorEventReceive',
2290
              message: 'text',
I
bugfix  
i-am-a-little-bird 已提交
2291 2292
              primaryButton: {
                value: 'confirm',
2293
                action: () => {
Y
yamila 已提交
2294
                  event.handler.handleConfirm()
I
bugfix  
i-am-a-little-bird 已提交
2295 2296 2297 2298 2299
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2300
                  event.handler.handleCancel()
2301 2302 2303
                }
              },
              cancel: () => {
Y
yamila 已提交
2304
                event.handler.handleCancel()
2305 2306
              }
            })
Y
yamila 已提交
2307
            return true
2308 2309 2310 2311 2312 2313
          })
      }
    }
  }
  ```

I
i-am-a-little-bird 已提交
2314 2315 2316 2317
### 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 已提交
2318
通知用户收到SSL客户端证书请求事件。
I
i-am-a-little-bird 已提交
2319 2320 2321

**参数:**

H
HelloCrease 已提交
2322 2323 2324 2325 2326 2327 2328
| 参数名      | 参数类型                                     | 参数描述            |
| -------- | ---------------------------------------- | --------------- |
| handler  | [ClientAuthenticationHandler](#clientauthenticationhandler9) | 通知Web组件用户操作行为。  |
| host     | string                                   | 请求证书服务器的主机名。    |
| port     | number                                   | 请求证书服务器的端口号。    |
| keyTypes | Array<string>                            | 可接受的非对称秘钥类型。    |
| issuers  | Array<string>                            | 与私钥匹配的证书可接受颁发者。 |
I
i-am-a-little-bird 已提交
2329 2330 2331

  **示例:**
  ```ts
2332
  // xxx.ets API9
I
i-am-a-little-bird 已提交
2333 2334 2335 2336
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2337
    controller: web_webview.WebviewController = new web_webview.WebviewController()
I
i-am-a-little-bird 已提交
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
          .onClientAuthenticationRequest((event) => {
            AlertDialog.show({
              title: 'onClientAuthenticationRequest',
              message: 'text',
              primaryButton: {
                value: 'confirm',
                action: () => {
Y
yamila 已提交
2349
                  event.handler.confirm("/system/etc/user.pk8", "/system/etc/chain-user.pem")
I
i-am-a-little-bird 已提交
2350 2351 2352 2353 2354
                }
              },
              secondaryButton: {
                value: 'cancel',
                action: () => {
Y
yamila 已提交
2355
                  event.handler.cancel()
I
i-am-a-little-bird 已提交
2356 2357 2358
                }
              },
              cancel: () => {
Y
yamila 已提交
2359
                event.handler.ignore()
I
i-am-a-little-bird 已提交
2360 2361
              }
            })
Y
yamila 已提交
2362
            return true
I
i-am-a-little-bird 已提交
2363 2364 2365 2366 2367 2368
          })
      }
    }
  }
  ```

2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 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
  ```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%')
    }
  }
  ```

2469 2470 2471 2472 2473 2474 2475
### onPermissionRequest<sup>9+</sup>

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

通知收到获取权限请求。

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

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

L
laosan_ted 已提交
2481 2482
**示例:**

2483 2484
  ```ts
  // xxx.ets
L
lixiang 已提交
2485 2486
  import web_webview from '@ohos.web.webview'

2487 2488 2489
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2490
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2491 2492
    build() {
      Column() {
2493 2494 2495 2496 2497
        Web({ src: 'www.example.com', controller: this.controller })
          .onPermissionRequest((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
L
laosan_ted 已提交
2498 2499 2500
              primaryButton: {
                value: 'deny',
                action: () => {
Y
yamila 已提交
2501
                  event.request.deny()
L
laosan_ted 已提交
2502 2503 2504
                }
              },
              secondaryButton: {
2505 2506
                value: 'onConfirm',
                action: () => {
Y
yamila 已提交
2507
                  event.request.grant(event.request.getAccessibleResource())
2508 2509 2510
                }
              },
              cancel: () => {
Y
yamila 已提交
2511
                event.request.deny()
2512
              }
2513
            })
2514
          })
Y
yu-shihao4 已提交
2515
      }
2516 2517 2518
    }
  }
  ```
2519

2520 2521 2522 2523
### onContextMenuShow<sup>9+</sup>

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

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

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

H
HelloCrease 已提交
2528 2529 2530 2531
| 参数名    | 参数类型                                     | 参数描述        |
| ------ | ---------------------------------------- | ----------- |
| param  | [WebContextMenuParam](#webcontextmenuparam9) | 菜单相关参数。     |
| result | [WebContextMenuResult](#webcontextmenuresult9) | 菜单相应事件传入内核。 |
2532

Y
yu-shihao4 已提交
2533 2534
**返回值:**

H
HelloCrease 已提交
2535 2536
| 类型      | 说明                       |
| ------- | ------------------------ |
Y
yu-shihao4 已提交
2537 2538
| boolean | 自定义菜单返回true,默认菜单返回false。 |

L
laosan_ted 已提交
2539
**示例:**
Y
yu-shihao4 已提交
2540

2541 2542
  ```ts
  // xxx.ets
L
lixiang 已提交
2543 2544
  import web_webview from '@ohos.web.webview'

2545 2546 2547
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2548
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2549 2550
    build() {
      Column() {
2551
        Web({ src: 'www.example.com', controller: this.controller })
Y
yu-shihao4 已提交
2552
          .onContextMenuShow((event) => {
Y
yamila 已提交
2553 2554 2555
            console.info("x coord = " + event.param.x())
            console.info("link url = " + event.param.getLinkUrl())
            return true
2556 2557
        })
      }
2558 2559 2560
    }
  }
  ```
2561

L
laosan_ted 已提交
2562 2563 2564 2565 2566 2567 2568 2569
### onScroll<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2570 2571
| 参数名     | 参数类型   | 参数描述         |
| ------- | ------ | ------------ |
L
1111  
lixiang 已提交
2572 2573
| xOffset | number | 以网页最左端为基准,水平滚动条滚动所在位置。 |
| yOffset | number | 以网页最上端为基准,竖直滚动条滚动所在位置。 |
L
laosan_ted 已提交
2574 2575 2576 2577 2578

**示例:**

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

L
laosan_ted 已提交
2581 2582 2583
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2584
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2585 2586 2587 2588
    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
        .onScroll((event) => {
Y
yamila 已提交
2589 2590
            console.info("x = " + event.xOffset)
            console.info("y = " + event.yOffset)
L
laosan_ted 已提交
2591 2592 2593 2594 2595 2596
        })
      }
    }
  }
  ```

2597 2598 2599 2600
### onGeolocationShow

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

2601
通知用户收到地理位置信息获取请求。
2602 2603 2604

**参数:**

H
HelloCrease 已提交
2605 2606
| 参数名         | 参数类型                            | 参数描述           |
| ----------- | ------------------------------- | -------------- |
2607
| origin      | string                          | 指定源的字符串索引。     |
H
HelloCrease 已提交
2608
| geolocation | [JsGeolocation](#jsgeolocation) | 通知Web组件用户操作行为。 |
2609 2610

**示例:**
2611

2612 2613
  ```ts
  // xxx.ets
L
lixiang 已提交
2614 2615
  import web_webview from '@ohos.web.webview'

2616 2617 2618
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2619
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
    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 已提交
2631
                event.geolocation.invoke(event.origin, true, true)
2632 2633 2634
              }
            },
            cancel: () => {
Y
yamila 已提交
2635
              event.geolocation.invoke(event.origin, false, true)
2636 2637 2638 2639 2640 2641 2642 2643
            }
          })
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2644 2645 2646 2647 2648 2649 2650 2651
### onGeolocationHide

onGeolocationHide(callback: () => void)

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

**参数:**

H
HelloCrease 已提交
2652 2653 2654
| 参数名      | 参数类型       | 参数描述                 |
| -------- | ---------- | -------------------- |
| callback | () => void | 地理位置信息获取请求已被取消的回调函数。 |
L
laosan_ted 已提交
2655 2656 2657 2658 2659

**示例:**

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

L
laosan_ted 已提交
2662 2663 2664
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2665
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationHide(() => {
          console.log("onGeolocationHide...")
        })
      }
    }
  }
  ```

E
echoorchid 已提交
2678 2679 2680 2681 2682 2683 2684 2685
### onFullScreenEnter<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2686 2687 2688
| 参数名     | 参数类型                                     | 参数描述           |
| ------- | ---------------------------------------- | -------------- |
| handler | [FullScreenExitHandler](#fullscreenexithandler9) | 用于退出全屏模式的函数句柄。 |
E
echoorchid 已提交
2689 2690 2691 2692 2693

**示例:**

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

E
echoorchid 已提交
2696 2697 2698
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2699
    controller: web_webview.WebviewController = new web_webview.WebviewController()
Y
yamila 已提交
2700
    handler: FullScreenExitHandler = null
E
echoorchid 已提交
2701 2702 2703 2704
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onFullScreenEnter((event) => {
Y
yamila 已提交
2705 2706
          console.log("onFullScreenEnter...")
          this.handler = event.handler
E
echoorchid 已提交
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
        })
      }
    }
  }
  ```

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

onFullScreenExit(callback: () => void)

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

**参数:**

H
HelloCrease 已提交
2721 2722 2723
| 参数名      | 参数类型       | 参数描述          |
| -------- | ---------- | ------------- |
| callback | () => void | 退出全屏模式时的回调函数。 |
E
echoorchid 已提交
2724 2725 2726 2727 2728

**示例:**

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

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

X
xiongjun_gitee 已提交
2751 2752 2753 2754
### onWindowNew<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2761 2762 2763 2764 2765
| 参数名           | 参数类型                                     | 参数描述                       |
| ------------- | ---------------------------------------- | -------------------------- |
| isAlert       | boolean                                  | true代表请求创建对话框,false代表新标签页。 |
| isUserTrigger | boolean                                  | true代表用户触发,false代表非用户触发。   |
| targetUrl     | string                                   | 目标url。                     |
L
lixiang 已提交
2766
| handler       | [ControllerHandler](#controllerhandler9) | 用于设置新建窗口的WebviewController实例。  |
X
xiongjun_gitee 已提交
2767 2768 2769 2770 2771

**示例:**

  ```ts
  // xxx.ets
L
laosan_ted 已提交
2772
  import web_webview from '@ohos.web.webview'
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
  
  //在同一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 已提交
2792 2793 2794
  @Entry
  @Component
  struct WebComponent {
2795
    controller: web_webview.WebviewController = new web_webview.WebviewController()
2796
    dialogController: CustomDialogController = null
X
xiongjun_gitee 已提交
2797 2798
    build() {
      Column() {
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
        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 已提交
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
      }
    }
  }
  ```

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

onWindowExit(callback: () => void)

通知用户窗口关闭请求。

**参数:**

H
HelloCrease 已提交
2831 2832 2833
| 参数名      | 参数类型       | 参数描述         |
| -------- | ---------- | ------------ |
| callback | () => void | 窗口请求关闭的回调函数。 |
X
xiongjun_gitee 已提交
2834 2835 2836 2837 2838

**示例:**

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

X
xiongjun_gitee 已提交
2841 2842 2843
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2844
    controller: web_webview.WebviewController = new web_webview.WebviewController()
X
xiongjun_gitee 已提交
2845 2846 2847
    build() {
      Column() {
        Web({ src:'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2848
        .onWindowExit(() => {
Y
yamila 已提交
2849
          console.log("onWindowExit...")
X
xiongjun_gitee 已提交
2850 2851 2852 2853 2854 2855
        })
      }
    }
  }
  ```

L
laosan_ted 已提交
2856 2857 2858 2859 2860 2861 2862 2863
### onSearchResultReceive<sup>9+</sup>

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

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

**参数:**

H
HelloCrease 已提交
2864 2865 2866 2867 2868
| 参数名                | 参数类型    | 参数描述                                     |
| ------------------ | ------- | ---------------------------------------- |
| activeMatchOrdinal | number  | 当前匹配的查找项的序号(从0开始)。                       |
| numberOfMatches    | number  | 所有匹配到的关键词的个数。                            |
| isDoneCounting     | boolean | 当次页内查找操作是否结束。该方法可能会回调多次,直到isDoneCounting为true为止。 |
L
laosan_ted 已提交
2869 2870 2871 2872 2873

**示例:**

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

L
laosan_ted 已提交
2876 2877 2878
  @Entry
  @Component
  struct WebComponent {
L
lixiang 已提交
2879
    controller: web_webview.WebviewController = new web_webview.WebviewController()
L
laosan_ted 已提交
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892

    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 已提交
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902
### onDataResubmitted<sup>9+</sup>

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

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

**参数:**

| 参数名  | 参数类型                                             | 参数描述               |
| ------- | ---------------------------------------------------- | ---------------------- |
C
chensi10 已提交
2903
| handler | [DataResubmissionHandler](#dataresubmissionhandler9) | 表单数据重新提交句柄。 |
C
chensi10 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916

**示例:**

  ```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 已提交
2917
         .onDataResubmitted((event) => {
C
chensi10 已提交
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
          console.log('onDataResubmitted')
          event.handler.resend();
        })
      }
    }
  }
  ```

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

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

C
chensi10 已提交
2930
设置旧页面不再呈现,新页面即将可见时触发的回调函数。
C
chensi10 已提交
2931 2932 2933

**参数:**

C
chensi10 已提交
2934 2935 2936
| 参数名 | 参数类型 | 参数描述                                          |
| ------ | -------- | ------------------------------------------------- |
| url    | string   | 旧页面不再呈现,新页面即将可见时新页面的url地址。 |
C
chensi10 已提交
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949

**示例:**

  ```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 已提交
2950
         .onPageVisible((event) => {
C
chensi10 已提交
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
          console.log('onPageVisible url:' + event.url)
        })
      }
    }
  }
  ```

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

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

L
1111  
lixiang 已提交
2962
设置键盘事件的回调函数,该回调在被Webview使用前触发。
C
chensi10 已提交
2963 2964 2965 2966 2967 2968 2969

**参数:**

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

C
chensi10 已提交
2970 2971 2972 2973 2974 2975
**返回值:**

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

C
chensi10 已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
**示例:**

  ```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 已提交
2988
         .onInterceptKeyEvent((event) => {
2989
          if (event.keyCode == 2017 || event.keyCode == 2018) {
C
chensi10 已提交
2990 2991 2992 2993
            console.info(`onInterceptKeyEvent get event.keyCode ${event.keyCode}`)
            return true;
          }
          return false;
C
chensi10 已提交
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
        })
      }
    }
  }
  ```

### 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 已提交
3024 3025
        Web({ src:'www.baidu.com', controller: this.controller })
         .onTouchIconUrlReceived((event) => {
C
chensi10 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
          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 已提交
3059 3060
         .onFaviconReceived((event) => {
          console.log('onFaviconReceived:' + JSON.stringify(event))
C
chensi10 已提交
3061 3062 3063 3064 3065 3066 3067
          this.icon = event.favicon;
        })
      }
    }
  }
  ```

L
Lei Gao 已提交
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 3098 3099 3100 3101
### 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)
          })
      }
    }
  }
  ```

3102 3103
### onFirstContentfulPaint<sup>10+</sup>

3104
onFirstContentfulPaint(callback: (event?: { navigationStartTick: number, firstContentfulPaintMs: number }) => void)
3105 3106 3107 3108 3109 3110 3111

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

**参数:**

| 参数名                 |  参数类型  | 参数描述                            |
| -----------------------| -------- | ----------------------------------- |
3112 3113
| navigationStartTick    | number   | navigation开始的时间,单位以微秒表示。|
| firstContentfulPaintMs | number   | 从navigation开始第一次绘制内容的时间,单位是以毫秒表示。|
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137

**示例:**

  ```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)
          })
      }
    }
  }
  ```

3138 3139
### onLoadIntercept<sup>10+</sup>

3140
onLoadIntercept(callback: (event?: { data: WebResourceRequest }) => boolean)
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169

当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 })
3170 3171 3172 3173 3174
          .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())
3175 3176 3177 3178 3179 3180 3181
            return true
          })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
3182
## ConsoleMessage
3183

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

Z
zhou-liting125 已提交
3186
### getLineNumber
3187

Z
zhou-liting125 已提交
3188 3189 3190 3191
getLineNumber(): number

获取ConsoleMessage的行数。

3192
**返回值:**
L
laosan_ted 已提交
3193

3194 3195 3196
| 类型     | 说明                   |
| ------ | -------------------- |
| number | 返回ConsoleMessage的行数。 |
Z
zhou-liting125 已提交
3197 3198 3199 3200 3201 3202 3203

### getMessage

getMessage(): string

获取ConsoleMessage的日志信息。

3204
**返回值:**
L
laosan_ted 已提交
3205

3206 3207 3208
| 类型     | 说明                     |
| ------ | ---------------------- |
| string | 返回ConsoleMessage的日志信息。 |
Z
zhou-liting125 已提交
3209 3210 3211 3212

### getMessageLevel

getMessageLevel(): MessageLevel
3213

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

3216
**返回值:**
L
laosan_ted 已提交
3217

3218 3219 3220
| 类型                                | 说明                     |
| --------------------------------- | ---------------------- |
| [MessageLevel](#messagelevel枚举说明) | 返回ConsoleMessage的信息级别。 |
Z
zhou-liting125 已提交
3221 3222 3223 3224 3225 3226 3227

### getSourceId

getSourceId(): string

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

3228
**返回值:**
L
laosan_ted 已提交
3229

3230 3231 3232
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回网页源文件路径和名字。 |
Z
zhou-liting125 已提交
3233 3234

## JsResult
L
lixingchi1 已提交
3235

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

Z
zhou-liting125 已提交
3238 3239 3240
### handleCancel

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

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

Z
zhou-liting125 已提交
3244
### handleConfirm
L
add web  
liujinwei 已提交
3245

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

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

Z
zhou-liting125 已提交
3250 3251 3252 3253 3254 3255
### handlePromptConfirm<sup>9+</sup>

handlePromptConfirm(result: string): void

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

Z
zhou-liting125 已提交
3256
**参数:**
L
laosan_ted 已提交
3257

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

E
echoorchid 已提交
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
## FullScreenExitHandler<sup>9+</sup>

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

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

exitFullScreen(): void

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

X
xiongjun_gitee 已提交
3272 3273
## ControllerHandler<sup>9+</sup>

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

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

3278
setWebController(controller: WebviewController): void
X
xiongjun_gitee 已提交
3279

3280
设置WebviewController对象,如果不需要打开新窗口请设置为null。
X
xiongjun_gitee 已提交
3281 3282 3283

**参数:**

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

Z
zhou-liting125 已提交
3288 3289
## WebResourceError

L
update  
laosan_ted 已提交
3290
web组件资源管理错误信息对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
3291 3292 3293 3294 3295 3296 3297

### getErrorCode

getErrorCode(): number

获取加载资源的错误码。

3298
**返回值:**
L
laosan_ted 已提交
3299

3300 3301 3302
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回加载资源的错误码。 |
Z
zhou-liting125 已提交
3303 3304 3305 3306 3307 3308 3309

### getErrorInfo

getErrorInfo(): string

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

3310
**返回值:**
L
laosan_ted 已提交
3311

3312 3313 3314
| 类型     | 说明           |
| ------ | ------------ |
| string | 返回加载资源的错误信息。 |
Z
zhou-liting125 已提交
3315 3316 3317

## WebResourceRequest

L
update  
laosan_ted 已提交
3318
web组件获取资源请求对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
3319 3320 3321 3322 3323 3324 3325

### getRequestHeader

getResponseHeader() : Array\<Header\>

获取资源请求头信息。

3326
**返回值:**
L
laosan_ted 已提交
3327

3328 3329 3330
| 类型                         | 说明         |
| -------------------------- | ---------- |
| Array\<[Header](#header)\> | 返回资源请求头信息。 |
Z
zhou-liting125 已提交
3331 3332 3333 3334 3335 3336 3337

### getRequestUrl

getRequestUrl(): string

获取资源请求的URL信息。

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

3340 3341 3342
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源请求的URL信息。 |
Z
zhou-liting125 已提交
3343 3344 3345 3346 3347 3348 3349

### isMainFrame

isMainFrame(): boolean

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

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

3352 3353 3354
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否为主frame。 |
Z
zhou-liting125 已提交
3355 3356 3357 3358 3359 3360 3361

### isRedirect

isRedirect(): boolean

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

3362
**返回值:**
L
laosan_ted 已提交
3363

3364 3365 3366
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否被服务端重定向。 |
Z
zhou-liting125 已提交
3367 3368 3369 3370 3371 3372 3373

### isRequestGesture

isRequestGesture(): boolean

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

3374
**返回值:**
L
laosan_ted 已提交
3375

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

3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
### getRequestMethod<sup>9+</sup>

getRequestMethod(): string

获取请求方法。

**返回值:**

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

Z
zhou-liting125 已提交
3392
## Header
L
liwenzhen 已提交
3393 3394 3395

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

3396 3397 3398 3399
| 名称          | 类型     | 描述            |
| ----------- | ------ | ------------- |
| headerKey   | string | 请求/响应头的key。   |
| headerValue | string | 请求/响应头的value。 |
L
liwenzhen 已提交
3400

L
add web  
liujinwei 已提交
3401

Z
zhou-liting125 已提交
3402
## WebResourceResponse
L
add web  
liujinwei 已提交
3403

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

Z
zhou-liting125 已提交
3406
### getReasonMessage
T
Ted 已提交
3407

Z
zhou-liting125 已提交
3408
getReasonMessage(): string
T
Ted 已提交
3409

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

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

3414 3415 3416
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源响应的状态码描述。 |
T
Ted 已提交
3417

Z
zhou-liting125 已提交
3418
### getResponseCode
T
Ted 已提交
3419

Z
zhou-liting125 已提交
3420 3421 3422 3423
getResponseCode(): number

获取资源响应的状态码。

3424
**返回值:**
L
laosan_ted 已提交
3425

3426 3427 3428
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回资源响应的状态码。 |
Z
zhou-liting125 已提交
3429 3430 3431 3432 3433 3434 3435

### getResponseData

getResponseData(): string

获取资源响应数据。

3436
**返回值:**
L
laosan_ted 已提交
3437

3438 3439 3440
| 类型     | 说明        |
| ------ | --------- |
| string | 返回资源响应数据。 |
Z
zhou-liting125 已提交
3441 3442 3443 3444 3445 3446 3447

### getResponseEncoding

getResponseEncoding(): string

获取资源响应的编码。

3448
**返回值:**
L
laosan_ted 已提交
3449

3450 3451 3452
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回资源响应的编码。 |
Z
zhou-liting125 已提交
3453 3454 3455 3456 3457 3458 3459

### getResponseHeader

getResponseHeader() : Array\<Header\>

获取资源响应头。

3460
**返回值:**
L
laosan_ted 已提交
3461

3462 3463 3464
| 类型                         | 说明       |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | 返回资源响应头。 |
Z
zhou-liting125 已提交
3465 3466 3467 3468 3469 3470 3471

### getResponseMimeType

getResponseMimeType(): string

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

3472
**返回值:**
L
laosan_ted 已提交
3473

3474 3475 3476
| 类型     | 说明                 |
| ------ | ------------------ |
| string | 返回资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3477 3478 3479

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

3480
setResponseData(data: string | number)
Z
zhou-liting125 已提交
3481 3482 3483

设置资源响应数据。

Z
zhou-liting125 已提交
3484
**参数:**
L
laosan_ted 已提交
3485

3486 3487 3488
| 参数名 | 参数类型         | 必填 | 默认值 | 参数描述                                                     |
| ------ | ---------------- | ---- | ------ | ------------------------------------------------------------ |
| data   | string \| number | 是   | -      | 要设置的资源响应数据。string表示输入类型是字符串,number表示输入类型是文件句柄。 |
Z
zhou-liting125 已提交
3489 3490 3491 3492 3493 3494 3495

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

setResponseEncoding(encoding: string)

设置资源响应的编码。

Z
zhou-liting125 已提交
3496
**参数:**
L
laosan_ted 已提交
3497

3498 3499 3500
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述         |
| -------- | ------ | ---- | ---- | ------------ |
| encoding | string | 是    | -    | 要设置的资源响应的编码。 |
Z
zhou-liting125 已提交
3501 3502 3503 3504 3505 3506 3507

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

setResponseMimeType(mimeType: string)

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

Z
zhou-liting125 已提交
3508
**参数:**
L
laosan_ted 已提交
3509

3510 3511 3512
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述                 |
| -------- | ------ | ---- | ---- | -------------------- |
| mimeType | string | 是    | -    | 要设置的资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
3513 3514 3515 3516 3517 3518 3519

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

setReasonMessage(reason: string)

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

Z
zhou-liting125 已提交
3520
**参数:**
L
laosan_ted 已提交
3521

3522 3523 3524
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------ | ------ | ---- | ---- | --------------- |
| reason | string | 是    | -    | 要设置的资源响应的状态码描述。 |
Z
zhou-liting125 已提交
3525 3526 3527 3528 3529 3530 3531

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

setResponseHeader(header: Array\<Header\>)

设置资源响应头。

Z
zhou-liting125 已提交
3532
**参数:**
L
laosan_ted 已提交
3533

3534 3535 3536
| 参数名    | 参数类型                       | 必填   | 默认值  | 参数描述       |
| ------ | -------------------------- | ---- | ---- | ---------- |
| header | Array\<[Header](#header)\> | 是    | -    | 要设置的资源响应头。 |
Z
zhou-liting125 已提交
3537 3538 3539 3540 3541 3542 3543

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

setResponseCode(code: number)

设置资源响应的状态码。

Z
zhou-liting125 已提交
3544
**参数:**
L
laosan_ted 已提交
3545

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

3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
### setResponseIsReady<sup>9+</sup>

setResponseIsReady(IsReady: boolean)

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

**参数:**

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

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

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

Z
zhou-liting125 已提交
3566
### handleFileList<sup>9+</sup>
3567

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

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

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

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

Z
zhou-liting125 已提交
3578 3579
## FileSelectorParam<sup>9+</sup>

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

L
lixiang 已提交
3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593
### getTitle<sup>9+</sup>

getTitle(): string

获取文件选择器标题。

**返回值:**

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

Z
zhou-liting125 已提交
3594 3595 3596 3597 3598 3599
### getMode<sup>9+</sup>

getMode(): FileSelectorMode

获取文件选择器的模式。

3600
**返回值:**
L
laosan_ted 已提交
3601

3602 3603 3604
| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode枚举说明) | 返回文件选择器的模式。 |
Z
zhou-liting125 已提交
3605 3606 3607 3608 3609 3610 3611

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

getAcceptType(): Array\<string\>

获取文件过滤类型。

3612
**返回值:**
L
laosan_ted 已提交
3613

3614 3615 3616
| 类型              | 说明        |
| --------------- | --------- |
| Array\<string\> | 返回文件过滤类型。 |
Z
zhou-liting125 已提交
3617 3618 3619 3620 3621 3622 3623

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

isCapture(): boolean

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

3624
**返回值:**
L
laosan_ted 已提交
3625

3626 3627 3628
| 类型      | 说明           |
| ------- | ------------ |
| boolean | 返回是否调用多媒体能力。 |
3629 3630 3631

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

Z
zhou-liting125 已提交
3632
Web组件返回的http auth认证请求确认或取消和使用缓存密码认证功能对象。示例代码参考[onHttpAuthRequest事件](#onhttpauthrequest9)
3633 3634 3635 3636 3637 3638 3639 3640

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

cancel(): void

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

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

3642 3643 3644 3645
confirm(userName: string, pwd: string): boolean

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

Z
zhou-liting125 已提交
3646
**参数:**
3647

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

Z
zhou-liting125 已提交
3653
**返回值:**
L
laosan_ted 已提交
3654

3655 3656 3657
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 认证成功返回true,失败返回false。 |
3658 3659 3660 3661 3662

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

isHttpAuthInfoSaved(): boolean

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

Z
zhou-liting125 已提交
3665
**返回值:**
L
laosan_ted 已提交
3666

3667 3668 3669
| 类型      | 说明                        |
| ------- | ------------------------- |
| boolean | 存在密码认证成功返回true,其他返回false。 |
3670

3671 3672
## SslErrorHandler<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3673
Web组件返回的SSL错误通知事件用户处理功能对象。示例代码参考[onSslErrorEventReceive事件](#onsslerroreventreceive9)
3674 3675 3676 3677 3678

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

handleCancel(): void

I
i-am-a-little-bird 已提交
3679
通知Web组件取消此请求。
3680 3681 3682 3683 3684

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

handleConfirm(): void

I
i-am-a-little-bird 已提交
3685 3686 3687 3688
通知Web组件继续使用SSL证书。

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

I
bugfix  
i-am-a-little-bird 已提交
3689
Web组件返回的SSL客户端证书请求事件用户处理功能对象。示例代码参考[onClientAuthenticationRequest事件](#onclientauthenticationrequest9)
I
i-am-a-little-bird 已提交
3690 3691 3692 3693 3694 3695

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

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

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

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

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

3704 3705 3706 3707
### confirm<sup>10+</sup>

confirm(authUri : string): void

3708 3709
**需要权限:** ohos.permission.ACCESS_CERT_MANAGER

3710 3711 3712 3713 3714 3715 3716 3717
通知Web组件使用指定的凭据(从证书管理模块获得)。

**参数:**

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

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

I
i-am-a-little-bird 已提交
3720 3721
cancel(): void

I
bugfix  
i-am-a-little-bird 已提交
3722
通知Web组件取消相同host和port服务器发送的客户端证书请求事件。同时,相同host和port服务器的请求,不重复上报该事件。
I
i-am-a-little-bird 已提交
3723 3724 3725 3726 3727 3728

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

ignore(): void

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

3730 3731
## PermissionRequest<sup>9+</sup>

X
xiongjun_gitee 已提交
3732
Web组件返回授权或拒绝权限功能的对象。示例代码参考[onPermissionRequest事件](#onpermissionrequest9)
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747

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

deny(): void

拒绝网页所请求的权限。

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

getOrigin(): string

获取网页来源。

**返回值:**

H
HelloCrease 已提交
3748 3749 3750
| 类型     | 说明           |
| ------ | ------------ |
| string | 当前请求权限网页的来源。 |
3751 3752 3753 3754 3755

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

getAccessibleResource(): Array\<string\>

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

**返回值:**

H
HelloCrease 已提交
3760 3761
| 类型              | 说明            |
| --------------- | ------------- |
3762 3763 3764 3765 3766 3767
| Array\<string\> | 网页所请求的权限资源列表。 |

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

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

X
xiongjun_gitee 已提交
3768
对网页访问的给定权限进行授权。
3769 3770 3771

**参数:**

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

I
i-am-a-little-bird 已提交
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
## 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  | 支持全选。 |

3811 3812
## WebContextMenuParam<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3813
实现长按页面元素或鼠标右键弹出来的菜单信息。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
3814 3815 3816 3817 3818 3819 3820 3821 3822

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

x(): number

弹出菜单的x坐标。

**返回值:**

H
HelloCrease 已提交
3823 3824
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3825
| number | 显示正常返回非负整数,否则返回-1。 |
3826 3827 3828 3829 3830 3831 3832 3833 3834

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

y(): number

弹出菜单的y坐标。

**返回值:**

H
HelloCrease 已提交
3835 3836
| 类型     | 说明                 |
| ------ | ------------------ |
Y
yu-shihao4 已提交
3837
| number | 显示正常返回非负整数,否则返回-1。 |
3838 3839 3840 3841 3842

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

getLinkUrl(): string

Y
yu-shihao4 已提交
3843
获取链接地址。
3844 3845 3846

**返回值:**

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

3851
### getUnfilteredLinkUrl<sup>9+</sup>
3852

3853
getUnfilteredLinkUrl(): string
3854

Y
yu-shihao4 已提交
3855
获取链接地址。
3856 3857 3858

**返回值:**

H
HelloCrease 已提交
3859 3860
| 类型     | 说明                    |
| ------ | --------------------- |
Y
yu-shihao4 已提交
3861
| string | 如果长按位置是链接,返回原始的url链接。 |
3862 3863 3864 3865 3866 3867 3868 3869 3870

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

getSourceUrl(): string

获取sourceUrl链接。

**返回值:**

H
HelloCrease 已提交
3871 3872
| 类型     | 说明                       |
| ------ | ------------------------ |
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
| string | 如果选中的元素有src属性,返回src的url。 |

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

existsImageContents(): boolean

是否存在图像内容。

**返回值:**

H
HelloCrease 已提交
3883 3884
| 类型      | 说明                        |
| ------- | ------------------------- |
3885 3886
| boolean | 长按位置中有图片返回true,否则返回false。 |

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

I
bugfix  
i-am-a-little-bird 已提交
3889
getMediaType(): ContextMenuMediaType
I
i-am-a-little-bird 已提交
3890 3891 3892 3893 3894 3895 3896

获取网页元素媒体类型。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3901
getSelectionText(): string
I
i-am-a-little-bird 已提交
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912

获取选中文本。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3913
getSourceType(): ContextMenuSourceType
I
i-am-a-little-bird 已提交
3914 3915 3916 3917 3918 3919 3920

获取菜单事件来源。

**返回值:**

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

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

I
bugfix  
i-am-a-little-bird 已提交
3925
getInputFieldType(): ContextMenuInputFieldType
I
i-am-a-little-bird 已提交
3926 3927 3928 3929 3930 3931 3932

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

**返回值:**

| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
I
bugfix  
i-am-a-little-bird 已提交
3933
| [ContextMenuInputFieldType](#contextmenuinputfieldtype9枚举说明) | 输入框类型。 |
I
i-am-a-little-bird 已提交
3934 3935 3936

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

I
bugfix  
i-am-a-little-bird 已提交
3937
isEditable(): boolean
I
i-am-a-little-bird 已提交
3938 3939 3940 3941 3942 3943 3944

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

**返回值:**

| 类型      | 说明                        |
| ------- | ------------------------- |
I
bugfix  
i-am-a-little-bird 已提交
3945
| boolean | 网页元素可编辑返回true,不可编辑返回false。 |
I
i-am-a-little-bird 已提交
3946 3947 3948

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

I
bugfix  
i-am-a-little-bird 已提交
3949
getEditStateFlags(): number
I
i-am-a-little-bird 已提交
3950 3951 3952 3953 3954 3955 3956

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

**返回值:**

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

3959 3960
## WebContextMenuResult<sup>9+</sup>

I
bugfix  
i-am-a-little-bird 已提交
3961
实现长按页面元素或鼠标右键弹出来的菜单所执行的响应事件。示例代码参考[onContextMenuShow事件](#oncontextmenushow9)
3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974

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

closeContextMenu(): void

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

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

copyImage(): void

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

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

I
bugfix  
i-am-a-little-bird 已提交
3977
copy(): void
I
i-am-a-little-bird 已提交
3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998

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

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

paste(): void

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

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

cut(): void

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

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

selectAll(): void

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

3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010
## JsGeolocation

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

### invoke

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

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

**参数:**

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

L
lixiang 已提交
4017
## MessageLevel枚举说明
L
laosan_ted 已提交
4018

L
lixiang 已提交
4019 4020 4021 4022 4023 4024 4025
| 名称    | 描述    |
| ----- | :---- |
| Debug | 调试级别。 |
| Error | 错误级别。 |
| Info  | 消息级别。 |
| Log   | 日志级别。 |
| Warn  | 警告级别。 |
Z
zengyawen 已提交
4026

L
lixiang 已提交
4027
## RenderExitReason枚举说明
Z
zengyawen 已提交
4028

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

L
lixiang 已提交
4031 4032 4033 4034 4035 4036 4037
| 名称                         | 描述                |
| -------------------------- | ----------------- |
| ProcessAbnormalTermination | 渲染进程异常退出。         |
| ProcessWasKilled           | 收到SIGKILL,或被手动终止。 |
| ProcessCrashed             | 渲染进程崩溃退出,如段错误。    |
| ProcessOom                 | 程序内存不足。           |
| ProcessExitUnknown         | 其他原因。             |
L
laosan_ted 已提交
4038

L
lixiang 已提交
4039
## MixedMode枚举说明
L
laosan_ted 已提交
4040

L
lixiang 已提交
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 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 4139 4140 4141 4142 4143 4144 4145 4146 4147 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
| 名称         | 描述                                 |
| ---------- | ---------------------------------- |
| 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深色模式跟随系统。                 |

## 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管理对象。

**返回值:**

| 类型        | 说明                                       |
| --------- | ---------------------------------------- |
| WebCookie | web组件cookie管理对象,参考[WebCookie](#webcookie)定义。 |

**示例:**

  ```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 已提交
4210

L
laosan_ted 已提交
4211
**示例:**
L
laosan_ted 已提交
4212

L
laosan_ted 已提交
4213 4214 4215 4216 4217
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4218
    controller: WebController = new WebController()
4219

L
laosan_ted 已提交
4220 4221 4222 4223
    build() {
      Column() {
        Button('requestFocus')
          .onClick(() => {
Y
yamila 已提交
4224
            this.controller.requestFocus()
L
laosan_ted 已提交
4225 4226 4227 4228 4229 4230 4231
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

L
laosan_ted 已提交
4232
### accessBackward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4233 4234 4235

accessBackward(): boolean

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

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

Z
zhou-liting125 已提交
4240
**返回值:**
L
laosan_ted 已提交
4241

4242 4243 4244
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以后退返回true,否则返回false。 |
L
update  
laosan_ted 已提交
4245

Z
zhou-liting125 已提交
4246
**示例:**
L
laosan_ted 已提交
4247

Z
zhou-liting125 已提交
4248 4249
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4250
  @Entry
L
update  
laosan_ted 已提交
4251 4252
  @Component
  struct WebComponent {
Y
yamila 已提交
4253
    controller: WebController = new WebController()
4254

L
update  
laosan_ted 已提交
4255 4256 4257
    build() {
      Column() {
        Button('accessBackward')
L
laosan_ted 已提交
4258
          .onClick(() => {
Y
yamila 已提交
4259 4260
            let result = this.controller.accessBackward()
            console.log('result:' + result)
L
laosan_ted 已提交
4261
          })
Z
zhou-liting125 已提交
4262
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4263
      }
L
update  
laosan_ted 已提交
4264 4265 4266 4267
    }
  }
  ```

L
laosan_ted 已提交
4268
### accessForward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4269 4270 4271

accessForward(): boolean

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

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

Z
zhou-liting125 已提交
4276
**返回值:**
L
laosan_ted 已提交
4277

4278 4279 4280
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以前进返回true,否则返回false。 |
L
update  
laosan_ted 已提交
4281

Z
zhou-liting125 已提交
4282
**示例:**
L
laosan_ted 已提交
4283

Z
zhou-liting125 已提交
4284 4285
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4286
  @Entry
L
update  
laosan_ted 已提交
4287 4288
  @Component
  struct WebComponent {
Y
yamila 已提交
4289
    controller: WebController = new WebController()
4290

L
update  
laosan_ted 已提交
4291 4292 4293
    build() {
      Column() {
        Button('accessForward')
L
laosan_ted 已提交
4294
          .onClick(() => {
Y
yamila 已提交
4295 4296
            let result = this.controller.accessForward()
            console.log('result:' + result)
L
laosan_ted 已提交
4297
          })
Z
zhou-liting125 已提交
4298
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4299
      }
L
update  
laosan_ted 已提交
4300 4301 4302 4303
    }
  }
  ```

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

L
lixingchi1 已提交
4306
accessStep(step: number): boolean
Z
zengyawen 已提交
4307

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

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

Z
zhou-liting125 已提交
4312
**参数:**
Z
zengyawen 已提交
4313

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

Z
zhou-liting125 已提交
4318
**返回值:**
L
laosan_ted 已提交
4319

4320 4321 4322
| 类型      | 说明        |
| ------- | --------- |
| boolean | 页面是否前进或后退 |
4323

Z
zhou-liting125 已提交
4324
**示例:**
L
laosan_ted 已提交
4325

Z
zhou-liting125 已提交
4326 4327
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4328
  @Entry
L
update  
laosan_ted 已提交
4329 4330
  @Component
  struct WebComponent {
Y
yamila 已提交
4331 4332
    controller: WebController = new WebController()
    @State steps: number = 2
4333

L
update  
laosan_ted 已提交
4334 4335 4336
    build() {
      Column() {
        Button('accessStep')
L
laosan_ted 已提交
4337
          .onClick(() => {
Y
yamila 已提交
4338 4339
            let result = this.controller.accessStep(this.steps)
            console.log('result:' + result)
L
laosan_ted 已提交
4340
          })
Z
zhou-liting125 已提交
4341
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4342
      }
L
update  
laosan_ted 已提交
4343 4344 4345 4346
    }
  }
  ```

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

L
update  
laosan_ted 已提交
4349
backward(): void
L
lixingchi1 已提交
4350

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

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

Z
zhou-liting125 已提交
4355
**示例:**
L
laosan_ted 已提交
4356

Z
zhou-liting125 已提交
4357 4358
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4359
  @Entry
L
update  
laosan_ted 已提交
4360 4361
  @Component
  struct WebComponent {
Y
yamila 已提交
4362
    controller: WebController = new WebController()
4363

L
update  
laosan_ted 已提交
4364 4365 4366
    build() {
      Column() {
        Button('backward')
L
laosan_ted 已提交
4367
          .onClick(() => {
Y
yamila 已提交
4368
            this.controller.backward()
L
laosan_ted 已提交
4369
          })
Z
zhou-liting125 已提交
4370
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4371
      }
L
update  
laosan_ted 已提交
4372 4373 4374
    }
  }
  ```
L
lixingchi1 已提交
4375

L
laosan_ted 已提交
4376
### forward<sup>(deprecated)</sup>
L
lixingchi1 已提交
4377

L
update  
laosan_ted 已提交
4378
forward(): void
L
lixingchi1 已提交
4379

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

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

Z
zhou-liting125 已提交
4384
**示例:**
L
laosan_ted 已提交
4385

Z
zhou-liting125 已提交
4386 4387
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4388
  @Entry
L
update  
laosan_ted 已提交
4389 4390
  @Component
  struct WebComponent {
Y
yamila 已提交
4391
    controller: WebController = new WebController()
4392

L
update  
laosan_ted 已提交
4393 4394 4395
    build() {
      Column() {
        Button('forward')
L
laosan_ted 已提交
4396
          .onClick(() => {
Y
yamila 已提交
4397
            this.controller.forward()
L
laosan_ted 已提交
4398
          })
Z
zhou-liting125 已提交
4399
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4400
      }
L
update  
laosan_ted 已提交
4401 4402 4403 4404
    }
  }
  ```

L
laosan_ted 已提交
4405
### deleteJavaScriptRegister<sup>(deprecated)</sup>
L
update  
laosan_ted 已提交
4406 4407 4408

deleteJavaScriptRegister(name: string)

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

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

Z
zhou-liting125 已提交
4413
**参数:**
L
laosan_ted 已提交
4414

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

Z
zhou-liting125 已提交
4419
**示例:**
L
laosan_ted 已提交
4420

Z
zhou-liting125 已提交
4421 4422
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4423
  @Entry
L
update  
laosan_ted 已提交
4424 4425
  @Component
  struct WebComponent {
Y
yamila 已提交
4426 4427
    controller: WebController = new WebController()
    @State name: string = 'Object'
4428

L
update  
laosan_ted 已提交
4429 4430 4431
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
L
laosan_ted 已提交
4432
          .onClick(() => {
Y
yamila 已提交
4433
            this.controller.deleteJavaScriptRegister(this.name)
L
laosan_ted 已提交
4434
          })
Z
zhou-liting125 已提交
4435
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4436
      }
L
update  
laosan_ted 已提交
4437 4438 4439 4440
    }
  }
  ```

L
laosan_ted 已提交
4441
### getHitTest<sup>(deprecated)</sup>
L
lixingchi1 已提交
4442 4443 4444

getHitTest(): HitTestType

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

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

Z
zhou-liting125 已提交
4449
**返回值:**
L
laosan_ted 已提交
4450

4451 4452 4453
| 类型                              | 说明          |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype枚举说明) | 被点击区域的元素类型。 |
L
lixingchi1 已提交
4454

Z
zhou-liting125 已提交
4455
**示例:**
L
laosan_ted 已提交
4456

Z
zhou-liting125 已提交
4457 4458
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4459
  @Entry
L
update  
laosan_ted 已提交
4460 4461
  @Component
  struct WebComponent {
Y
yamila 已提交
4462
    controller: WebController = new WebController()
4463

L
update  
laosan_ted 已提交
4464 4465 4466
    build() {
      Column() {
        Button('getHitTest')
L
laosan_ted 已提交
4467
          .onClick(() => {
Y
yamila 已提交
4468 4469
            let hitType = this.controller.getHitTest()
            console.log("hitType: " + hitType)
L
laosan_ted 已提交
4470
          })
Z
zhou-liting125 已提交
4471
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4472
      }
L
update  
laosan_ted 已提交
4473 4474 4475 4476
    }
  }
  ```

L
laosan_ted 已提交
4477
### loadData<sup>(deprecated)</sup>
L
lixingchi1 已提交
4478

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

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

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

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

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

Z
zhou-liting125 已提交
4489
**参数:**
L
laosan_ted 已提交
4490

4491 4492 4493 4494 4495 4496 4497
| 参数名        | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---------- | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
4498

Z
zhou-liting125 已提交
4499
**示例:**
L
laosan_ted 已提交
4500

Z
zhou-liting125 已提交
4501 4502
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4503
  @Entry
L
update  
laosan_ted 已提交
4504 4505
  @Component
  struct WebComponent {
Y
yamila 已提交
4506
    controller: WebController = new WebController()
4507

L
update  
laosan_ted 已提交
4508 4509 4510
    build() {
      Column() {
        Button('loadData')
L
laosan_ted 已提交
4511 4512 4513 4514 4515
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
Y
yamila 已提交
4516
            })
L
laosan_ted 已提交
4517
          })
Z
zhou-liting125 已提交
4518
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4519
      }
L
update  
laosan_ted 已提交
4520 4521 4522 4523
    }
  }
  ```

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

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

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

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

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

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

Z
zhou-liting125 已提交
4536
**参数:**
L
laosan_ted 已提交
4537

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

Z
zhou-liting125 已提交
4543
**示例:**
L
laosan_ted 已提交
4544

Z
zhou-liting125 已提交
4545 4546
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4547
  @Entry
L
update  
laosan_ted 已提交
4548 4549
  @Component
  struct WebComponent {
Y
yamila 已提交
4550
    controller: WebController = new WebController()
4551

L
update  
laosan_ted 已提交
4552 4553 4554
    build() {
      Column() {
        Button('loadUrl')
L
laosan_ted 已提交
4555
          .onClick(() => {
Y
yamila 已提交
4556
            this.controller.loadUrl({ url: 'www.example.com' })
L
laosan_ted 已提交
4557
          })
Z
zhou-liting125 已提交
4558
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4559
      }
L
update  
laosan_ted 已提交
4560 4561 4562 4563
    }
  }
  ```

L
laosan_ted 已提交
4564
### onActive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4565 4566 4567

onActive(): void

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

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

Z
zhou-liting125 已提交
4572
**示例:**
L
laosan_ted 已提交
4573

Z
zhou-liting125 已提交
4574 4575
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4576
  @Entry
L
update  
laosan_ted 已提交
4577 4578
  @Component
  struct WebComponent {
Y
yamila 已提交
4579
    controller: WebController = new WebController()
4580

L
update  
laosan_ted 已提交
4581 4582 4583
    build() {
      Column() {
        Button('onActive')
L
laosan_ted 已提交
4584
          .onClick(() => {
Y
yamila 已提交
4585
            this.controller.onActive()
L
laosan_ted 已提交
4586
          })
Z
zhou-liting125 已提交
4587
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4588
      }
L
update  
laosan_ted 已提交
4589 4590 4591 4592
    }
  }
  ```

L
laosan_ted 已提交
4593
### onInactive<sup>(deprecated)</sup>
L
lixingchi1 已提交
4594 4595 4596

onInactive(): void

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

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

Z
zhou-liting125 已提交
4601
**示例:**
L
laosan_ted 已提交
4602

Z
zhou-liting125 已提交
4603 4604
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4605
  @Entry
L
update  
laosan_ted 已提交
4606 4607
  @Component
  struct WebComponent {
Y
yamila 已提交
4608
    controller: WebController = new WebController()
4609

L
update  
laosan_ted 已提交
4610 4611 4612
    build() {
      Column() {
        Button('onInactive')
L
laosan_ted 已提交
4613
          .onClick(() => {
Y
yamila 已提交
4614
            this.controller.onInactive()
L
laosan_ted 已提交
4615
          })
Z
zhou-liting125 已提交
4616
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4617
      }
L
update  
laosan_ted 已提交
4618 4619 4620 4621
    }
  }
  ```

L
laosan_ted 已提交
4622
### zoom<sup>(deprecated)</sup>
4623 4624 4625
zoom(factor: number): void

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

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

Z
zhou-liting125 已提交
4629
**参数:**
L
laosan_ted 已提交
4630

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

Z
zhou-liting125 已提交
4635
**示例:**
L
laosan_ted 已提交
4636

Z
zhou-liting125 已提交
4637 4638
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4639
  @Entry
L
update  
laosan_ted 已提交
4640 4641
  @Component
  struct WebComponent {
Y
yamila 已提交
4642 4643
    controller: WebController = new WebController()
    @State factor: number = 1
4644

L
update  
laosan_ted 已提交
4645 4646 4647
    build() {
      Column() {
        Button('zoom')
L
laosan_ted 已提交
4648
          .onClick(() => {
Y
yamila 已提交
4649
            this.controller.zoom(this.factor)
L
laosan_ted 已提交
4650
          })
Z
zhou-liting125 已提交
4651
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4652
      }
L
update  
laosan_ted 已提交
4653 4654 4655 4656
    }
  }
  ```

L
laosan_ted 已提交
4657
### refresh<sup>(deprecated)</sup>
L
lixingchi1 已提交
4658

Z
zhou-liting125 已提交
4659
refresh()
L
lixingchi1 已提交
4660

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

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

Z
zhou-liting125 已提交
4665
**示例:**
L
laosan_ted 已提交
4666

Z
zhou-liting125 已提交
4667 4668
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4669
  @Entry
L
update  
laosan_ted 已提交
4670 4671
  @Component
  struct WebComponent {
Y
yamila 已提交
4672
    controller: WebController = new WebController()
4673

L
update  
laosan_ted 已提交
4674 4675 4676
    build() {
      Column() {
        Button('refresh')
L
laosan_ted 已提交
4677
          .onClick(() => {
Y
yamila 已提交
4678
            this.controller.refresh()
L
laosan_ted 已提交
4679
          })
Z
zhou-liting125 已提交
4680
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4681
      }
L
update  
laosan_ted 已提交
4682 4683 4684 4685
    }
  }
  ```

L
laosan_ted 已提交
4686
### registerJavaScriptProxy<sup>(deprecated)</sup>
L
lixingchi1 已提交
4687

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

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

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

Z
zhou-liting125 已提交
4694
**参数:**
L
laosan_ted 已提交
4695

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

Z
zhou-liting125 已提交
4702
**示例:**
L
laosan_ted 已提交
4703

Z
zhou-liting125 已提交
4704 4705
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4706
  @Entry
L
update  
laosan_ted 已提交
4707 4708 4709 4710 4711
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
Y
yamila 已提交
4712
        return "ArkUI Web Component"
L
update  
laosan_ted 已提交
4713 4714
      },
      toString: () => {
Y
yamila 已提交
4715
        console.log('Web Component toString')
L
update  
laosan_ted 已提交
4716 4717 4718 4719 4720 4721 4722 4723 4724 4725
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
Y
yamila 已提交
4726
            })
L
update  
laosan_ted 已提交
4727 4728 4729 4730 4731 4732 4733 4734
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```
4735

L
update  
laosan_ted 已提交
4736
  ```html
L
laosan_ted 已提交
4737
  <!-- index.html -->
L
update  
laosan_ted 已提交
4738 4739 4740 4741 4742 4743 4744 4745
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
Y
yamila 已提交
4746 4747
          str = objName.test("test function")
          console.log('objName.test result:'+ str)
L
update  
laosan_ted 已提交
4748 4749 4750
      }
  </script>
  </html>
4751

L
update  
laosan_ted 已提交
4752 4753
  ```

L
laosan_ted 已提交
4754
### runJavaScript<sup>(deprecated)</sup>
L
lixingchi1 已提交
4755

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

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

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

Z
zhou-liting125 已提交
4762
**参数:**
L
laosan_ted 已提交
4763

4764 4765 4766 4767
| 参数名      | 参数类型                     | 必填   | 默认值  | 参数描述                                     |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | 是    | -    | JavaScript脚本。                            |
| callback | (result: string) => void | 否    | -    | 回调执行JavaScript脚本结果。JavaScript脚本若执行失败或无返回值时,返回null。 |
L
lixingchi1 已提交
4768

Z
zhou-liting125 已提交
4769
**示例:**
L
laosan_ted 已提交
4770

Z
zhou-liting125 已提交
4771 4772
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4773
  @Entry
L
update  
laosan_ted 已提交
4774 4775
  @Component
  struct WebComponent {
Y
yamila 已提交
4776
    controller: WebController = new WebController()
L
update  
laosan_ted 已提交
4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788
    @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 已提交
4789 4790
            }})
          console.info('url: ', e.url)
L
update  
laosan_ted 已提交
4791 4792 4793 4794 4795 4796 4797
        })
      }
    }
  }
  ```

  ```html
L
laosan_ted 已提交
4798
  <!-- index.html -->
L
update  
laosan_ted 已提交
4799 4800 4801 4802 4803 4804 4805 4806
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
Y
yamila 已提交
4807
        console.log('Ark WebComponent')
L
update  
laosan_ted 已提交
4808 4809 4810 4811 4812 4813 4814
        return "This value is from index.html"
    }
    </script>
  </html>

  ```

L
laosan_ted 已提交
4815
### stop<sup>(deprecated)</sup>
L
lixingchi1 已提交
4816

Z
zhou-liting125 已提交
4817
stop()
Z
zengyawen 已提交
4818

L
lixingchi1 已提交
4819
停止页面加载。
Z
zengyawen 已提交
4820

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

Z
zhou-liting125 已提交
4823
**示例:**
L
laosan_ted 已提交
4824

Z
zhou-liting125 已提交
4825 4826
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4827
  @Entry
L
update  
laosan_ted 已提交
4828 4829
  @Component
  struct WebComponent {
Y
yamila 已提交
4830
    controller: WebController = new WebController()
4831

L
update  
laosan_ted 已提交
4832 4833 4834
    build() {
      Column() {
        Button('stop')
L
laosan_ted 已提交
4835
          .onClick(() => {
Y
yamila 已提交
4836
            this.controller.stop()
L
laosan_ted 已提交
4837
          })
Z
zhou-liting125 已提交
4838
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4839
      }
L
update  
laosan_ted 已提交
4840 4841 4842 4843
    }
  }
  ```

L
laosan_ted 已提交
4844
### clearHistory<sup>(deprecated)</sup>
T
Ted 已提交
4845 4846 4847 4848 4849

clearHistory(): void

删除所有前进后退记录。

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

Z
zhou-liting125 已提交
4852
**示例:**
L
laosan_ted 已提交
4853

Z
zhou-liting125 已提交
4854 4855
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4856
  @Entry
L
update  
laosan_ted 已提交
4857 4858
  @Component
  struct WebComponent {
Y
yamila 已提交
4859
    controller: WebController = new WebController()
4860

L
update  
laosan_ted 已提交
4861 4862 4863
    build() {
      Column() {
        Button('clearHistory')
L
laosan_ted 已提交
4864
          .onClick(() => {
Y
yamila 已提交
4865
            this.controller.clearHistory()
L
laosan_ted 已提交
4866
          })
Z
zhou-liting125 已提交
4867
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4868
      }
L
update  
laosan_ted 已提交
4869 4870 4871 4872
    }
  }
  ```

L
lixiang 已提交
4873
## WebCookie<sup>(deprecated)</sup>
I
i-am-a-little-bird 已提交
4874

4875
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookie。通过controller方法中的getCookieManager方法可以获取WebCookie对象,进行后续的cookie管理操作。
I
i-am-a-little-bird 已提交
4876

L
lixiang 已提交
4877
### setCookie<sup>(deprecated)</sup>
4878
setCookie(url: string, value: string): boolean
I
i-am-a-little-bird 已提交
4879

4880
设置cookie,该方法为同步方法。设置成功返回true,否则返回false。
L
lixiang 已提交
4881
从API version 9开始不在维护,建议使用[setCookie<sup>9+</sup>](../apis/js-apis-webview.md#setcookie)代替。
T
Ted 已提交
4882

4883
**参数:**
T
Ted 已提交
4884

4885 4886
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
4887
| url   | string | 是    | -    | 要设置的cookie所属的url,建议使用完整的url。 |
4888
| value | string | 是    | -    | cookie的值。         |
Z
zhou-liting125 已提交
4889 4890

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

4892 4893 4894
| 类型      | 说明            |
| ------- | ------------- |
| boolean | 设置cookie是否成功。 |
4895

Z
zhou-liting125 已提交
4896
**示例:**
L
laosan_ted 已提交
4897

Z
zhou-liting125 已提交
4898 4899
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
4900
  @Entry
L
update  
laosan_ted 已提交
4901 4902
  @Component
  struct WebComponent {
Y
yamila 已提交
4903
    controller: WebController = new WebController()
4904

L
update  
laosan_ted 已提交
4905 4906
    build() {
      Column() {
4907
        Button('setCookie')
L
laosan_ted 已提交
4908
          .onClick(() => {
L
lixiang 已提交
4909
            let result = this.controller.getCookieManager().setCookie("https://www.example.com", "a=b")
4910
            console.log("result: " + result)
L
laosan_ted 已提交
4911
          })
Z
zhou-liting125 已提交
4912
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
4913
      }
L
update  
laosan_ted 已提交
4914 4915 4916
    }
  }
  ```
L
lixiang 已提交
4917 4918
### saveCookie<sup>(deprecated)</sup>
saveCookie(): boolean
L
update  
laosan_ted 已提交
4919

4920
将当前存在内存中的cookie同步到磁盘中,该方法为同步方法。
L
lixiang 已提交
4921
从API version 9开始不在维护,建议使用[saveCookieAsync<sup>9+</sup>](../apis/js-apis-webview.md#savecookieasync)代替。
4922 4923

**返回值:**
4924

4925 4926 4927
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 同步内存cookie到磁盘操作是否成功。 |
4928 4929

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

4931 4932 4933 4934 4935
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
Y
yamila 已提交
4936
    controller: WebController = new WebController()
4937

4938 4939
    build() {
      Column() {
L
lixiang 已提交
4940
        Button('saveCookie')
4941
          .onClick(() => {
L
lixiang 已提交
4942
            let result = this.controller.getCookieManager().saveCookie()
4943
            console.log("result: " + result)
4944 4945 4946 4947 4948
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
C
chensi10 已提交
4949
  ```