• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebController, deprecated)
2
3Implements a **WebController** to control the behavior of the **Web** component. A **WebController** can control only one **Web** component, and the APIs in the **WebController** can be invoked only after it has been bound to the target **Web** component.
4
5This API is deprecated since API version 9. You are advised to use [WebviewController<sup>9+</sup>](./arkts-apis-webview-WebviewController.md) instead.
6
7> **NOTE**
8>
9> - The initial APIs of this component are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
10>
11> - The initial APIs of this class are supported since API version 8.
12>
13> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
14
15## Creating an Object
16
17<!--deprecated_code_no_check-->
18```ts
19let webController: WebController = new WebController()
20```
21
22## constructor<sup>(deprecated)</sup>
23
24constructor()
25
26Constructs a **WebController** object.
27
28> **NOTE**
29>
30> This API is supported since API version 8 and deprecated since API version 9. No API is provided for substitute.
31
32**System capability**: SystemCapability.Web.Webview.Core
33
34## getCookieManager<sup>(deprecated)</sup>
35
36getCookieManager(): WebCookie
37
38Obtains the cookie management object of the **Web** component.
39
40This API is deprecated since API version 9. You are advised to use [getCookie](./arkts-apis-webview-WebCookieManager.md#getcookiedeprecated) instead.
41
42**System capability**: SystemCapability.Web.Webview.Core
43
44**Return value**
45
46| Type       | Description                                      |
47| --------- | ---------------------------------------- |
48| WebCookie | Cookie management object of the **Web** component. For details, see [WebCookie](./arkts-basic-components-web-WebCookie.md).|
49
50**Example**
51
52  ```ts
53  // xxx.ets
54  @Entry
55  @Component
56  struct WebComponent {
57    controller: WebController = new WebController()
58
59    build() {
60      Column() {
61        Button('getCookieManager')
62          .onClick(() => {
63            let cookieManager = this.controller.getCookieManager()
64          })
65        Web({ src: 'www.example.com', controller: this.controller })
66      }
67    }
68  }
69  ```
70
71## requestFocus<sup>(deprecated)</sup>
72
73requestFocus()
74
75Requests focus for this web page.
76
77This API is deprecated since API version 9. You are advised to use [requestFocus<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#requestfocus) instead.
78
79**System capability**: SystemCapability.Web.Webview.Core
80
81**Example**
82
83  ```ts
84  // xxx.ets
85  @Entry
86  @Component
87  struct WebComponent {
88    controller: WebController = new WebController()
89
90    build() {
91      Column() {
92        Button('requestFocus')
93          .onClick(() => {
94            this.controller.requestFocus()
95          })
96        Web({ src: 'www.example.com', controller: this.controller })
97      }
98    }
99  }
100  ```
101
102## accessBackward<sup>(deprecated)</sup>
103
104accessBackward(): boolean
105
106Checks whether going to the previous page can be performed on the current page.
107
108This API is deprecated since API version 9. You are advised to use [accessBackward<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#accessbackward) instead.
109
110**System capability**: SystemCapability.Web.Webview.Core
111
112**Return value**
113
114| Type     | Description                   |
115| ------- | --------------------- |
116| boolean | **true** is returned if going to the previous page can be performed on the current page; otherwise, **false** is returned.|
117
118**Example**
119
120  ```ts
121  // xxx.ets
122  @Entry
123  @Component
124  struct WebComponent {
125    controller: WebController = new WebController()
126
127    build() {
128      Column() {
129        Button('accessBackward')
130          .onClick(() => {
131            let result = this.controller.accessBackward()
132            console.log('result:' + result)
133          })
134        Web({ src: 'www.example.com', controller: this.controller })
135      }
136    }
137  }
138  ```
139
140## accessForward<sup>(deprecated)</sup>
141
142accessForward(): boolean
143
144Checks whether going to the next page can be performed on the current page.
145
146This API is deprecated since API version 9. You are advised to use [accessForward<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#accessforward) instead.
147
148**System capability**: SystemCapability.Web.Webview.Core
149
150**Return value**
151
152| Type     | Description                   |
153| ------- | --------------------- |
154| boolean | If going to the next page can be performed on the current page, **true** is returned; otherwise, **false** is returned.|
155
156**Example**
157
158  ```ts
159  // xxx.ets
160  @Entry
161  @Component
162  struct WebComponent {
163    controller: WebController = new WebController()
164
165    build() {
166      Column() {
167        Button('accessForward')
168          .onClick(() => {
169            let result = this.controller.accessForward()
170            console.log('result:' + result)
171          })
172        Web({ src: 'www.example.com', controller: this.controller })
173      }
174    }
175  }
176  ```
177
178## accessStep<sup>(deprecated)</sup>
179
180accessStep(step: number): boolean
181
182Performs a specific number of steps forward or backward from the current page.
183
184This API is deprecated since API version 9. You are advised to use [accessStep<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#accessstep) instead.
185
186**System capability**: SystemCapability.Web.Webview.Core
187
188**Parameters**
189
190| Name | Type  | Mandatory | Description                 |
191| ---- | ------ | ---- | --------------------- |
192| step | number | Yes  | Number of the steps to take. A positive number means to go forward, and a negative number means to go backward.|
193
194**Return value**
195
196| Type     | Description       |
197| ------- | --------- |
198| boolean | Whether going forward or backward from the current page is successful.|
199
200**Example**
201
202  ```ts
203  // xxx.ets
204  @Entry
205  @Component
206  struct WebComponent {
207    controller: WebController = new WebController()
208    @State steps: number = 2
209
210    build() {
211      Column() {
212        Button('accessStep')
213          .onClick(() => {
214            let result = this.controller.accessStep(this.steps)
215            console.log('result:' + result)
216          })
217        Web({ src: 'www.example.com', controller: this.controller })
218      }
219    }
220  }
221  ```
222
223## backward<sup>(deprecated)</sup>
224
225backward()
226
227Goes to the previous page based on the history stack. This API is generally used together with **accessBackward**.
228
229This API is deprecated since API version 9. You are advised to use [backward<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#backward) instead.
230
231**System capability**: SystemCapability.Web.Webview.Core
232
233**Example**
234
235  ```ts
236  // xxx.ets
237  @Entry
238  @Component
239  struct WebComponent {
240    controller: WebController = new WebController()
241
242    build() {
243      Column() {
244        Button('backward')
245          .onClick(() => {
246            this.controller.backward()
247          })
248        Web({ src: 'www.example.com', controller: this.controller })
249      }
250    }
251  }
252  ```
253
254## forward<sup>(deprecated)</sup>
255
256forward()
257
258Goes to the next page based on the history stack. This API is generally used together with **accessForward**.
259
260This API is deprecated since API version 9. You are advised to use [forward<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#forward) instead.
261
262**System capability**: SystemCapability.Web.Webview.Core
263
264**Example**
265
266  ```ts
267  // xxx.ets
268  @Entry
269  @Component
270  struct WebComponent {
271    controller: WebController = new WebController()
272
273    build() {
274      Column() {
275        Button('forward')
276          .onClick(() => {
277            this.controller.forward()
278          })
279        Web({ src: 'www.example.com', controller: this.controller })
280      }
281    }
282  }
283  ```
284
285## deleteJavaScriptRegister<sup>(deprecated)</sup>
286
287deleteJavaScriptRegister(name: string)
288
289Deletes a specific application JavaScript object that is registered with the window through **registerJavaScriptProxy**. The deletion takes effect immediately, with no need for invoking the [refresh](#refreshdeprecated) API.
290
291This API is deprecated since API version 9. You are advised to use [deleteJavaScriptRegister<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#deletejavascriptregister) instead.
292
293**System capability**: SystemCapability.Web.Webview.Core
294
295**Parameters**
296
297| Name | Type  | Mandatory | Description                                    |
298| ---- | ------ | ---- | ---------------------------------------- |
299| name | string | Yes  | Name of the registered JavaScript object, which can be used to invoke the corresponding object on the application side from the web side.|
300
301**Example**
302
303  ```ts
304  // xxx.ets
305  @Entry
306  @Component
307  struct WebComponent {
308    controller: WebController = new WebController()
309    @State name: string = 'Object'
310
311    build() {
312      Column() {
313        Button('deleteJavaScriptRegister')
314          .onClick(() => {
315            this.controller.deleteJavaScriptRegister(this.name)
316          })
317        Web({ src: 'www.example.com', controller: this.controller })
318      }
319    }
320  }
321  ```
322
323## getHitTest<sup>(deprecated)</sup>
324
325getHitTest(): HitTestType
326
327Obtains the element type of the area being clicked.
328
329This API is deprecated since API version 9. You are advised to use [getHitTest<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#gethittestdeprecated) instead.
330
331**System capability**: SystemCapability.Web.Webview.Core
332
333**Return value**
334
335| Type                             | Description         |
336| ------------------------------- | ----------- |
337| [HitTestType](./arkts-basic-components-web-e.md#hittesttype) | Element type of the area being clicked.|
338
339**Example**
340
341  ```ts
342  // xxx.ets
343  @Entry
344  @Component
345  struct WebComponent {
346    controller: WebController = new WebController()
347
348    build() {
349      Column() {
350        Button('getHitTest')
351          .onClick(() => {
352            let hitType = this.controller.getHitTest()
353            console.log("hitType: " + hitType)
354          })
355        Web({ src: 'www.example.com', controller: this.controller })
356      }
357    }
358  }
359  ```
360
361## loadData<sup>(deprecated)</sup>
362
363loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string })
364
365Loads data. If **baseUrl** is empty, the specified character string will be loaded using the data protocol.
366
367If **baseUrl** is set to a data URL, the encoded string will be loaded by the **Web** component using the data protocol.
368
369If **baseUrl** is set to an HTTP or HTTPS URL, the encoded string will be processed by the **Web** component as a non-encoded string in a manner similar to **loadUrl**.
370
371This API is deprecated since API version 9. You are advised to use [loadData<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#loaddata) instead.
372
373**System capability**: SystemCapability.Web.Webview.Core
374
375**Parameters**
376
377| Name       | Type  | Mandatory  | Description                                    |
378| ---------- | ------ | ---- | ---------------------------------------- |
379| data       | string | Yes  | Character string obtained after being Base64 or URL encoded.             |
380| mimeType   | string | Yes  | Media type (MIME).                             |
381| encoding   | string | Yes  | Encoding type, which can be Base64 or URL.               |
382| baseUrl    | string | No  | URL (HTTP/HTTPS/data compliant), which is assigned by the **Web** component to **window.origin**.|
383| historyUrl | string | No  | Historical record URL. If this parameter is not empty, it can be managed in historical records to implement page going backward and forward. This parameter is invalid when **baseUrl** is left empty.|
384
385**Example**
386
387  ```ts
388  // xxx.ets
389  @Entry
390  @Component
391  struct WebComponent {
392    controller: WebController = new WebController()
393
394    build() {
395      Column() {
396        Button('loadData')
397          .onClick(() => {
398            this.controller.loadData({
399              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
400              mimeType: "text/html",
401              encoding: "UTF-8"
402            })
403          })
404        Web({ src: 'www.example.com', controller: this.controller })
405      }
406    }
407  }
408  ```
409
410## loadUrl<sup>(deprecated)</sup>
411
412loadUrl(options: { url: string | Resource, headers?: Array\<Header\> })
413
414Loads a URL using the specified HTTP header.
415
416The object injected through **loadUrl** is valid only in the current document. It will be invalid on a new page navigated to through **loadUrl**.
417
418The object injected through **registerJavaScriptProxy** is still valid on a new page redirected through **loadUrl**.
419
420This API is deprecated since API version 9. You are advised to use [loadUrl<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#loadurl) instead.
421
422**System capability**: SystemCapability.Web.Webview.Core
423
424**Parameters**
425
426| Name    | Type                      | Mandatory | Description          |
427| -------- | -------------------------- | ---- | -------------- |
428| url      | string \| Resource                     | Yes | URL to load.    |
429| headers  | Array\<[Header](./arkts-basic-components-web-i.md#header)\> | No   | Additional HTTP request header of the URL.<br>The default value is **[]**.|
430
431**Example**
432
433  ```ts
434  // xxx.ets
435  @Entry
436  @Component
437  struct WebComponent {
438    controller: WebController = new WebController()
439
440    build() {
441      Column() {
442        Button('loadUrl')
443          .onClick(() => {
444            this.controller.loadUrl({ url: 'www.example.com' })
445          })
446        Web({ src: 'www.example.com', controller: this.controller })
447      }
448    }
449  }
450  ```
451
452## onActive<sup>(deprecated)</sup>
453
454onActive(): void
455
456Called when the **Web** component enters the active state.
457
458This API is deprecated since API version 9. You are advised to use [onActive<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#onactive) instead.
459
460**System capability**: SystemCapability.Web.Webview.Core
461
462**Example**
463
464  ```ts
465  // xxx.ets
466  @Entry
467  @Component
468  struct WebComponent {
469    controller: WebController = new WebController()
470
471    build() {
472      Column() {
473        Button('onActive')
474          .onClick(() => {
475            this.controller.onActive()
476          })
477        Web({ src: 'www.example.com', controller: this.controller })
478      }
479    }
480  }
481  ```
482
483## onInactive<sup>(deprecated)</sup>
484
485onInactive(): void
486
487Called when the **Web** component enters the inactive state.
488
489This API is deprecated since API version 9. You are advised to use [onInactive<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#oninactive) instead.
490
491**System capability**: SystemCapability.Web.Webview.Core
492
493**Example**
494
495  ```ts
496  // xxx.ets
497  @Entry
498  @Component
499  struct WebComponent {
500    controller: WebController = new WebController()
501
502    build() {
503      Column() {
504        Button('onInactive')
505          .onClick(() => {
506            this.controller.onInactive()
507          })
508        Web({ src: 'www.example.com', controller: this.controller })
509      }
510    }
511  }
512  ```
513
514## zoom<sup>(deprecated)</sup>
515
516zoom(factor: number): void
517
518Sets a zoom factor for the current web page.
519
520This API is deprecated since API version 9. You are advised to use [zoom<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#zoom) instead.
521
522**System capability**: SystemCapability.Web.Webview.Core
523
524**Parameters**
525
526| Name   | Type  | Mandatory  | Description                          |
527| ------ | ------ | ---- | ------------------------------ |
528| factor | number | Yes   | Zoom factor to set. A positive value indicates zoom-in, and a negative value indicates zoom-out.|
529
530**Example**
531
532  ```ts
533  // xxx.ets
534  @Entry
535  @Component
536  struct WebComponent {
537    controller: WebController = new WebController()
538    @State factor: number = 1
539
540    build() {
541      Column() {
542        Button('zoom')
543          .onClick(() => {
544            this.controller.zoom(this.factor)
545          })
546        Web({ src: 'www.example.com', controller: this.controller })
547      }
548    }
549  }
550  ```
551
552## refresh<sup>(deprecated)</sup>
553
554refresh()
555
556Called when the **Web** component refreshes the web page.
557
558This API is deprecated since API version 9. You are advised to use [refresh<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#refresh) instead.
559
560**System capability**: SystemCapability.Web.Webview.Core
561
562**Example**
563
564  ```ts
565  // xxx.ets
566  @Entry
567  @Component
568  struct WebComponent {
569    controller: WebController = new WebController()
570
571    build() {
572      Column() {
573        Button('refresh')
574          .onClick(() => {
575            this.controller.refresh()
576          })
577        Web({ src: 'www.example.com', controller: this.controller })
578      }
579    }
580  }
581  ```
582
583## registerJavaScriptProxy<sup>(deprecated)</sup>
584
585registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })
586
587Registers a JavaScript object with the window. APIs of this object can then be invoked in the window. You must invoke the [refresh](#refreshdeprecated) API for the registration to take effect.
588
589This API is deprecated since API version 9. You are advised to use [registerJavaScriptProxy<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#registerjavascriptproxy) instead.
590
591**System capability**: SystemCapability.Web.Webview.Core
592
593**Parameters**
594
595| Name       | Type           | Mandatory | Description                                    |
596| ---------- | --------------- | ---- | ---------------------------------------- |
597| object     | object          | Yes   | Application-side JavaScript object to be registered. Methods and attributes can be declared, but cannot be directly called on HTML5. The parameters and return value can only be of the string, number, or Boolean type.|
598| name       | string          | Yes   | Name of the object to be registered, which is the same as that invoked in the window. After registration, the window can use this name to access the JavaScript object at the application side.|
599| methodList | Array\<string\> | Yes   | Methods of the JavaScript object to be registered at the application side.                |
600
601**Example**
602
603  ```ts
604  // xxx.ets
605  class TestObj {
606    constructor() {
607    }
608
609    test(): string {
610      return "ArkUI Web Component"
611    }
612
613    toString(): void {
614      console.log('Web Component toString')
615    }
616  }
617
618  @Entry
619  @Component
620  struct Index {
621    controller: WebController = new WebController()
622    testObj = new TestObj();
623    build() {
624      Column() {
625        Row() {
626          Button('Register JavaScript To Window').onClick(() => {
627            this.controller.registerJavaScriptProxy({
628              object: this.testObj,
629              name: "objName",
630              methodList: ["test", "toString"],
631            })
632          })
633        }
634        Web({ src: $rawfile('index.html'), controller: this.controller })
635          .javaScriptAccess(true)
636      }
637    }
638  }
639  ```
640
641  HTML file to be loaded:
642  ```html
643  <!-- index.html -->
644  <!DOCTYPE html>
645  <html>
646      <head>
647          <meta charset="utf-8">
648      </head>
649      <body>
650          Hello world!
651          <script type="text/javascript">
652              function htmlTest() {
653                  str = objName.test("test function")
654                  console.log('objName.test result:'+ str)
655              }
656          </script>
657      </body>
658  </html>
659
660  ```
661
662## runJavaScript<sup>(deprecated)</sup>
663
664runJavaScript(options: { script: string, callback?: (result: string) => void })
665
666Executes a JavaScript script. This API uses an asynchronous callback to return the script execution result. **runJavaScript** can be invoked only after **loadUrl** is executed. For example, it can be invoked in **onPageEnd**.
667
668This API is deprecated since API version 9. You are advised to use [runJavaScript<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#runjavascript) instead.
669
670**System capability**: SystemCapability.Web.Webview.Core
671
672**Parameters**
673
674| Name     | Type                    | Mandatory| Description                                    |
675| -------- | ------------------------ | ---- | ---------------------------------------- |
676| script   | string                   | Yes  | JavaScript script.                           |
677| callback | (result: string) => void | No  | Callback used to return the result. Returns **null** if the JavaScript script fails to be executed or no value is returned.|
678
679**Example**
680
681  ```ts
682  // xxx.ets
683  @Entry
684  @Component
685  struct WebComponent {
686    controller: WebController = new WebController()
687    @State webResult: string = ''
688    build() {
689      Column() {
690        Text(this.webResult).fontSize(20)
691        Web({ src: $rawfile('index.html'), controller: this.controller })
692        .javaScriptAccess(true)
693        .onPageEnd((event) => {
694          this.controller.runJavaScript({
695            script: 'test()',
696            callback: (result: string)=> {
697              this.webResult = result
698              console.info(`The test() return value is: ${result}`)
699            }})
700          if (event) {
701            console.info('url: ', event.url)
702          }
703        })
704      }
705    }
706  }
707  ```
708  HTML file to be loaded:
709  ```html
710  <!-- index.html -->
711  <!DOCTYPE html>
712  <html>
713    <head>
714        <meta charset="utf-8">
715    </head>
716    <body>
717        Hello world!
718        <script type="text/javascript">
719            function test() {
720                console.log('Ark WebComponent')
721                return "This value is from index.html"
722            }
723        </script>
724    </body>
725  </html>
726  ```
727
728## stop<sup>(deprecated)</sup>
729
730stop()
731
732Stops page loading.
733
734This API is deprecated since API version 9. You are advised to use [stop<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#stop) instead.
735
736**System capability**: SystemCapability.Web.Webview.Core
737
738**Example**
739
740  ```ts
741  // xxx.ets
742  @Entry
743  @Component
744  struct WebComponent {
745    controller: WebController = new WebController()
746
747    build() {
748      Column() {
749        Button('stop')
750          .onClick(() => {
751            this.controller.stop()
752          })
753        Web({ src: 'www.example.com', controller: this.controller })
754      }
755    }
756  }
757  ```
758
759## clearHistory<sup>(deprecated)</sup>
760
761clearHistory(): void
762
763Clears the browsing history.
764
765This API is deprecated since API version 9. You are advised to use [clearHistory<sup>9+</sup>](./arkts-apis-webview-WebviewController.md#clearhistory) instead.
766
767**System capability**: SystemCapability.Web.Webview.Core
768
769**Example**
770
771  ```ts
772  // xxx.ets
773  @Entry
774  @Component
775  struct WebComponent {
776    controller: WebController = new WebController()
777
778    build() {
779      Column() {
780        Button('clearHistory')
781          .onClick(() => {
782            this.controller.clearHistory()
783          })
784        Web({ src: 'www.example.com', controller: this.controller })
785      }
786    }
787  }
788  ```
789