• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Web
2
3The **\<Web>** component can be used to display web pages. For details, see [Web API](../reference/arkui-ts/ts-basic-components-web.md).
4
5## Creating a Component
6
7Create a **\<Web>** component in the .ets file under **main/ets/MainAbility/pages**. Then, in the created component, use **src** to specify the web page URI to be referenced, and bind a controller to the component to call the component APIs.
8
9  ```
10  // xxx.ets
11  @Entry
12  @Component
13  struct WebComponent {
14    controller: WebController = new WebController();
15    build() {
16      Column() {
17        Web({ src: 'https://www.example.com', controller: this.controller })
18      }
19    }
20  }
21  ```
22
23## Setting Styles and Attributes
24
25When using the **\<Web>** component, you need to specify the styles and attributes. The sample code is as follows.
26
27```
28// xxx.ets
29@Entry
30@Component
31struct WebComponent {
32  fileAccess: boolean = true;
33  controller: WebController = new WebController();
34  build() {
35    Column() {
36      Text('Hello world!')
37        .fontSize(20)
38      Web({ src: 'https://www.example.com', controller: this.controller })
39        // Set the height and padding.
40        .height(500)
41        .padding(20)
42        // Set the file access permission and script execution permission.
43        .fileAccess(this.fileAccess)
44        .javaScriptAccess(true)
45      Text('End')
46        .fontSize(20)
47    }
48  }
49}
50```
51## Adding Events and Methods
52
53Add the **onProgressChange** event for the **\<Web>** component, which is triggered when the loading progress changes and returns the progress value in its callback. Assign the progress value to the **\<Progress>** component to control the status of the component. When the progress reaches 100%, the **\<Progress>** component is hidden, and the web page loading is complete.
54
55```
56// xxx.ets
57@Entry
58@Component
59struct WebComponent {
60  @State progress: number = 0;
61  @State hideProgress: boolean = true;
62  fileAccess: boolean = true;
63  controller: WebController = new WebController();
64  build() {
65    Column() {
66      Text('Hello world!')
67        .fontSize(20)
68      Progress({value: this.progress, total: 100})
69        .color('#0000ff')
70        .visibility(this.hideProgress ? Visibility.None : Visibility.Visible)
71      Web({ src: 'https://www.example.com', controller: this.controller })
72        .fileAccess(this.fileAccess)
73        .javaScriptAccess(true)
74        .height(500)
75        .padding(20)
76        // Add an onProgressChange event.
77        .onProgressChange(e => {
78          this.progress = e.newProgress;
79          // When the progress reaches 100%, the progress bar disappears.
80          if (this.progress === 100) {
81            this.hideProgress = true;
82          } else {
83            this.hideProgress = false;
84          }
85        })
86      Text('End')
87        .fontSize(20)
88    }
89  }
90}
91```
92Add the **runJavaScript** method to the **onPageEnd** event. The **onPageEnd** event is triggered when the web page finishes loading. In this case, the **runJavaScript** method can be used to execute JavaScript scripts in the HTML file. In the sample code below, when the web page finishes loading, the **onPageEnd** event is triggered to call the **test** method in the HTML file and print information on the console.
93
94```
95// xxx.ets
96@Entry
97@Component
98struct WebComponent {
99  @State progress: number = 0;
100  @State hideProgress: boolean = true;
101  fileAccess: boolean = true;
102  // Define the controller for the \<Web> component.
103  controller: WebController = new WebController();
104  build() {
105    Column() {
106      Text('Hello world!')
107        .fontSize(20)
108      Progress({value: this.progress, total: 100})
109        .color('#0000ff')
110        .visibility(this.hideProgress ? Visibility.None : Visibility.Visible)
111      // Initialize the \<Web> component and bind it to the controller.
112      Web({ src: $rawfile('index.html'), controller: this.controller })
113        .fileAccess(this.fileAccess)
114        .javaScriptAccess(true)
115        .height(500)
116        .padding(20)
117        .onProgressChange(e => {
118          this.progress = e.newProgress;
119          if (this.progress === 100) {
120            this.hideProgress = true;
121          } else {
122            this.hideProgress = false;
123          }
124        })
125        .onPageEnd(e => {
126          // test() is defined in index.html.
127          this.controller.runJavaScript({ script: 'test()' });
128          console.info('url: ', e.url);
129        })
130      Text('End')
131        .fontSize(20)
132    }
133  }
134}
135```
136
137Create an HTML file in **main/resources/rawfile**.
138
139```
140<!-- index.html -->
141<!DOCTYPE html>
142<html>
143<meta charset="utf-8">
144<body>
145    Hello world!
146</body>
147<script type="text/javascript">
148  function test() {
149      console.info('Ark Web Component');
150  }
151</script>
152</html>
153```
154## Scenario Example
155
156In this example, you'll implement a **\<Web>** component where videos can be played dynamically. Embed a video resource into an HTML page, and then use the **\<Web>** component controller to invoke the **onActive** and **onInactive** methods to activate and pause page rendering, respectively. Upon clicking of the **onInactive** button, the **\<Web>** component stops rendering and the video playback pauses. Upon clicking of the **onActive** button, the **\<Web>** component is activated and the video playback resumes.
157
158  ```
159  // xxx.ets
160@Entry
161@Component
162struct WebComponent {
163  controller: WebController = new WebController();
164  build() {
165    Column() {
166      Row() {
167        Button('onActive').onClick(() => {
168          console.info("Web Component onActive");
169          this.controller.onActive();
170        })
171        Button('onInactive').onClick(() => {
172          console.info("Web Component onInactive");
173          this.controller.onInactive();
174        })
175      }
176      Web({ src: $rawfile('index.html'), controller: this.controller })
177        .fileAccess(true)
178    }
179  }
180}
181  ```
182
183  ```
184  <!-- index.html -->
185  <!DOCTYPE html>
186  <html>
187  <meta charset="utf-8">
188  <body>
189      <video width="320" height="240" controls="controls" muted="muted" autoplay="autoplay">
190          <!-- The test.mp4 video file is stored in main/resources/rawfile. -->
191          <source src="test.mp4" type="video/mp4">
192      </video>
193  </body>
194  </html>
195  ```
196