微信公众号网页开发监控手机后退并关闭页面到主页
2017-09-16 15:24
281 查看
这个部分需要用到三个东西:
history.pushState
window.onpopstate
微信内置浏览器JS:
你可以在最后一个(需要点击返回退出浏览器的)页面载入的时候借助
然后监听返回按钮的点击事件(事实上就是出发了浏览器的历史记录变动事件),当监听到用户点击返回按钮时执行类似下面的函数,调用微信内置JS函数
java ee开发 页面如下
点击手机的后退按钮,则关闭该页面到公众号主页
history.pushState
window.onpopstate
微信内置浏览器JS:
WeixinJSBridge.call('closeWindow');
你可以在最后一个(需要点击返回退出浏览器的)页面载入的时候借助
history.pushState保存下两个状态:
history.pushState({page : 'state1'},'state','#state1'); history.pushState({page : 'state2'},'state','#state2');
然后监听返回按钮的点击事件(事实上就是出发了浏览器的历史记录变动事件),当监听到用户点击返回按钮时执行类似下面的函数,调用微信内置JS函数
WeixinJSBridge.call('closeWindow');关闭浏览器,即可完成用户点击返回按钮退出微信浏览器的操作。:
window.onpopstate = function(event) { if (event.state.page === 'state1') { WeixinJSBridge.call('closeWindow'); } }
java ee开发 页面如下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE HTML>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<base href="<%=basePath%>">
<html lang="zn-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>出错了</title>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<link rel="stylesheet"href="css/service.css">
<script type="text/javascript" src="js/exit.js"></script>
</head>
<script type="text/javascript">
history.pushState({page : 'state1'},'state','#state1'); history.pushState({page : 'state2'},'state','#state2');
window.onpopstate = function(event) { if (event.state.page === 'state1') { WeixinJSBridge.call('closeWindow'); } }
</script>
<body>
<div class='nonetwork'>
<img src='img/nonetwork.png'>
<h3>抱歉!当前会话已过期</h3>
<h3>请重新打开页面</h3>
</div>
</body>
</html>
点击手机的后退按钮,则关闭该页面到公众号主页
相关文章推荐
- 【微信公众号发红包转账】微信公众号上手机网页接收请求,通过公众号给用户发红包 开发流程
- 微信公众号开发——网页授权 (页面分享 、获取用户基本信息)
- 微信公众号开发网页授权获得OPENID的过滤器
- 谈谈关于前端开发网页适配手机的相关设置
- IE8打开某个页面提示“Internet Explorer已经为了帮助保护您的计算机而关闭此网页”的解决方法
- 手机网页Html代码实现(解决显示页面很小的问题)
- jQuery Mobile 手机网页开发的 两个问题
- 微信公众号开发 (微信网页开发)
- 微信H5手机网页开发—快速入门
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 如何调试手机网页页面
- 查岗:两个60后海归博士开发的手机监控应用
- 手机浏览网页页面缩放
- h5页面,关闭手机键盘
- 微信公众号 对接网页开发
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 开发手机Widget关于页面css渲染的一点心得
- 网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放
- 手机网页开发问题总结(一)
- 微信公众号开发教程[015]-网页开发-网页授权