ajax后退解决方案(四)

目录

 

三、通过修改location.hash产生历史,hashchange事件处理后退

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>0</title>
</head>
<body>
	<input type="button" value="加1" onclick="add()" />
	<div id="info" style="border:red 1px solid;width:200px;padding:10px;">0</div>
</body>
</html>
<script src="history-0.4.js"></script>
<script>
	var info = document.getElementById('info');
	var i = 1;
	function add() {
		var data = {
		    param : i,
		    func : func
		};
		History.push(data);
		i++;
	}
	
	History.push({param:0, func: func});
	
	function func(i) {
	    info.innerHTML = i;
	    document.title = i;
	}
</script>

  

每次ajax操作去改变location.hash的值,每次修改后地址栏均会有所体现。在window.onhashchange中监听该事件。通过hash的值取对应的历史。

较iframe的好处是可以复制地址栏的url直接进入该历史记录,缺点是IE6/7不支持。

 

history-0.4.js

相关:

http://msdn.microsoft.com/en-us/library/cc288209(v=vs.85).aspx

https://developer.mozilla.org/en/DOM/window.onhashchange

http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#event-hashchange

posted on 2011-09-20 07:51  snandy  阅读(8795)  评论(5编辑  收藏  举报