# Loading Web Pages Page 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. If 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). ``` "requestPermissions":[ { "name" : "ohos.permission.INTERNET" } ] ``` ## Loading Network Pages You 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). In 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**. ```ts // xxx.ets import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { Button('loadUrl') .onClick(() => { try { // Upon button clicking, call loadUrl to redirect to www.example1.com. this.controller.loadUrl('www.example1.com'); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }) // When creating a Web component, set the default network page to be loaded to www.example.com. Web({ src: 'www.example.com', controller: this.controller }) } } } ``` ## Loading Local Pages To 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. The following example shows how to load a local page file. Local 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. To reference a local CSS file when loading a local HTML file, perform the following steps: ```html // Load the local CSS file in the sandbox path. ``` - Local page file in the application's resources/rawfile directory: **Figure 1** Path of local page files  - Application code: ```ts // xxx.ets import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { Button('loadUrl') .onClick(() => { try { // Upon button clicking, call loadUrl to redirect to local1.html. this.controller.loadUrl($rawfile("local1.html")); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }) // When creating a Web component, load the local.html file through $rawfile. Web({ src: $rawfile("local.html"), controller: this.controller }) } } } ``` - Code of the **local.html** page: ```html
Hello World
``` - Code of the **local1.html** page: ```htmlThis is local1 page
``` Example of loading local page files in the sandbox: 1. 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. ```ts // GlobalContext.ets export class GlobalContext { private constructor() {} private static instance: GlobalContext; private _objects = new MapHello World
``` ## Loading HTML Rich Text Data The **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. ```ts // xxx.ets import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { Button('loadData') .onClick(() => { try { // Upon button clicking, call loadData to load HTML rich text data. this.controller.loadData( "Source:source", "text/html", "UTF-8" ); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }) // When creating a Web component, set the default network page to be loaded to www.example.com. Web({ src: 'www.example.com', controller: this.controller }) } } } ``` The **Web** component can load HTML strings using data urls. ```ts // xxx.ets import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); htmlStr: string = "data:text/html, Source:
source"; build() { Column() { // When creating a Web component, set the default network page to be loaded to htmlStr. Web({ src: this.htmlStr, controller: this.controller }) } } } ``` ## Loading Local Resources Using the Resource Protocol The resource protocol allows access to files in the application resource directory. ```ts import { webview } from '@kit.ArkWeb'; @Entry @Component struct ResourceWebComponent { controller: webview.WebviewController = new webview.WebviewController(); build() { Column() { Button ('Load resources') .onClick(() => { try { // Load the index1.html file in resources/rawfile through using the resource protocol. this.controller.loadUrl('resource://rawfile/index1.html'); } catch (error) { console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); } }) // When the component is created, use the resource protocol to load resources. Web({ src: 'resource://rawfile/index.html', controller: this.controller}) } } } ``` Create the **index.html** file in **src/main/resources/rawfile**. ```html
Hello World
``` Create the **index1.html** file in **src/main/resources/rawfile**. ```htmlHello World Again
``` ## Samples The following samples are provided to help you better understand how to develop **Web** component: - [Browser (ArkTS) (Full SDK) (API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Web/Browser)