当前位置: 首页 > 图文教程 > 网络编程 > PHP > 结合AJAX的PHP开发之后退、前进和刷新(2)
编写类
我们来看看历史堆栈中需要存储的数据或属性。前面已经讨论了堆栈(数组)和指针。stack_limit 属性可以防止因为数据过多而造成的 cookie 溢出(参见清单 1)。在实践中,我们希望在删除最老的记录之前能够存储 40-50 个事件。出于测试的目的,我们将该值设置为 15。
清单 1. 历史堆栈的构造,包括类的属性
| function HistoryStack () { this.stack = new Array(); this.current = -1; this.stack_limit = 15; } |
| HistoryStack.prototype.addResource = function(resource) { if (this.stack.length > 0) { this.stack = this.stack.slice(0, this.current + 1); } this.stack.push(resource); while (this.stack.length > this.stack_limit) { this.stack.shift(); } this.current = this.stack.length - 1; this.save(); }; |
| HistoryStack.prototype.addResource = function(resource) HistoryStack.prototype.getCurrent = function () { return this.stack[this.current]; }; HistoryStack.prototype.hasPrev = function() { return (this.current > 0); }; HistoryStack.prototype.hasNext = function() { return (this.current < this.stack.length - 1 && this.current > -1); }; |
| HistoryStack.prototype.go = function(increment) { // Go back... if (increment < 0) { this.current = Math.max(0, this.current + increment); // Go forward... } else if (increment > 0) { this.current = Math.min(this.stack.length - 1,this.current + increment); // Reload... } else { location.reload(); } this.save(); }; |
| HistoryStack.prototype.setCookie = function(name, value) { var cookie_str = name + "=" + escap" |