参考demo:import { webview } from '@kit.ArkWeb'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController(); @State flag:boolean = true build() { Column() { if (this.flag) { Text('显示') } Web({ src: $rawfile('index5.html'), controller: this.controller }) .javaScriptAccess(true) .domStorageAccess(true) .onLoadIntercept((event) => { if (event.data.getRequestUrl() === 'xxx') { this.flag = false } else { this.flag = true } return false; }) }.height('100%').width('100%') } }<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <button id="init" onclick="jump()">跳转</button> </body> </html> <script> function jump() { window.location.href = 'xxx' } </script>
参考demo: