1# Loading Web Pages 2 3 4Page loading is a basic capability of the **Web** component. Depending on the data source, page loading falls into three types: loading of network pages, loading of local pages, and loading of HTML rich text data. 5 6 7If you need to load online resources, declare the network access permission in the **module.json5** file. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md). 8 9 ``` 10 "requestPermissions":[ 11 { 12 "name" : "ohos.permission.INTERNET" 13 } 14 ] 15 ``` 16 17## Loading Network Pages 18 19You can specify the default network page to be loaded when creating a **Web** component. After the default network page is loaded, call [loadUrl()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#loadurl) if you want to change the network page displayed by the **Web** component. The value of the first parameter **src** of the [Web component](../reference/apis-arkweb/arkts-basic-components-web.md) cannot be dynamically changed through a state variable (for example, @State). To change the value, call [loadUrl()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#loadurl). 20 21 22In the following example, after the **www.\example.com** page is loaded by the **Web** component, **loadUrl** is called to change the displayed page to **www\.example1.com**. 23 24 25 26```ts 27// xxx.ets 28import { webview } from '@kit.ArkWeb'; 29import { BusinessError } from '@kit.BasicServicesKit'; 30 31@Entry 32@Component 33struct WebComponent { 34 controller: webview.WebviewController = new webview.WebviewController(); 35 36 build() { 37 Column() { 38 Button('loadUrl') 39 .onClick(() => { 40 try { 41 // Upon button clicking, call loadUrl to redirect to www.example1.com. 42 this.controller.loadUrl('www.example1.com'); 43 } catch (error) { 44 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 45 } 46 }) 47 // When creating a Web component, set the default network page to be loaded to www.example.com. 48 Web({ src: 'www.example.com', controller: this.controller }) 49 } 50 } 51} 52``` 53 54 55## Loading Local Pages 56 57To reduce user waiting time in scenarios such as startup, redirection, and weak network, and save time for dynamic content loading, you can load local pages to optimize user experience. 58 59The following example shows how to load a local page file. 60 61Local page files are stored in the application's **rawfile** directory. You can specify the local page to be loaded by default when creating a **Web** component. After page loading is complete, you can call [loadUrl()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#loadurl) to change the displayed page of the **Web** component. 62 63To reference a local CSS file when loading a local HTML file, perform the following steps: 64 65```html 66<link rel="stylesheet" href="resource://rawfile/xxx.css"> 67<link rel="stylesheet" href="file:// /data/storage/el2/base/haps/entry/cache/xxx.css">// Load the local CSS file in the sandbox path. 68``` 69 70- Local page file in the application's resources/rawfile directory: 71 72 **Figure 1** Path of local page files 73 74  75 76 77- Application code: 78 79 ```ts 80 // xxx.ets 81 import { webview } from '@kit.ArkWeb'; 82 import { BusinessError } from '@kit.BasicServicesKit'; 83 84 @Entry 85 @Component 86 struct WebComponent { 87 controller: webview.WebviewController = new webview.WebviewController(); 88 89 build() { 90 Column() { 91 Button('loadUrl') 92 .onClick(() => { 93 try { 94 // Upon button clicking, call loadUrl to redirect to local1.html. 95 this.controller.loadUrl($rawfile("local1.html")); 96 } catch (error) { 97 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 98 } 99 }) 100 // When creating a Web component, load the local.html file through $rawfile. 101 Web({ src: $rawfile("local.html"), controller: this.controller }) 102 } 103 } 104 } 105 ``` 106 107 108- Code of the **local.html** page: 109 110 ```html 111 <!-- local.html --> 112 <!DOCTYPE html> 113 <html> 114 <body> 115 <p>Hello World</p> 116 </body> 117 </html> 118 ``` 119 120- Code of the **local1.html** page: 121 122 ```html 123 <!-- local1.html --> 124 <!DOCTYPE html> 125 <html> 126 <body> 127 <p>This is local1 page</p> 128 </body> 129 </html> 130 ``` 131 132Example of loading local page files in the sandbox: 133 1341. Obtain the sandbox path through the constructed singleton object **GlobalContext**. You need to enable the [fileAccess](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#fileaccess) permission of the file system in the application. 135 136 ```ts 137 // GlobalContext.ets 138 export class GlobalContext { 139 private constructor() {} 140 private static instance: GlobalContext; 141 private _objects = new Map<string, Object>(); 142 143 public static getContext(): GlobalContext { 144 if (!GlobalContext.instance) { 145 GlobalContext.instance = new GlobalContext(); 146 } 147 return GlobalContext.instance; 148 } 149 150 getObject(value: string): Object | undefined { 151 return this._objects.get(value); 152 } 153 154 setObject(key: string, objectClass: Object): void { 155 this._objects.set(key, objectClass); 156 } 157 } 158 ``` 159 160 ```ts 161 // xxx.ets 162 import { webview } from '@kit.ArkWeb'; 163 import { GlobalContext } from '../GlobalContext'; 164 165 let url = 'file://' + GlobalContext.getContext().getObject("filesDir") + '/index.html'; 166 167 @Entry 168 @Component 169 struct WebComponent { 170 controller: webview.WebviewController = new webview.WebviewController(); 171 172 build() { 173 Column() { 174 // Load the files in the sandbox. 175 Web({ src: url, controller: this.controller }) 176 .fileAccess(true) 177 } 178 } 179 } 180 ``` 181 1822. Modify the **EntryAbility.ets** file. 183 184 The following uses **filesDir** as an example to describe how to obtain the path of the sandbox. For details about how to obtain other paths, see [Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths). 185 186 ```ts 187 // xxx.ets 188 import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 189 import { webview } from '@kit.ArkWeb'; 190 import { GlobalContext } from '../GlobalContext'; 191 192 export default class EntryAbility extends UIAbility { 193 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 194 // Data synchronization between the UIAbility component and UI can be implemented by binding filesDir to the GlobalContext object. 195 GlobalContext.getContext().setObject("filesDir", this.context.filesDir); 196 console.log("Sandbox path is " + GlobalContext.getContext().getObject("filesDir")); 197 } 198 } 199 ``` 200 201 HTML file to be loaded: 202 203 ```html 204 <!-- index.html --> 205 <!DOCTYPE html> 206 <html> 207 <body> 208 <p>Hello World</p> 209 </body> 210 </html> 211 ``` 212 213 214## Loading HTML Rich Text Data 215 216The **Web** component provides the [loadData()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#loaddata) API for you to load HTML rich text data. If you need to display only some page fragments, you can use this feature to quickly load the page. To load a large number of HTML files, set **baseUrl** to data. 217 218```ts 219// xxx.ets 220import { webview } from '@kit.ArkWeb'; 221import { BusinessError } from '@kit.BasicServicesKit'; 222 223@Entry 224@Component 225struct WebComponent { 226 controller: webview.WebviewController = new webview.WebviewController(); 227 228 build() { 229 Column() { 230 Button('loadData') 231 .onClick(() => { 232 try { 233 // Upon button clicking, call loadData to load HTML rich text data. 234 this.controller.loadData( 235 "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>", 236 "text/html", 237 "UTF-8" 238 ); 239 } catch (error) { 240 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 241 } 242 }) 243 // When creating a Web component, set the default network page to be loaded to www.example.com. 244 Web({ src: 'www.example.com', controller: this.controller }) 245 } 246 } 247} 248``` 249 250The **Web** component can load HTML strings using data urls. 251 252```ts 253// xxx.ets 254import { webview } from '@kit.ArkWeb'; 255import { BusinessError } from '@kit.BasicServicesKit'; 256 257@Entry 258@Component 259struct WebComponent { 260 controller: webview.WebviewController = new webview.WebviewController(); 261 htmlStr: string = "data:text/html, <html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>"; 262 263 build() { 264 Column() { 265 // When creating a Web component, set the default network page to be loaded to htmlStr. 266 Web({ src: this.htmlStr, controller: this.controller }) 267 } 268 } 269} 270``` 271 272## Loading Local Resources Using the Resource Protocol 273 274The resource protocol allows access to files in the application resource directory. 275 276```ts 277import { webview } from '@kit.ArkWeb'; 278 279@Entry 280@Component 281struct ResourceWebComponent { 282 controller: webview.WebviewController = new webview.WebviewController(); 283 284 build() { 285 Column() { 286 Button ('Load resources') 287 .onClick(() => { 288 try { 289 // Load the index1.html file in resources/rawfile through using the resource protocol. 290 this.controller.loadUrl('resource://rawfile/index1.html'); 291 } catch (error) { 292 console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); 293 } 294 }) 295 296 // When the component is created, use the resource protocol to load resources. 297 Web({ 298 src: 'resource://rawfile/index.html', controller: this.controller}) 299 } 300 } 301} 302``` 303 304Create the **index.html** file in **src/main/resources/rawfile**. 305```html 306<!-- index.html --> 307<!DOCTYPE html> 308<html> 309 <body> 310 <p>Hello World</p> 311 </body> 312</html> 313``` 314 315Create the **index1.html** file in **src/main/resources/rawfile**. 316 317```html 318<!-- index1.html --> 319<!DOCTYPE html> 320<html> 321 <body> 322 <p>Hello World Again</p> 323 </body> 324</html> 325``` 326 327## Samples 328 329The following samples are provided to help you better understand how to develop **Web** component: 330 331- [Browser (ArkTS) (Full SDK) (API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Web/Browser) 332