1e41f4b71Sopenharmony_ci# Invoking Application Functions on the Frontend Page 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciYou can use the **Web** component to register application code with frontend pages. After the registration is done, frontend pages can use the registered object names to call application functions. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciTwo methods are available for registering the application code:<br>Call [javaScriptProxy()](../reference/apis-arkweb/ts-basic-components-web.md#javascriptproxy) during **Web** component initialization. Call [registerJavaScriptProxy()](../reference/apis-arkweb/js-apis-webview.md#registerjavascriptproxy) after **Web** component initialization. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciThe following example registers the **test()** function with the frontend page. This way, the **test()** function can be triggered and run on the frontend page. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- Sample code for using [javaScriptProxy()](../reference/apis-arkweb/ts-basic-components-web.md#javascriptproxy): 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci ```ts 16e41f4b71Sopenharmony_ci // xxx.ets 17e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci class testClass { 20e41f4b71Sopenharmony_ci constructor() { 21e41f4b71Sopenharmony_ci } 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci test(): string { 24e41f4b71Sopenharmony_ci return 'ArkTS Hello World!'; 25e41f4b71Sopenharmony_ci } 26e41f4b71Sopenharmony_ci } 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci @Entry 29e41f4b71Sopenharmony_ci @Component 30e41f4b71Sopenharmony_ci struct WebComponent { 31e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 32e41f4b71Sopenharmony_ci // Declare the object to be registered. 33e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci build() { 36e41f4b71Sopenharmony_ci Column() { 37e41f4b71Sopenharmony_ci // Load the local index.html page. 38e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController}) 39e41f4b71Sopenharmony_ci // Inject the object to the web client. 40e41f4b71Sopenharmony_ci .javaScriptProxy({ 41e41f4b71Sopenharmony_ci object: this.testObj, 42e41f4b71Sopenharmony_ci name: "testObjName", 43e41f4b71Sopenharmony_ci methodList: ["test"], 44e41f4b71Sopenharmony_ci controller: this.webviewController, 45e41f4b71Sopenharmony_ci // Optional parameter. 46e41f4b71Sopenharmony_ci asyncMethodList: [], 47e41f4b71Sopenharmony_ci permission: '{"javascriptProxyPermission":{"urlPermissionList":[{"scheme":"resource","host":"rawfile","port":"","path":""},' + 48e41f4b71Sopenharmony_ci '{"scheme":"e","host":"f","port":"g","path":"h"}],"methodList":[{"methodName":"test","urlPermissionList":' + 49e41f4b71Sopenharmony_ci '[{"scheme":"https","host":"xxx.com","port":"","path":""},{"scheme":"resource","host":"rawfile","port":"","path":""}]},' + 50e41f4b71Sopenharmony_ci '{"methodName":"test11","urlPermissionList":[{"scheme":"q","host":"r","port":"","path":"t"},' + 51e41f4b71Sopenharmony_ci '{"scheme":"u","host":"v","port":"","path":""}]}]}}' 52e41f4b71Sopenharmony_ci }) 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci- Sample code for using [registerJavaScriptProxy()](../reference/apis-arkweb/js-apis-webview.md#registerjavascriptproxy): 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci ```ts 62e41f4b71Sopenharmony_ci // xxx.ets 63e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 64e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci class testClass { 67e41f4b71Sopenharmony_ci constructor() { 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci test(): string { 71e41f4b71Sopenharmony_ci return "ArkUI Web Component"; 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci toString(): void { 75e41f4b71Sopenharmony_ci console.log('Web Component toString'); 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci @Entry 80e41f4b71Sopenharmony_ci @Component 81e41f4b71Sopenharmony_ci struct Index { 82e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 83e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci build() { 86e41f4b71Sopenharmony_ci Column() { 87e41f4b71Sopenharmony_ci Button('refresh') 88e41f4b71Sopenharmony_ci .onClick(() => { 89e41f4b71Sopenharmony_ci try { 90e41f4b71Sopenharmony_ci this.webviewController.refresh(); 91e41f4b71Sopenharmony_ci } catch (error) { 92e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 93e41f4b71Sopenharmony_ci } 94e41f4b71Sopenharmony_ci }) 95e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 96e41f4b71Sopenharmony_ci .onClick(() => { 97e41f4b71Sopenharmony_ci try { 98e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"], 99e41f4b71Sopenharmony_ci // Optional parameter: asyncMethodList 100e41f4b71Sopenharmony_ci [], 101e41f4b71Sopenharmony_ci // Optional parameter: permission 102e41f4b71Sopenharmony_ci '{"javascriptProxyPermission":{"urlPermissionList":[{"scheme":"resource","host":"rawfile","port":"","path":""},' + 103e41f4b71Sopenharmony_ci '{"scheme":"e","host":"f","port":"g","path":"h"}],"methodList":[{"methodName":"test","urlPermissionList":' + 104e41f4b71Sopenharmony_ci '[{"scheme":"https","host":"xxx.com","port":"","path":""},{"scheme":"resource","host":"rawfile","port":"","path":""}]},' + 105e41f4b71Sopenharmony_ci '{"methodName":"test11","urlPermissionList":[{"scheme":"q","host":"r","port":"","path":"t"},' + 106e41f4b71Sopenharmony_ci '{"scheme":"u","host":"v","port":"","path":""}]}]}}' 107e41f4b71Sopenharmony_ci ); 108e41f4b71Sopenharmony_ci } catch (error) { 109e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci }) 112e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci ``` 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci > **NOTE** 119e41f4b71Sopenharmony_ci > 120e41f4b71Sopenharmony_ci > - If you use [registerJavaScriptProxy()](../reference/apis-arkweb/js-apis-webview.md#registerjavascriptproxy) to register a function, call [refresh()](../reference/apis-arkweb/js-apis-webview.md#refresh) for the function to take effect. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci- The optional parameter permission is a JSON string. The following is an example: 123e41f4b71Sopenharmony_ci ```json 124e41f4b71Sopenharmony_ci { 125e41f4b71Sopenharmony_ci "javascriptProxyPermission": { 126e41f4b71Sopenharmony_ci "urlPermissionList": [ // Object-level permission. If it is granted, all methods are available. 127e41f4b71Sopenharmony_ci { 128e41f4b71Sopenharmony_ci "scheme": "resource", // Exact match. The value cannot be empty. 129e41f4b71Sopenharmony_ci "host": "rawfile", // Exact match. The value cannot be empty. 130e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 131e41f4b71Sopenharmony_ci "path": "" // Prefix match. If the value is empty, it is not checked. 132e41f4b71Sopenharmony_ci }, 133e41f4b71Sopenharmony_ci { 134e41f4b71Sopenharmony_ci "scheme": "https", // Exact match. The value cannot be empty. 135e41f4b71Sopenharmony_ci "host": "xxx.com", // Exact match. The value cannot be empty. 136e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 137e41f4b71Sopenharmony_ci "path": "a/b/c" // Prefix match. If the value is empty, it is not checked. 138e41f4b71Sopenharmony_ci } 139e41f4b71Sopenharmony_ci ], 140e41f4b71Sopenharmony_ci "methodList": [ 141e41f4b71Sopenharmony_ci { 142e41f4b71Sopenharmony_ci "methodName": "test", 143e41f4b71Sopenharmony_ci "urlPermissionList": [ // Method-level permission. 144e41f4b71Sopenharmony_ci { 145e41f4b71Sopenharmony_ci "scheme": "https", // Exact match. The value cannot be empty. 146e41f4b71Sopenharmony_ci "host": "xxx.com", // Exact match. The value cannot be empty. 147e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 148e41f4b71Sopenharmony_ci "path": "" // Prefix match. If the value is empty, it is not checked. 149e41f4b71Sopenharmony_ci }, 150e41f4b71Sopenharmony_ci { 151e41f4b71Sopenharmony_ci "scheme": "resource", // Exact match. The value cannot be empty. 152e41f4b71Sopenharmony_ci "host": "rawfile", // Exact match. The value cannot be empty. 153e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 154e41f4b71Sopenharmony_ci "path": "" // Prefix match. If the value is empty, it is not checked. 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci ] 157e41f4b71Sopenharmony_ci }, 158e41f4b71Sopenharmony_ci { 159e41f4b71Sopenharmony_ci "methodName": "test11", 160e41f4b71Sopenharmony_ci "urlPermissionList": [ // Method-level permission. 161e41f4b71Sopenharmony_ci { 162e41f4b71Sopenharmony_ci "scheme": "q", // Exact match. The value cannot be empty. 163e41f4b71Sopenharmony_ci "host": "r", // Exact match. The value cannot be empty. 164e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 165e41f4b71Sopenharmony_ci "path": "t" // Prefix match. If the value is empty, it is not checked. 166e41f4b71Sopenharmony_ci }, 167e41f4b71Sopenharmony_ci { 168e41f4b71Sopenharmony_ci "scheme": "u", // Exact match. The value cannot be empty. 169e41f4b71Sopenharmony_ci "host": "v", // Exact match. The value cannot be empty. 170e41f4b71Sopenharmony_ci "port": "", // Exact match. If the value is empty, it is not checked. 171e41f4b71Sopenharmony_ci "path": "" // Prefix match. If the value is empty, it is not checked. 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci ] 174e41f4b71Sopenharmony_ci } 175e41f4b71Sopenharmony_ci ] 176e41f4b71Sopenharmony_ci } 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci ``` 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci- Sample code for invoking application functions on the **index.html** frontend page: 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci ```html 183e41f4b71Sopenharmony_ci <!-- index.html --> 184e41f4b71Sopenharmony_ci <!DOCTYPE html> 185e41f4b71Sopenharmony_ci <html> 186e41f4b71Sopenharmony_ci <body> 187e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 188e41f4b71Sopenharmony_ci <p id="demo"></p> 189e41f4b71Sopenharmony_ci <script> 190e41f4b71Sopenharmony_ci function callArkTS() { 191e41f4b71Sopenharmony_ci let str = testObjName.test(); 192e41f4b71Sopenharmony_ci document.getElementById("demo").innerHTML = str; 193e41f4b71Sopenharmony_ci console.info('ArkTS Hello World! :' + str); 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci </script> 196e41f4b71Sopenharmony_ci </body> 197e41f4b71Sopenharmony_ci </html> 198e41f4b71Sopenharmony_ci ``` 199e41f4b71Sopenharmony_ci## Usage of Complex Types 200e41f4b71Sopenharmony_ci- Sample code for passing arrays between the application side and the frontend page: 201e41f4b71Sopenharmony_ci ```ts 202e41f4b71Sopenharmony_ci // xxx.ets 203e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 204e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci class testClass { 207e41f4b71Sopenharmony_ci constructor() { 208e41f4b71Sopenharmony_ci } 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci test(): Array<Number> { 211e41f4b71Sopenharmony_ci return [1, 2, 3, 4] 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci toString(param: String): void { 215e41f4b71Sopenharmony_ci console.log('Web Component toString' + param); 216e41f4b71Sopenharmony_ci } 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci @Entry 220e41f4b71Sopenharmony_ci @Component 221e41f4b71Sopenharmony_ci struct Index { 222e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 223e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci build() { 226e41f4b71Sopenharmony_ci Column() { 227e41f4b71Sopenharmony_ci Button('refresh') 228e41f4b71Sopenharmony_ci .onClick(() => { 229e41f4b71Sopenharmony_ci try { 230e41f4b71Sopenharmony_ci this.webviewController.refresh(); 231e41f4b71Sopenharmony_ci } catch (error) { 232e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 233e41f4b71Sopenharmony_ci } 234e41f4b71Sopenharmony_ci }) 235e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 236e41f4b71Sopenharmony_ci .onClick(() => { 237e41f4b71Sopenharmony_ci try { 238e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 239e41f4b71Sopenharmony_ci } catch (error) { 240e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 241e41f4b71Sopenharmony_ci } 242e41f4b71Sopenharmony_ci }) 243e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 244e41f4b71Sopenharmony_ci } 245e41f4b71Sopenharmony_ci } 246e41f4b71Sopenharmony_ci } 247e41f4b71Sopenharmony_ci ``` 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci ```html 250e41f4b71Sopenharmony_ci <!-- index.html --> 251e41f4b71Sopenharmony_ci <!DOCTYPE html> 252e41f4b71Sopenharmony_ci <html> 253e41f4b71Sopenharmony_ci <body> 254e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 255e41f4b71Sopenharmony_ci <p id="demo"></p> 256e41f4b71Sopenharmony_ci <script> 257e41f4b71Sopenharmony_ci function callArkTS() { 258e41f4b71Sopenharmony_ci testObjName.toString(testObjName.test()); 259e41f4b71Sopenharmony_ci } 260e41f4b71Sopenharmony_ci </script> 261e41f4b71Sopenharmony_ci </body> 262e41f4b71Sopenharmony_ci </html> 263e41f4b71Sopenharmony_ci ``` 264e41f4b71Sopenharmony_ci- Sample code for passing data of primitive types (not of Function or any other complex type) between the application side and the frontend page: 265e41f4b71Sopenharmony_ci ```ts 266e41f4b71Sopenharmony_ci // xxx.ets 267e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 268e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci class student { 271e41f4b71Sopenharmony_ci name: string = ''; 272e41f4b71Sopenharmony_ci age: string = ''; 273e41f4b71Sopenharmony_ci } 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci class testClass { 276e41f4b71Sopenharmony_ci constructor() { 277e41f4b71Sopenharmony_ci } 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci // Data of primitive types to pass: name:"jeck", age:"12" 280e41f4b71Sopenharmony_ci test(): student { 281e41f4b71Sopenharmony_ci let st: student = { name: "jeck", age: "12" }; 282e41f4b71Sopenharmony_ci return st; 283e41f4b71Sopenharmony_ci } 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci toString(param: ESObject): void { 286e41f4b71Sopenharmony_ci console.log('Web Component toString' + param["name"]); 287e41f4b71Sopenharmony_ci } 288e41f4b71Sopenharmony_ci } 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci @Entry 291e41f4b71Sopenharmony_ci @Component 292e41f4b71Sopenharmony_ci struct Index { 293e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 294e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci build() { 297e41f4b71Sopenharmony_ci Column() { 298e41f4b71Sopenharmony_ci Button('refresh') 299e41f4b71Sopenharmony_ci .onClick(() => { 300e41f4b71Sopenharmony_ci try { 301e41f4b71Sopenharmony_ci this.webviewController.refresh(); 302e41f4b71Sopenharmony_ci } catch (error) { 303e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 304e41f4b71Sopenharmony_ci } 305e41f4b71Sopenharmony_ci }) 306e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 307e41f4b71Sopenharmony_ci .onClick(() => { 308e41f4b71Sopenharmony_ci try { 309e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 310e41f4b71Sopenharmony_ci } catch (error) { 311e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 312e41f4b71Sopenharmony_ci } 313e41f4b71Sopenharmony_ci }) 314e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 315e41f4b71Sopenharmony_ci } 316e41f4b71Sopenharmony_ci } 317e41f4b71Sopenharmony_ci } 318e41f4b71Sopenharmony_ci ``` 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci ```html 321e41f4b71Sopenharmony_ci <!-- index.html --> 322e41f4b71Sopenharmony_ci <!DOCTYPE html> 323e41f4b71Sopenharmony_ci <html> 324e41f4b71Sopenharmony_ci <body> 325e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 326e41f4b71Sopenharmony_ci <p id="demo"></p> 327e41f4b71Sopenharmony_ci <script> 328e41f4b71Sopenharmony_ci function callArkTS() { 329e41f4b71Sopenharmony_ci testObjName.toString(testObjName.test()); 330e41f4b71Sopenharmony_ci } 331e41f4b71Sopenharmony_ci </script> 332e41f4b71Sopenharmony_ci </body> 333e41f4b71Sopenharmony_ci </html> 334e41f4b71Sopenharmony_ci ``` 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci- Sample code for invoking a callback of the frontend page from the application side: 337e41f4b71Sopenharmony_ci ```ts 338e41f4b71Sopenharmony_ci // xxx.ets 339e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 340e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci class testClass { 343e41f4b71Sopenharmony_ci constructor() { 344e41f4b71Sopenharmony_ci } 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci test(param: Function): void { 347e41f4b71Sopenharmony_ci param("call callback"); 348e41f4b71Sopenharmony_ci } 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci toString(param: String): void { 351e41f4b71Sopenharmony_ci console.log('Web Component toString' + param); 352e41f4b71Sopenharmony_ci } 353e41f4b71Sopenharmony_ci } 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci @Entry 356e41f4b71Sopenharmony_ci @Component 357e41f4b71Sopenharmony_ci struct Index { 358e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 359e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci build() { 362e41f4b71Sopenharmony_ci Column() { 363e41f4b71Sopenharmony_ci Button('refresh') 364e41f4b71Sopenharmony_ci .onClick(() => { 365e41f4b71Sopenharmony_ci try { 366e41f4b71Sopenharmony_ci this.webviewController.refresh(); 367e41f4b71Sopenharmony_ci } catch (error) { 368e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 369e41f4b71Sopenharmony_ci } 370e41f4b71Sopenharmony_ci }) 371e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 372e41f4b71Sopenharmony_ci .onClick(() => { 373e41f4b71Sopenharmony_ci try { 374e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 375e41f4b71Sopenharmony_ci } catch (error) { 376e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 377e41f4b71Sopenharmony_ci } 378e41f4b71Sopenharmony_ci }) 379e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci } 383e41f4b71Sopenharmony_ci ``` 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci ```html 386e41f4b71Sopenharmony_ci <!-- index.html --> 387e41f4b71Sopenharmony_ci <!DOCTYPE html> 388e41f4b71Sopenharmony_ci <html> 389e41f4b71Sopenharmony_ci <body> 390e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 391e41f4b71Sopenharmony_ci <p id="demo"></p> 392e41f4b71Sopenharmony_ci <script> 393e41f4b71Sopenharmony_ci function callArkTS() { 394e41f4b71Sopenharmony_ci testObjName.test(function(param){testObjName.toString(param)}); 395e41f4b71Sopenharmony_ci } 396e41f4b71Sopenharmony_ci </script> 397e41f4b71Sopenharmony_ci </body> 398e41f4b71Sopenharmony_ci </html> 399e41f4b71Sopenharmony_ci ``` 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci- Sample code for calling the function in an object of the frontend page from the application side: 402e41f4b71Sopenharmony_ci ```ts 403e41f4b71Sopenharmony_ci // xxx.ets 404e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 405e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ci class testClass { 408e41f4b71Sopenharmony_ci constructor() { 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci test(param: ESObject): void { 412e41f4b71Sopenharmony_ci param.hello("call obj func"); 413e41f4b71Sopenharmony_ci } 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci toString(param: String): void { 416e41f4b71Sopenharmony_ci console.log('Web Component toString' + param); 417e41f4b71Sopenharmony_ci } 418e41f4b71Sopenharmony_ci } 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci @Entry 421e41f4b71Sopenharmony_ci @Component 422e41f4b71Sopenharmony_ci struct Index { 423e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 424e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci build() { 427e41f4b71Sopenharmony_ci Column() { 428e41f4b71Sopenharmony_ci Button('refresh') 429e41f4b71Sopenharmony_ci .onClick(() => { 430e41f4b71Sopenharmony_ci try { 431e41f4b71Sopenharmony_ci this.webviewController.refresh(); 432e41f4b71Sopenharmony_ci } catch (error) { 433e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 434e41f4b71Sopenharmony_ci } 435e41f4b71Sopenharmony_ci }) 436e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 437e41f4b71Sopenharmony_ci .onClick(() => { 438e41f4b71Sopenharmony_ci try { 439e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 440e41f4b71Sopenharmony_ci } catch (error) { 441e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 442e41f4b71Sopenharmony_ci } 443e41f4b71Sopenharmony_ci }) 444e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 445e41f4b71Sopenharmony_ci } 446e41f4b71Sopenharmony_ci } 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci ``` 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci ```html 451e41f4b71Sopenharmony_ci <!-- index.html --> 452e41f4b71Sopenharmony_ci <!DOCTYPE html> 453e41f4b71Sopenharmony_ci <html> 454e41f4b71Sopenharmony_ci <body> 455e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 456e41f4b71Sopenharmony_ci <p id="demo"></p> 457e41f4b71Sopenharmony_ci <script> 458e41f4b71Sopenharmony_ci // Method 1 459e41f4b71Sopenharmony_ci class Student { 460e41f4b71Sopenharmony_ci constructor(nameList) { 461e41f4b71Sopenharmony_ci this.methodNameListForJsProxy = nameList; 462e41f4b71Sopenharmony_ci } 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci hello(param) { 465e41f4b71Sopenharmony_ci testObjName.toString(param) 466e41f4b71Sopenharmony_ci } 467e41f4b71Sopenharmony_ci } 468e41f4b71Sopenharmony_ci var st = new Student(["hello"]) 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci // Method 2 471e41f4b71Sopenharmony_ci // Create a constructor, with the first letter of the constructor capitalized. 472e41f4b71Sopenharmony_ci function Obj1(){ 473e41f4b71Sopenharmony_ci this.methodNameListForJsProxy=["hello"]; 474e41f4b71Sopenharmony_ci this.hello=function(param){ 475e41f4b71Sopenharmony_ci testObjName.toString(param) 476e41f4b71Sopenharmony_ci }; 477e41f4b71Sopenharmony_ci } 478e41f4b71Sopenharmony_ci // Use the constructor to create an object using the new keyword. 479e41f4b71Sopenharmony_ci var st1 = new Obj1(); 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci function callArkTS() { 482e41f4b71Sopenharmony_ci testObjName.test(st); 483e41f4b71Sopenharmony_ci testObjName.test(st1); 484e41f4b71Sopenharmony_ci } 485e41f4b71Sopenharmony_ci </script> 486e41f4b71Sopenharmony_ci </body> 487e41f4b71Sopenharmony_ci </html> 488e41f4b71Sopenharmony_ci ``` 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci- Sample code for calling the function in an object of the application side from the frontend page: 491e41f4b71Sopenharmony_ci ```ts 492e41f4b71Sopenharmony_ci // xxx.ets 493e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 494e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci class ObjOther { 497e41f4b71Sopenharmony_ci methodNameListForJsProxy: string[] 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci constructor(list: string[]) { 500e41f4b71Sopenharmony_ci this.methodNameListForJsProxy = list 501e41f4b71Sopenharmony_ci } 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci testOther(json: string): void { 504e41f4b71Sopenharmony_ci console.info(json) 505e41f4b71Sopenharmony_ci } 506e41f4b71Sopenharmony_ci } 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci class testClass { 509e41f4b71Sopenharmony_ci ObjReturn: ObjOther 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci constructor() { 512e41f4b71Sopenharmony_ci this.ObjReturn = new ObjOther(["testOther"]); 513e41f4b71Sopenharmony_ci } 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci test(): ESObject { 516e41f4b71Sopenharmony_ci return this.ObjReturn 517e41f4b71Sopenharmony_ci } 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci toString(param: string): void { 520e41f4b71Sopenharmony_ci console.log('Web Component toString' + param); 521e41f4b71Sopenharmony_ci } 522e41f4b71Sopenharmony_ci } 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci @Entry 525e41f4b71Sopenharmony_ci @Component 526e41f4b71Sopenharmony_ci struct Index { 527e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 528e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci build() { 531e41f4b71Sopenharmony_ci Column() { 532e41f4b71Sopenharmony_ci Button('refresh') 533e41f4b71Sopenharmony_ci .onClick(() => { 534e41f4b71Sopenharmony_ci try { 535e41f4b71Sopenharmony_ci this.webviewController.refresh(); 536e41f4b71Sopenharmony_ci } catch (error) { 537e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 538e41f4b71Sopenharmony_ci } 539e41f4b71Sopenharmony_ci }) 540e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 541e41f4b71Sopenharmony_ci .onClick(() => { 542e41f4b71Sopenharmony_ci try { 543e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 544e41f4b71Sopenharmony_ci } catch (error) { 545e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 546e41f4b71Sopenharmony_ci } 547e41f4b71Sopenharmony_ci }) 548e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 549e41f4b71Sopenharmony_ci } 550e41f4b71Sopenharmony_ci } 551e41f4b71Sopenharmony_ci } 552e41f4b71Sopenharmony_ci ``` 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci ```html 555e41f4b71Sopenharmony_ci <!-- index.html --> 556e41f4b71Sopenharmony_ci <!DOCTYPE html> 557e41f4b71Sopenharmony_ci <html> 558e41f4b71Sopenharmony_ci <body> 559e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 560e41f4b71Sopenharmony_ci <p id="demo"></p> 561e41f4b71Sopenharmony_ci <script> 562e41f4b71Sopenharmony_ci function callArkTS() { 563e41f4b71Sopenharmony_ci testObjName.test().testOther("call other object func"); 564e41f4b71Sopenharmony_ci } 565e41f4b71Sopenharmony_ci </script> 566e41f4b71Sopenharmony_ci </body> 567e41f4b71Sopenharmony_ci </html> 568e41f4b71Sopenharmony_ci ``` 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci- Examples of using a promise:<br> 571e41f4b71Sopenharmony_ci With the first method, a promise is created on the application side. 572e41f4b71Sopenharmony_ci ```ts 573e41f4b71Sopenharmony_ci // xxx.ets 574e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 575e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci class testClass { 578e41f4b71Sopenharmony_ci constructor() { 579e41f4b71Sopenharmony_ci } 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci test(): Promise<string> { 582e41f4b71Sopenharmony_ci let p: Promise<string> = new Promise((resolve, reject) => { setTimeout(() => {console.log('Execution completed'); reject('fail');}, 10000);}); 583e41f4b71Sopenharmony_ci return p; 584e41f4b71Sopenharmony_ci } 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci toString(param:String): void { 587e41f4b71Sopenharmony_ci console.log(" " + param); 588e41f4b71Sopenharmony_ci } 589e41f4b71Sopenharmony_ci } 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci @Entry 592e41f4b71Sopenharmony_ci @Component 593e41f4b71Sopenharmony_ci struct Index { 594e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 595e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ci build() { 598e41f4b71Sopenharmony_ci Column() { 599e41f4b71Sopenharmony_ci Button('refresh') 600e41f4b71Sopenharmony_ci .onClick(() => { 601e41f4b71Sopenharmony_ci try { 602e41f4b71Sopenharmony_ci this.webviewController.refresh(); 603e41f4b71Sopenharmony_ci } catch (error) { 604e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 605e41f4b71Sopenharmony_ci } 606e41f4b71Sopenharmony_ci }) 607e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 608e41f4b71Sopenharmony_ci .onClick(() => { 609e41f4b71Sopenharmony_ci try { 610e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 611e41f4b71Sopenharmony_ci } catch (error) { 612e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 613e41f4b71Sopenharmony_ci } 614e41f4b71Sopenharmony_ci }) 615e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 616e41f4b71Sopenharmony_ci } 617e41f4b71Sopenharmony_ci } 618e41f4b71Sopenharmony_ci } 619e41f4b71Sopenharmony_ci ``` 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci ```html 622e41f4b71Sopenharmony_ci <!-- index.html --> 623e41f4b71Sopenharmony_ci <!DOCTYPE html> 624e41f4b71Sopenharmony_ci <html> 625e41f4b71Sopenharmony_ci <body> 626e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 627e41f4b71Sopenharmony_ci <p id="demo"></p> 628e41f4b71Sopenharmony_ci <script> 629e41f4b71Sopenharmony_ci function callArkTS() { 630e41f4b71Sopenharmony_ci testObjName.test().then((param)=>{testObjName.toString(param)}).catch((param)=>{testObjName.toString(param)}) 631e41f4b71Sopenharmony_ci } 632e41f4b71Sopenharmony_ci </script> 633e41f4b71Sopenharmony_ci </body> 634e41f4b71Sopenharmony_ci </html> 635e41f4b71Sopenharmony_ci ``` 636e41f4b71Sopenharmony_ci With the first method, a promise is created on the frontend page. 637e41f4b71Sopenharmony_ci ```ts 638e41f4b71Sopenharmony_ci // xxx.ets 639e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 640e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci class testClass { 643e41f4b71Sopenharmony_ci constructor() { 644e41f4b71Sopenharmony_ci } 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci test(param:Function): void { 647e41f4b71Sopenharmony_ci setTimeout( () => { param("suc") }, 10000) 648e41f4b71Sopenharmony_ci } 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci toString(param:String): void { 651e41f4b71Sopenharmony_ci console.log(" " + param); 652e41f4b71Sopenharmony_ci } 653e41f4b71Sopenharmony_ci } 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_ci @Entry 656e41f4b71Sopenharmony_ci @Component 657e41f4b71Sopenharmony_ci struct Index { 658e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 659e41f4b71Sopenharmony_ci @State testObj: testClass = new testClass(); 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci build() { 662e41f4b71Sopenharmony_ci Column() { 663e41f4b71Sopenharmony_ci Button('refresh') 664e41f4b71Sopenharmony_ci .onClick(() => { 665e41f4b71Sopenharmony_ci try { 666e41f4b71Sopenharmony_ci this.webviewController.refresh(); 667e41f4b71Sopenharmony_ci } catch (error) { 668e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 669e41f4b71Sopenharmony_ci } 670e41f4b71Sopenharmony_ci }) 671e41f4b71Sopenharmony_ci Button('Register JavaScript To Window') 672e41f4b71Sopenharmony_ci .onClick(() => { 673e41f4b71Sopenharmony_ci try { 674e41f4b71Sopenharmony_ci this.webviewController.registerJavaScriptProxy(this.testObj, "testObjName", ["test", "toString"]); 675e41f4b71Sopenharmony_ci } catch (error) { 676e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 677e41f4b71Sopenharmony_ci } 678e41f4b71Sopenharmony_ci }) 679e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.webviewController }) 680e41f4b71Sopenharmony_ci } 681e41f4b71Sopenharmony_ci } 682e41f4b71Sopenharmony_ci } 683e41f4b71Sopenharmony_ci ``` 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci ```html 686e41f4b71Sopenharmony_ci <!-- index.html --> 687e41f4b71Sopenharmony_ci <!DOCTYPE html> 688e41f4b71Sopenharmony_ci <html> 689e41f4b71Sopenharmony_ci <body> 690e41f4b71Sopenharmony_ci <button type="button" onclick="callArkTS()">Click Me!</button> 691e41f4b71Sopenharmony_ci <p id="demo"></p> 692e41f4b71Sopenharmony_ci <script> 693e41f4b71Sopenharmony_ci function callArkTS() { 694e41f4b71Sopenharmony_ci let funpromise 695e41f4b71Sopenharmony_ci var p = new Promise(function(resolve, reject){funpromise=(param)=>{resolve(param)}}) 696e41f4b71Sopenharmony_ci testObjName.test(funpromise) 697e41f4b71Sopenharmony_ci p.then((param)=>{testObjName.toString(param)}) 698e41f4b71Sopenharmony_ci } 699e41f4b71Sopenharmony_ci </script> 700e41f4b71Sopenharmony_ci </body> 701e41f4b71Sopenharmony_ci </html> 702e41f4b71Sopenharmony_ci ``` 703