1# Class (WebStorage) 2 3Implements a **WebStorage** object to manage the Web SQL database and HTML5 Web Storage APIs. All **Web** components in an application share a **WebStorage** object. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. 8> 9> - The initial APIs of this class are supported since API version 9. 10> 11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 12> 13> - You must load the **Web** component before calling the APIs in **WebStorage**. 14 15## Modules to Import 16 17```ts 18import { webview } from '@kit.ArkWeb'; 19``` 20 21## deleteOrigin 22 23static deleteOrigin(origin: string): void 24 25Deletes all data in the specified origin. 26 27**System capability**: SystemCapability.Web.Webview.Core 28 29**Parameters** 30 31| Name| Type | Mandatory| Description | 32| ------ | ------ | ---- | ------------------------ | 33| origin | string | Yes | Index of the origin, which is obtained through [getOrigins](#getorigins).| 34 35**Error codes** 36 37For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 38 39| ID| Error Message | 40| -------- | ------------------------------------------------------ | 41| 17100011 | Invalid origin. | 42| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 43 44**Example** 45 46```ts 47// xxx.ets 48import { webview } from '@kit.ArkWeb'; 49import { BusinessError } from '@kit.BasicServicesKit'; 50 51@Entry 52@Component 53struct WebComponent { 54 controller: webview.WebviewController = new webview.WebviewController(); 55 origin: string = "resource://rawfile/"; 56 57 build() { 58 Column() { 59 Button('deleteOrigin') 60 .onClick(() => { 61 try { 62 webview.WebStorage.deleteOrigin(this.origin); 63 } catch (error) { 64 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 65 } 66 67 }) 68 Web({ src: $rawfile('index.html'), controller: this.controller }) 69 .databaseAccess(true) 70 } 71 } 72} 73``` 74 75HTML file to be loaded: 76 ```html 77 <!-- index.html --> 78 <!DOCTYPE html> 79 <html> 80 <head> 81 <meta charset="UTF-8"> 82 <title>test</title> 83 <script type="text/javascript"> 84 85 var db = openDatabase('mydb','1.0','Test DB',2 * 1024 * 1024); 86 var msg; 87 88 db.transaction(function(tx){ 89 tx.executeSql('INSERT INTO LOGS (id,log) VALUES(1,"test1")'); 90 tx.executeSql('INSERT INTO LOGS (id,log) VALUES(2,"test2")'); 91 msg = '<p>Data table created, with two data records inserted.</p>'; 92 93 document.querySelector('#status').innerHTML = msg; 94 }); 95 96 db.transaction(function(tx){ 97 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) { 98 var len = results.rows.length,i; 99 msg = "<p>Number of records: " + len + "</p>"; 100 101 document.querySelector('#status').innerHTML += msg; 102 103 for(i = 0; i < len; i++){ 104 msg = "<p><b>" + results.rows.item(i).log + "</b></p>"; 105 106 document.querySelector('#status').innerHTML += msg; 107 } 108 },null); 109 }); 110 111 </script> 112 </head> 113 <body> 114 <div id="status" name="status">Status</div> 115 </body> 116 </html> 117 ``` 118 119## getOrigins 120 121static getOrigins(callback: AsyncCallback\<Array\<WebStorageOrigin>>): void 122 123Obtains information about all origins that are currently using the Web SQL Database. This API uses an asynchronous callback to return the result. 124 125**System capability**: SystemCapability.Web.Webview.Core 126 127**Parameters** 128 129| Name | Type | Mandatory| Description | 130| -------- | -------------------------------------- | ---- | ------------------------------------------------------ | 131| callback | AsyncCallback\<Array\<[WebStorageOrigin](./arkts-apis-webview-i.md#webstorageorigin)>> | Yes | Callback used to return the information about the origins.| 132 133**Error codes** 134 135For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 136 137| ID| Error Message | 138| -------- | ------------------------------------------------------ | 139| 17100012 | Invalid web storage origin. | 140| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 141 142**Example** 143 144```ts 145// xxx.ets 146import { webview } from '@kit.ArkWeb'; 147import { BusinessError } from '@kit.BasicServicesKit'; 148 149@Entry 150@Component 151struct WebComponent { 152 controller: webview.WebviewController = new webview.WebviewController(); 153 154 build() { 155 Column() { 156 Button('getOrigins') 157 .onClick(() => { 158 try { 159 webview.WebStorage.getOrigins((error, origins) => { 160 if (error) { 161 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 162 return; 163 } 164 for (let i = 0; i < origins.length; i++) { 165 console.log('origin: ' + origins[i].origin); 166 console.log('usage: ' + origins[i].usage); 167 console.log('quota: ' + origins[i].quota); 168 } 169 }) 170 } catch (error) { 171 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 172 } 173 174 }) 175 Web({ src: $rawfile('index.html'), controller: this.controller }) 176 .databaseAccess(true) 177 } 178 } 179} 180``` 181 182For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 183 184## getOrigins 185 186static getOrigins(): Promise\<Array\<WebStorageOrigin>> 187 188Obtains information about all origins that are currently using the Web SQL Database. This API uses a promise to return the result. 189 190**System capability**: SystemCapability.Web.Webview.Core 191 192**Return value** 193 194| Type | Description | 195| -------------------------------- | ------------------------------------------------------------ | 196| Promise\<Array\<[WebStorageOrigin](./arkts-apis-webview-i.md#webstorageorigin)>> | Target Promise used to return the information about the origins.| 197 198**Error codes** 199 200For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 201 202| ID| Error Message | 203| -------- | ------------------------------------------------------ | 204| 17100012 | Invalid web storage origin. | 205| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 206 207**Example** 208 209```ts 210// xxx.ets 211import { webview } from '@kit.ArkWeb'; 212import { BusinessError } from '@kit.BasicServicesKit'; 213 214@Entry 215@Component 216struct WebComponent { 217 controller: webview.WebviewController = new webview.WebviewController(); 218 219 build() { 220 Column() { 221 Button('getOrigins') 222 .onClick(() => { 223 try { 224 webview.WebStorage.getOrigins() 225 .then(origins => { 226 for (let i = 0; i < origins.length; i++) { 227 console.log('origin: ' + origins[i].origin); 228 console.log('usage: ' + origins[i].usage); 229 console.log('quota: ' + origins[i].quota); 230 } 231 }) 232 .catch((e: BusinessError) => { 233 console.log('error: ' + JSON.stringify(e)); 234 }) 235 } catch (error) { 236 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 237 } 238 239 }) 240 Web({ src: $rawfile('index.html'), controller: this.controller }) 241 .databaseAccess(true) 242 } 243 } 244} 245``` 246 247For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 248 249## getOriginQuota 250 251static getOriginQuota(origin: string, callback: AsyncCallback\<number>): void 252 253Obtains the storage quota of an origin in the Web SQL Database, in bytes. This API uses an asynchronous callback to return the result. 254 255**System capability**: SystemCapability.Web.Webview.Core 256 257**Parameters** 258 259| Name | Type | Mandatory| Description | 260| -------- | --------------------- | ---- | ------------------ | 261| origin | string | Yes | Index of the origin.| 262| callback | AsyncCallback\<number> | Yes | Storage quota of the origin.<br>**number** is a long integer ranging from -2,147,483,648 to 2,147,483,647. | 263 264**Error codes** 265 266For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 267 268| ID| Error Message | 269| -------- | ------------------------------------------------------ | 270| 17100011 | Invalid origin. | 271| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 272 273**Example** 274 275```ts 276// xxx.ets 277import { webview } from '@kit.ArkWeb'; 278import { BusinessError } from '@kit.BasicServicesKit'; 279 280@Entry 281@Component 282struct WebComponent { 283 controller: webview.WebviewController = new webview.WebviewController(); 284 origin: string = "resource://rawfile/"; 285 286 build() { 287 Column() { 288 Button('getOriginQuota') 289 .onClick(() => { 290 try { 291 webview.WebStorage.getOriginQuota(this.origin, (error, quota) => { 292 if (error) { 293 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 294 return; 295 } 296 console.log('quota: ' + quota); 297 }) 298 } catch (error) { 299 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 300 } 301 302 }) 303 Web({ src: $rawfile('index.html'), controller: this.controller }) 304 .databaseAccess(true) 305 } 306 } 307} 308``` 309 310For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 311 312## getOriginQuota 313 314static getOriginQuota(origin: string): Promise\<number> 315 316Obtains the storage quota of an origin in the Web SQL Database, in bytes. This API uses a promise to return the result. 317 318**System capability**: SystemCapability.Web.Webview.Core 319 320**Parameters** 321 322| Name| Type | Mandatory| Description | 323| ------ | ------ | ---- | ------------------ | 324| origin | string | Yes | Index of the origin.| 325 326**Return value** 327 328| Type | Description | 329| --------------- | --------------------------------------- | 330| Promise\<number> | Promise used to return the storage quota of the origin.| 331 332**Error codes** 333 334For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 335 336| ID| Error Message | 337| -------- | ------------------------------------------------------ | 338| 17100011 | Invalid origin. | 339| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 340 341**Example** 342 343```ts 344// xxx.ets 345import { webview } from '@kit.ArkWeb'; 346import { BusinessError } from '@kit.BasicServicesKit'; 347 348@Entry 349@Component 350struct WebComponent { 351 controller: webview.WebviewController = new webview.WebviewController(); 352 origin: string = "resource://rawfile/"; 353 354 build() { 355 Column() { 356 Button('getOriginQuota') 357 .onClick(() => { 358 try { 359 webview.WebStorage.getOriginQuota(this.origin) 360 .then(quota => { 361 console.log('quota: ' + quota); 362 }) 363 .catch((e: BusinessError) => { 364 console.log('error: ' + JSON.stringify(e)); 365 }) 366 } catch (error) { 367 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 368 } 369 370 }) 371 Web({ src: $rawfile('index.html'), controller: this.controller }) 372 .databaseAccess(true) 373 } 374 } 375} 376``` 377 378For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 379 380## getOriginUsage 381 382static getOriginUsage(origin: string, callback: AsyncCallback\<number>): void 383 384Obtains the storage usage of an origin in the Web SQL Database, in bytes. This API uses an asynchronous callback to return the result. 385 386**System capability**: SystemCapability.Web.Webview.Core 387 388**Parameters** 389 390| Name | Type | Mandatory| Description | 391| -------- | --------------------- | ---- | ------------------ | 392| origin | string | Yes | Index of the origin.| 393| callback | AsyncCallback\<number> | Yes | Storage usage of the origin. | 394 395**Error codes** 396 397For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 398 399| ID| Error Message | 400| -------- | ------------------------------------------------------ | 401| 17100011 | Invalid origin. | 402| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 403 404**Example** 405 406```ts 407// xxx.ets 408import { webview } from '@kit.ArkWeb'; 409import { BusinessError } from '@kit.BasicServicesKit'; 410 411@Entry 412@Component 413struct WebComponent { 414 controller: webview.WebviewController = new webview.WebviewController(); 415 origin: string = "resource://rawfile/"; 416 417 build() { 418 Column() { 419 Button('getOriginUsage') 420 .onClick(() => { 421 try { 422 webview.WebStorage.getOriginUsage(this.origin, (error, usage) => { 423 if (error) { 424 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 425 return; 426 } 427 console.log('usage: ' + usage); 428 }) 429 } catch (error) { 430 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 431 } 432 433 }) 434 Web({ src: $rawfile('index.html'), controller: this.controller }) 435 .databaseAccess(true) 436 } 437 } 438} 439``` 440 441For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 442 443## getOriginUsage 444 445static getOriginUsage(origin: string): Promise\<number> 446 447Obtains the storage usage of an origin in the Web SQL Database, in bytes. This API uses a promise to return the result. 448 449**System capability**: SystemCapability.Web.Webview.Core 450 451**Parameters** 452 453| Name| Type | Mandatory| Description | 454| ------ | ------ | ---- | ------------------ | 455| origin | string | Yes | Index of the origin.| 456 457**Return value** 458 459| Type | Description | 460| --------------- | ------------------------------------- | 461| Promise\<number> | Promise used to return the storage usage of the origin.| 462 463**Error codes** 464 465For details about the error codes, see [Webview Error Codes](errorcode-webview.md). 466 467| ID| Error Message | 468| -------- | ----------------------------------------------------- | 469| 17100011 | Invalid origin. | 470| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 471 472**Example** 473 474```ts 475// xxx.ets 476import { webview } from '@kit.ArkWeb'; 477import { BusinessError } from '@kit.BasicServicesKit'; 478 479@Entry 480@Component 481struct WebComponent { 482 controller: webview.WebviewController = new webview.WebviewController(); 483 origin: string = "resource://rawfile/"; 484 485 build() { 486 Column() { 487 Button('getOriginUsage') 488 .onClick(() => { 489 try { 490 webview.WebStorage.getOriginUsage(this.origin) 491 .then(usage => { 492 console.log('usage: ' + usage); 493 }).catch((e: BusinessError) => { 494 console.error('error: ' + JSON.stringify(e)); 495 }) 496 } catch (error) { 497 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 498 } 499 }) 500 Web({ src: $rawfile('index.html'), controller: this.controller }) 501 .databaseAccess(true) 502 } 503 } 504} 505``` 506 507For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 508 509## deleteAllData 510 511static deleteAllData(incognito?: boolean): void 512 513Deletes all data in the Web SQL Database. 514 515**System capability**: SystemCapability.Web.Webview.Core 516 517**Parameters** 518 519| Name| Type | Mandatory| Description | 520| ------ | ------ | ---- | ------------------ | 521| incognito<sup>11+</sup> | boolean | No | Whether to delete all data in the Web SQL Database in incognito mode. The value **true** means to delete all data in the Web SQL Database in incognito mode, and **false** means the opposite.| 522 523**Example** 524 525```ts 526// xxx.ets 527import { webview } from '@kit.ArkWeb'; 528import { BusinessError } from '@kit.BasicServicesKit'; 529 530@Entry 531@Component 532struct WebComponent { 533 controller: webview.WebviewController = new webview.WebviewController(); 534 535 build() { 536 Column() { 537 Button('deleteAllData') 538 .onClick(() => { 539 try { 540 webview.WebStorage.deleteAllData(); 541 } catch (error) { 542 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 543 } 544 }) 545 Web({ src: $rawfile('index.html'), controller: this.controller }) 546 .databaseAccess(true) 547 } 548 } 549} 550``` 551 552For details about the HTML file loaded, see the HTML file loaded using the [deleteOrigin](#deleteorigin) API. 553