1# Components 2 3The **Web** component can be used to display web pages. It can be used with the [@ohos.web.webview](arkts-apis-webview.md) module, which provides APIs for web control. 4 5> **NOTE** 6> 7> - 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. 8> 9> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 10 11## Required Permissions 12 13To use online resources, the application must have the **ohos.permission.INTERNET** permission. For details about how to apply for a permission, see [Declaring Permissions](../../security/AccessToken/declare-permissions.md). 14 15## Child Components 16 17Not supported 18 19## APIs 20 21Web(value: WebOptions) 22 23> **NOTE** 24> 25> Transition animation is not supported. 26> 27> **Web** components on the same page must be bound to different **WebviewController** instances. 28 29**System capability**: SystemCapability.Web.Webview.Core 30 31**Parameters** 32 33| Name | Type | Mandatory | Description | 34| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 35| value | [WebOptions](./arkts-basic-components-web-i.md#weboptions) | Yes | Define web options.| 36 37**Example** 38 39Example of loading online web pages: 40 41 ```ts 42 // xxx.ets 43 import { webview } from '@kit.ArkWeb'; 44 45 @Entry 46 @Component 47 struct WebComponent { 48 controller: webview.WebviewController = new webview.WebviewController(); 49 50 build() { 51 Column() { 52 Web({ src: 'www.example.com', controller: this.controller }) 53 } 54 } 55 } 56 ``` 57 58Example of loading online web pages in incognito mode: 59 60 ```ts 61 // xxx.ets 62 import { webview } from '@kit.ArkWeb'; 63 64 @Entry 65 @Component 66 struct WebComponent { 67 controller: webview.WebviewController = new webview.WebviewController(); 68 69 build() { 70 Column() { 71 Web({ src: 'www.example.com', controller: this.controller, incognitoMode: true }) 72 } 73 } 74 } 75 ``` 76 77Example of rendering the **Web** component in synchronous mode: 78 79 ```ts 80 // xxx.ets 81 import { webview } from '@kit.ArkWeb'; 82 83 @Entry 84 @Component 85 struct WebComponent { 86 controller: webview.WebviewController = new webview.WebviewController(); 87 88 build() { 89 Column() { 90 Web({ src: 'www.example.com', controller: this.controller, renderMode: RenderMode.SYNC_RENDER }) 91 } 92 } 93 } 94 ``` 95 96Example of using the **Web** component to specify the shared rendering process. 97 98 ```ts 99 // xxx.ets 100 import { webview } from '@kit.ArkWeb'; 101 102 @Entry 103 @Component 104 struct WebComponent { 105 controller: webview.WebviewController = new webview.WebviewController(); 106 107 build() { 108 Column() { 109 Web({ src: 'www.example.com', controller: this.controller, sharedRenderProcessToken: "111" }) 110 Web({ src: 'www.w3.org', controller: this.controller, sharedRenderProcessToken: "111" }) 111 } 112 } 113 } 114 ``` 115 116Example of loading local web pages using **$rawfile()**: 117 118 119 ```ts 120 // xxx.ets 121 import { webview } from '@kit.ArkWeb'; 122 123 @Entry 124 @Component 125 struct WebComponent { 126 controller: webview.WebviewController = new webview.WebviewController(); 127 128 build() { 129 Column() { 130 // Load a local resource file through $rawfile. 131 Web({ src: $rawfile("index.html"), controller: this.controller }) 132 } 133 } 134 } 135 ``` 136 137Example of loading a link with the hash (#) route through the resource protocol in WebView: 138 139When **$rawfile** is used to load a URL contains a number sign (#), the content following the number sign is treated as a fragment. To avoid this issue, you can use the **resource://rawfile/** protocol prefix instead. 140 ```ts 141 // xxx.ets 142 import { webview } from '@kit.ArkWeb'; 143 144 @Entry 145 @Component 146 struct WebComponent { 147 controller: webview.WebviewController = new webview.WebviewController(); 148 149 build() { 150 Column() { 151 // Load a local resource file through the resource protocol. 152 Web({ src: "resource://rawfile/index.html#home", controller: this.controller }) 153 } 154 } 155 } 156 ``` 157 158Create an **index.html** file in **src/main/resources/rawfile**. 159```html 160<!-- index.html --> 161<!DOCTYPE html> 162<html> 163<body> 164<div id="content"></div> 165 166<script> 167 function loadContent() { 168 var hash = window.location.hash; 169 var contentDiv = document.getElementById('content'); 170 171 if (hash === '#home') { 172 contentDiv.innerHTML = '<h1>Home Page</h1><p>Welcome to the Home Page!</p>'; 173 } else { 174 contentDiv.innerHTML = '<h1>Default Page</h1><p>This is the default content.</p>'; 175 } 176 } 177 178 // Load the UI. 179 window.addEventListener('load', loadContent); 180 181 // Update the UI when the hash changes. 182 window.addEventListener('hashchange', loadContent); 183</script> 184</body> 185</html> 186``` 187 188To load the local resource file in the sandbox path, you need to configure the [fileAccess](./arkts-basic-components-web-attributes.md#fileaccess) permission of the file system in the application. 189 1901. Obtain the sandbox path through the constructed singleton object **GlobalContext**. 191 192 ```ts 193 // GlobalContext.ets 194 export class GlobalContext { 195 private constructor() {} 196 private static instance: GlobalContext; 197 private _objects = new Map<string, Object>(); 198 199 public static getContext(): GlobalContext { 200 if (!GlobalContext.instance) { 201 GlobalContext.instance = new GlobalContext(); 202 } 203 return GlobalContext.instance; 204 } 205 206 getObject(value: string): Object | undefined { 207 return this._objects.get(value); 208 } 209 210 setObject(key: string, objectClass: Object): void { 211 this._objects.set(key, objectClass); 212 } 213 } 214 ``` 215 216 ```ts 217 // xxx.ets 218 import { webview } from '@kit.ArkWeb'; 219 import { GlobalContext } from '../GlobalContext'; 220 221 let url = 'file://' + GlobalContext.getContext().getObject("filesDir") + '/index.html'; 222 223 @Entry 224 @Component 225 struct WebComponent { 226 controller: webview.WebviewController = new webview.WebviewController(); 227 228 build() { 229 Column() { 230 // Load the files in the sandbox. 231 Web({ src: url, controller: this.controller }) 232 .fileAccess(true) 233 } 234 } 235 } 236 ``` 237 2382. Modify the **EntryAbility.ets** file. 239 240 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). 241 242 ```ts 243 // xxx.ets 244 import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 245 import { webview } from '@kit.ArkWeb'; 246 import { GlobalContext } from '../GlobalContext'; 247 248 export default class EntryAbility extends UIAbility { 249 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 250 // Data synchronization between the UIAbility component and UI can be implemented by binding filesDir to the GlobalContext object. 251 GlobalContext.getContext().setObject("filesDir", this.context.filesDir); 252 console.log("Sandbox path is " + GlobalContext.getContext().getObject("filesDir")); 253 } 254 } 255 ``` 256 257 HTML file to be loaded: 258 259 ```html 260 <!-- index.html --> 261 <!DOCTYPE html> 262 <html> 263 <body> 264 <p>Hello World</p> 265 </body> 266 </html> 267 ``` 268