当前位置: 首页 > 图文教程 > 网络编程 > PHP > 结合AJAX的PHP开发之后退、前进和刷新(4)
在第 1 部分中,相册中的每个链接都是由 get_table_link() 和 get_image_link() 两个函数生成的。通过编辑这些函数,可以在调用 Sajax 函数之前让该函数先调用历史堆栈。清单 9 以粗体显示了这些变化。
清单 9. get_table_link() 和 get_image_link() 函数的更新版本
| function get_table_link ( $title, $start, $step ) { $link = "myHistory.addResource('table-$start-$step'); " ."x_get_table($start, $step, to_window); " ."return false;"; return '<a href="#" onclick="' . $link . '">' . $title.'</a>'; } function get_image_link ( $title, $index ) { $link = "myHistory.addResource('image-$index'); " ."x_get_image($index, to_window); " ."return false;"; return '<a href="#" onclick="' . $link . '">' . $title .'</a>'; } |
| display_history_buttons(); |
| function display_history_buttons() { var str = ''; if (myHistory.hasPrev()) { str += '<a href="#" onclick="do_back(); return false;"> <img src="icons/back_on.gif" alt="Back" /></a>'; } else { str += '<img src="icons/back_off.gif" alt="" />'; } if (myHistory.hasNext()) { str += '<a href="#" onclick="do_forward(); return false;"> <img src="icons/forward_on.gif" alt="Forward" /></a>'; } else { str += '<img src="icons/forward_off.gif" alt="" />'; } str += '<a href="#" onclick="do_reload(); return false;"> <img src="icons/reload.gif" alt="Reload" /></a>'; document.getElementById("historybuttons").innerHTML = str; } |
| function load_current() { // No existing history. if (myHistory.stack.length == 0) { x_get_table(to_window); myHistory.addResource('table-0-5'); // Load from history. } else { var current = myHistory.getCurrent(); var params = current.split('-'); if (params[0] == 'table') { x_get_table(params[1], params[2], to_window); } else if (params[0] == 'image') { x_get_image(params[1], to_window); } } } |
| window.onload = function () { load_current(); }; |
| function do_back() { myHistory.go(-1); load_current(); } function do_forward() { myHistory.go(1); load_current(); } function do_reload() { myHistory.go(0); } |