HarmonyOS webview内部history问题

webview内使用location.href导航后,window.history没有增加记录,导致无法返回。

HarmonyOS
2024-09-05 10:25:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

web内回退示例demo。

import webview from '@ohos.web.webview'; 
import { BusinessError } from '@ohos.base'; 
import router from '@ohos.router'; 
 
@Entry 
@Component 
struct WebViewPage { 
  @State canBackward: boolean = false; 
  @State canForward: boolean = false; 
  controller: webview.WebviewController = new webview.WebviewController(); 
 
  build() { 
    Row() { 
      Column() { 
        Button('Back').onClick(() => { 
          router.back(); 
        }); 
 
 
        Row() { 
          if (this.canBackward) { 
            Button('web内回退').onClick(() => { 
              this.controller.backOrForward(-1); 
            }); 
          } 
          if (this.canForward) { 
            Button('web内前进').onClick(() => { 
              this.controller.backOrForward(1); 
            }); 
          } 
        } 
 
        Web({ src: $rawfile('index4.html'), controller: this.controller }) 
          .javaScriptAccess(true) 
          .fileAccess(true) 
          .backgroundColor(Color.Grey) 
          .onConsole(event => { 
            console.warn('...onConsole: ' + event?.message.getMessage()); 
            return true; 
          }) 
          .onTitleReceive((event) => { 
            //两个方案 
            let list = this.controller.getBackForwardEntries(); 
            // this.canBackward = list.currentIndex > 0; 
            // this.canForward = list.currentIndex < list.size - 1; 
            this.canBackward = this.controller.accessStep(-1); 
            this.canForward = this.controller.accessStep(1); 
            console.warn('...WebViewPage onTitleReceive, accessBackward:' + this.canBackward); 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
<html> 
  <meta charset="utf-8"> 
  <body> 
  <div style="margin: 100 0 0 200;"> 
  <button type="button" onclick="goNewWeb()">location.href跳转页面</button> 
  </div> 
  <div style="margin: 40 0 0 200;"> 
  <a href="./index.html">a标签跳转页面</a> 
  </div> 
 
  <div style="margin: 40 0 0 200;">Hello, index4.html</div> 
 
  </body> 
  <script type="text/javascript"> 
  function goNewWeb(){ 
    console.warn('...index4 goNewWeb'); 
    location.href = './index.html'; 
  } 
  </script> 
 
  </html>
分享
微博
QQ
微信
回复
2024-09-05 18:26:30
相关问题
HarmonyOS h5 window.history 无效
405浏览 • 1回复 待解决
HarmonyOS 调用内部资源问题
552浏览 • 1回复 待解决
HarmonyOS UI内部使用AppStorage问题
798浏览 • 1回复 待解决
HarmonyOS webView代理问题
469浏览 • 1回复 待解决
HarmonyOS webview通信问题
413浏览 • 1回复 待解决
HarmonyOS 企业内部应用问题咨询
393浏览 • 1回复 待解决
HarmonyOS webview加载数据问题
451浏览 • 1回复 待解决
HarmonyOS webview内存占用问题
374浏览 • 1回复 待解决
HarmonyOS webView种植cookie问题
636浏览 • 1回复 待解决
HarmonyOS webview刷新问题
418浏览 • 2回复 待解决
HarmonyOS webview_flutter问题
260浏览 • 1回复 待解决
HarmonyOS webview跨域问题
1136浏览 • 1回复 待解决
HarmonyOS 企业内部App上架问题
586浏览 • 1回复 待解决
HarmonyOS 企业内部应用升级问题咨询
436浏览 • 1回复 待解决
webview 问题有哪些?
709浏览 • 1回复 待解决
HarmonyOS webview打不开的问题
425浏览 • 1回复 待解决
HarmonyOS webview组件跨域问题
358浏览 • 1回复 待解决
HarmonyOS webview加载本地html问题
1214浏览 • 1回复 待解决
HarmonyOS 折叠屏webview宽度问题
766浏览 • 1回复 待解决
HarmonyOS 企业内部应用分发问题咨询
462浏览 • 1回复 待解决
HarmonyOS Webview load本地资源的问题
216浏览 • 1回复 待解决