好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Html5 监听拦截Android返回键方法

浏览器窗口有一个history对象,用来保存浏览历史。

如果当前窗口先后访问了三个网址,那么history对象就包括三项,history.length属性等于3。

history对象提供了一 系列 方法,允许在浏览历史之间移动:

window.history.back():移动到上一个访问页面,等同于浏览器的后退键。

window.history.forward():移动到下一个访问页面,等同于浏览器的前进键。

window.history.go(num):接受一个整数作为参数,移动到该整数指定的页面,比如go(1)相当于forward(),go(-1)相当于back()。

window.history.push stat e():HT ML 5为history对象添加了两个新方法,window.history.pushState()和window.history.replaceState(),用来在浏览历史中添加和修改记录。

注:1.如果移动的位置超出了访问历史的边界,以上三个方法并不报错,而是默默的失败。

2.设置时,页面通常是从浏览器缓存之中加载,而不是重新要求 服务器 发送新的网页。

重点 讲解 下: window. history.pushState()

window.history.pushState(state, t IT le, utl),在页面中创建一个 history 实体。直接添加到 历史记录 中。

其中参数:

state:一个与指定网址相关的状态对象,popstate事件触发时,该对象会传入回调函数。如果不需要这个对象,此处可以填null。

title:新页面的标题,但是所有浏览器目前都忽略这个值,因此这里可以填null。

url:新的网址, 必须与当前页面处在同一个域 。浏览器的地址栏将显示这个网址。

注:pushState方法不会触发页面刷新,只是导致history对象发生变化,地址栏会有反应。

举例实现:

Html5 监听拦截And ROI d返回键方法如下:

1. 监听 popstate 事件

window.addEventListener("popstate", function(){
    // DOS omething
}, false)

2.取消默认的返回操作,即监听拦截返回键:添加一条空的 history 实体作为替代 原来 的 history 实体

window.history.pushState(null, null, " # ");

举例:

<!DOCTY PE  html>
<html>
  < ;m eta n am e="viewport" content="width=device-width">
  <script type="text/javascript">
      VAR  count = 0 ;
     window.history.pushState(null, null, "#");
     window.addEventListener("popstate", function(e) {
        window.history.pushState(null, null, "#");
        document.getElementById(' LOG View').innerHTML = "用户点击返回" + (++count)
     })
  </script>
<body>
  <p id="logView"> test </p>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

总结

以上是 为你收集整理的 详解Html5 监听拦截Android返回键方法 全部内容,希望文章能够帮你解决 详解Html5 监听拦截Android返回键方法 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于详解Html5 监听拦截Android返回键方法的详细内容...

  阅读:33次