美文网首页
vue+vant浏览器回退时弹窗提示消息,去掉域名显示

vue+vant浏览器回退时弹窗提示消息,去掉域名显示

作者: 前端蜗牛老师 | 来源:发表于2020-11-25 10:46 被阅读0次

场景描述:再做移动端时,比如下单跳到支付页面,当用户不愿意付钱,点回退按钮时,如果此时给一个弹窗提示,挽留用户,会很大程度上提高支付率。

技术: vue+vant 框架开发(不管是啥框架,下面方法都行)
在用vant框架的dialog组件时,有个坑,就是第一次进入页面,点回退会出来弹窗,然后取消弹窗,再次点击回退就会有问题,不回出来弹窗。再点 回退就会返回上一个页面。开始以为时window.history.pushState('forward', null, document.URL)有问题因此再多次尝试无果放弃,采用原生弹窗confirm的方式写。
代码如下

mounted() {
  if (window.history && window.history.pushState) {
     window.history.pushState('forward', null, document.URL)
     window.addEventListener('popstate', this.cancelFun, false)
   }
}
cancelFun() {
     this.reConfirm('当前有未完成的订单,确认退出支付吗?')
   },
   reConfirm(message) {
     const wConfirm = window.confirm
     window.confirm = function(message) {
       try {
         const iframe = document.createElement('IFRAME')
         iframe.style.display = 'none'
         iframe.setAttribute('src', 'data:text/plain,')
         document.documentElement.appendChild(iframe)
         const alertFrame = window.frames[0]
         let iwindow = alertFrame.window
         if (iwindow === undefined) {
           iwindow = alertFrame.contentWindow
         }
         const result = iwindow.confirm(message)
         iframe.parentNode.removeChild(iframe)
         return result
       } catch (exc) {
         return wConfirm(message)
       }
     }
     const r = window.confirm(message)
     if (r === true) {
       this.$router.go(-1)
     } else {
       window.history.pushState('forward', null, document.URL)
     }
   }

是原生弹框,效果挺好///
qq交流群:148229086
又帮到你的话,点赞,关注,评论,谢谢啦😄😄😄
(https://www.jianshu.com/u/5c516e381a3a)
公众号: 程序员蜗牛(有前端等编程资料分享)
github主页: https://github.com/websmallrabbit

相关文章

网友评论

      本文标题:vue+vant浏览器回退时弹窗提示消息,去掉域名显示

      本文链接:https://www.haomeiwen.com/subject/oxggiktx.html