页面跳转,返回

(function(win){
	var TabBarPlug=function(options){
		this.barTitle='DEMOBAR';
	}
	TabBarPlug.prototype={
		//获取浏览过的地址
		getSessionUrl: function(){
			return JSON.parse(sessionStorage.getItem('historyUrl') || '[]');
		},
		//存储浏览历史地址
		setSessionUrl: function (source) {
			sessionStorage.setItem('historyUrl', JSON.stringify(source || []));
			return true;
		},
		//测试
		setBarTitle: function(pageUrl){
			pageUrl=pageUrl.toLowerCase();
			if(pageUrl.indexOf('a.html')>-1){
				this.barTitle='DEMOA';
			}
			if(pageUrl.indexOf('b.html')>-1){
				this.barTitle='DEMOB';
			}
		},
		//当前页面和浏览历史最后一条匹配
		isCurPage: function (urls) {
			var curUrl = location.href;
			if (!urls) {
				urls = [];
			}
			var lastUrl = urls[urls.length - 1];
			if (lastUrl == curUrl) {
				return true;
			} else {
				return false;
			}
		},
		init: function(){
			var historyUrl = this.getSessionUrl();
			var curUrl = location.href;
			this.setBarTitle(curUrl);
			//因为当执行history.back后,当前页面会在刷新一次,这时候是不需要加入浏览记录的
			if (!this.isCurPage(historyUrl)) {
				historyUrl.push(curUrl);
				this.setSessionUrl(historyUrl);//存储浏览记录
			}
		},
		go: function (e) {
			e ? e : window.event;
			e.preventDefault();
			e.stopPropagation();
			var historyUrl = this.getSessionUrl();
			var that = this;
			var backApp = function () {
				sessionStorage.removeItem('historyUrl');//移除session
				win.close();
			};
			var back = function () {
				//IE history.length=0 其他:fixfox、gg history.length=1
				if (that.isCurPage(historyUrl)) {//防止用户多次点击
				  historyUrl.pop();//删除浏览地址
				  that.setSessionUrl(historyUrl);//存储浏览记录
				  //发现在app内嵌的页面需要延迟执行,否则会出现连续关闭两个页面的情况
				  setTimeout(function () {
				    history.back();//回退
				  }, 500);
				  //回退app
				  if (historyUrl.length <= 0) {
				    backApp();
				  }
				} 
			};
			console.log(historyUrl);
			back();
		}
	}
	win.TabBarPlug=new TabBarPlug();
})(window);	
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<title>a</title>
		<style type="text/css">
			*{ margin:0; padding:0; }
			body{ height: 100%; font-size: 14px; color:#fff; background: #222; }
			#back{ font-size: 20px; color: red; }
			
		</style>
	</head>
	<body>
		
		<div class="box">
			lldsjfls<br>
			lsdjfls<br>
			<a href="b.html">跳转b页面</a>
			lsdjfls<br>
			lsdjfls<br>
			<div id="back"> click me to back</div>
		</div>
		<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
		<script src="js/tabBar.js"></script>
		<script type="text/javascript">
			TabBarPlug.init();//每个页面需要加入这个初始化记录浏览地址
			document.getElementById('back').addEventListener('click', function(e){
			    TabBarPlug.go(e);//执行回退
			});
		</script>

	</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值