首页 > 其他分享 >HarmonyOS 管理页面跳转及浏览记录导航

HarmonyOS 管理页面跳转及浏览记录导航

时间:2023-09-19 16:00:11浏览次数:33  
标签:web webviewController HarmonyOS call 跳转 webview 页面

 

历史记录导航

使用者在前端页面点击网页中的链接时,Web组件默认会自动打开并加载目标网址。当前端页面替换为新的加载链接时,会自动记录已经访问的网页地址。可以通过forward()backward()接口向前/向后浏览上一个/下一个历史记录。

在下面的示例中,点击应用的按钮来触发前端页面的后退操作。

 

// xxx.ets
import web_webview from '@ohos.web.webview';

@Entry
@Component
struct WebComponent {
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();
  build() {
    Column() {
      Button('loadData')
        .onClick(() => {
          if (this.webviewController.accessBackward()) {
            this.webviewController.backward();
            return true;
          }
        })
      Web({ src: 'https://www.example.com/cn/', controller: this.webviewController})
    }
  }
}
 

  

如果存在历史记录,accessBackward()接口会返回true。同样,您可以使用accessForward()接口检查是否存在前进的历史记录。如果您不执行检查,那么当用户浏览到历史记录的末尾时,调用forward()backward()接口时将不执行任何操作。

页面跳转

当点击网页中的链接需要跳转到应用内其他页面时,可以通过使用Web组件的onUrlLoadIntercept()接口来实现。

在下面的示例中,应用首页Index.ets加载前端页面route.html,在前端route.html页面点击超链接,可跳转到应用的ProfilePage.ets页面。

● 应用首页index.ets页面代码。

 

// index.ets
import web_webview from '@ohos.web.webview';
import router from '@ohos.router';
@Entry
@Component
struct WebComponent {
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();

  build() {
    Column() {
      Web({ src: $rawfile('route.html'), controller: this.webviewController })
        .onUrlLoadIntercept((event) => {
          let url: string = event.data as string;
          if (url.indexOf('native://') === 0) {
            // 跳转其他界面
            router.pushUrl({ url:url.substring(9) })
            return true;
          }
          return false;
        })
    }
  }
}
 

  

● route.html前端页面代码。

 

<!-- route.html -->
<!DOCTYPE html>
<html>
<body>
  <div>
      <a href="native://pages/ProfilePage">个人中心</a>
   </div>
</body>
</html>
 

  

● 跳转页面ProfilePage.ets代码。

 

@Entry
@Component
struct ProfilePage {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Text(this.message)
        .fontSize(20)
    }
  }
}
 

  

跨应用跳转

Web组件可以实现点击前端页面超链接跳转到其他应用。

在下面的示例中,点击call.html前端页面中的超连接,跳转到电话应用的拨号界面。

● 应用侧代码。

 

// xxx.ets
import web_webview from '@ohos.web.webview';
import call from '@ohos.telephony.call';

@Entry
@Component
struct WebComponent {
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();

  build() {
    Column() {
      Web({ src: $rawfile('xxx.html'), controller: this.webviewController})
        .onUrlLoadIntercept((event) => {
          let url: string = event.data as string;
          // 判断链接是否为拨号链接
          if (url.indexOf('tel://') === 0) {
            // 跳转拨号界面
            call.makeCall(url.substring(6), (err) => {
              if (!err) {
                console.info('make call succeeded.');
              } else {
                console.info('make call fail, err is:' + JSON.stringify(err));
              }
            });
            return true;
          }
          return false;
        })
    }

  

● 前端页面call.html代码。

 

<!-- call.html -->
<!DOCTYPE html>
<html>
<body>
  <div>
    <a href="tel://xxx xxxx xxx">拨打电话</a>
  </div>
</body>
</html>

  

标签:web,webviewController,HarmonyOS,call,跳转,webview,页面
From: https://www.cnblogs.com/HarmonyOSDev/p/17714895.html

相关文章

  • SSSRF-302跳转 Bypass
    302跳转Bypass题目描述:SSRF中有个很重要的一点是请求可能会跟随302跳转,尝试利用这个来绕过对IP的检测访问到位于127.0.0.1的flag.php吧!  这道题绕过的方式有很多,这里先讲讲302跳转,302跳转就是由一个URL跳转到另外一个URL当中去,就好比现实生活中的呼叫转移,在网页中比如一个网......
  • destoon上做纯js实现html指定页面导出word
    因为最近做了范文网站需要,所以要下载为word文档,如果php进行处理,很吃后台服务器,所以想用前端进行实现。查询github发现,确实有这方面的插件。js导出word文档所需要的两个插件:FileSaver.jsjquery.wordexport.js首先引入:<!--生成word!--><scriptsrc="https://cdn.bootcss......
  • HttpClient采集页面数据
    1、导入相关依赖<!--https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client--><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.3.0</version&......
  • 2023-09-18 hexo博客之如何自定义页面内容宽度==》在custom.styl中添加两行代码即可
    前言:我的hexo主题为hexo-theme-next 5.1.4版本。操作如下:打开你的博客名称\themes\hexo-theme-next\source\css\_variables,找到这个文件custom.styl,然后把下面代码添加进去:$main-desktop=1200px$content-desktop=1000px刷新页面即可见效。......
  • 如何使用谷歌搜索的时候,不是从当前页面而是从新页面打开链接?
    参考链接:https://support.google.com/chrome/thread/3520860/how-do-i-set-chrome-to-open-links-in-a-new-tab-on-the-same-browser-window?hl=en1.使用ctrl+左键点击链接2.在Google主界面进行更改进入主界面https://www.google.com/webhp,点击下方的设置选择其中的搜索设置......
  • html前端页面多规格商品sku选择
    <style>body{background-color:palegoldenrod;position:relative;}footer{border:1pxsolidred;height:50px;position:fixed;bottom:0;left:0;width:100%;}.btn{padding:015px;height:35px;line-hei......
  • 当页面中文本不允许选择时,使文本框中文本可以选择的js代码
    <bodyonselectstart="returnoSelect(event.srcElement);"><scriptlanguage="javascript">functionoSelect(obj){if(obj.type!='text')returnfalse;}</script><inputtype="text"name=&quo......
  • 按下按钮后页面的滚动条向下(或向上)滑动,松开之后便停留在当前位置的效果如何实现?
    网友问题?客户要求做一个框架,按下小框架页面中的“上翻”或者“下翻”按钮后,大框架页面便随之向上(或向下)滚动,松开按钮滚动停止,并停留在当前位置,如何解决?解决方案如下:-----------------mm.htm:-----------------<framesetrows="*,20%"><framename="main"src='liu.htm'target="fo......
  • 页面输出太多会严重影响web程序的性能
    我有这样一个小程序:asp+sqlserver2000。数据量增加的很快,最近发现它的性能非常差,每次打开都需要十几秒,甚至几十秒,因为我的程序分页用的是我自己的分页程序:难道这个分页程序有问题,但是其他地方用到它没有感觉到慢呀,我没事就琢磨他,到网上查资料,结果没有具体查到说到我......
  • TienChin 渠道管理-渠道页面完善
    最后附上渠道管理的数据installSQL语句:INSERTINTOTienChin.tienchin_channel(channel_id,channel_name,status,remark,type,create_by,update_by,create_time,update_time,del_flag)VALUES(3,'小红书渠道',1,'小红书渠道',1,'qudao','qu......