H5 应用程序返回button的js代码设计,设计仿stack
2015-09-15 21:17
429 查看
history.back();
该代码具有天然的缺陷,二手知道,于H5应用,尤其是模仿移动应用程序时,,这是不够。
在放大期js为了实现类似特征,请轻喷。
不多说,上代码:
/** * Created by piggsoft@163.com on 2015/5/28. */ var historyUtils = { add : function (url) { var historyArray = historyUtils.getLocal(); if (!historyArray) { historyArray = []; } var currentPage = historyArray.pop(); if (currentPage && currentPage == url) { //do nothing } else if (currentPage){ historyArray.push(currentPage); //历史里面没有如今传入的url。在加回去 } historyArray.push(url); historyUtils.saveLocal(historyArray); }, back : function() { var historyArray = historyUtils.getLocal(); var currentPage = historyArray.pop();//去掉当前页面,pop取最后,相似stack var history = historyArray.pop(); if (!history) {//没有历史页面 historyUtils.add(currentPage);//将当前页面增加回数组中 return; } historyUtils.saveLocal(historyArray); window.location.href = history; }, getLocal : function() { var result = window.sessionStorage.getItem(historyUtils.key); if (!result) { return null; } return JSON.parse(result); }, saveLocal : function(data) { window.sessionStorage.setItem(historyUtils.key, JSON.stringify(data)); }, init : function() { historyUtils.saveLocal([]); }, key : "_history_" } historyUtils.add(window.location.href);
在须要实现back的地方调用
historyUtils.back();
相关文章推荐
- JavaScript 事件
- js中使用正则表达式(一)创建正则表达式的方式:正则对象方式和正则字面量方式
- jsp九大内置对象
- JSON数据的解析
- servlet&jsp的设计模式
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- 浅谈JS闭包
- jsp中session的生命周期
- jsp的4个作用域
- javascript中的function
- jsp的九大内置对象
- javascript判断非空
- JSP之Session生命周期
- Js按键调到文本框
- JSP的四大作用域
- 自己的JS框架--Amy框架。
- js中的各种“位置”——“top、clientTop、scrollTop、offsetTop……”,你知道多少
- JS身份证验证
- javascript函数科里化
- JS模块加载