- windows XP |
- windows vista |
- windows 2000 |
- windows 2003 |
- windows 2008 |
- linux |
- unix |
- mac
- access |
- mysql |
- sql server |
- oracle |
- DB2
- word |
- excel |
- powerpoint |
- wps |
测试类
可以用简单的 HTML 页面和一些 JavaScript 来测试完成的类。测试页面将在上方显示历史记录按钮,只有活动的按钮是突出显示并且可以单击的。我们没有建立复杂的测试应用程序,该页面在每次单击链接时仅仅生成随机数。这些数字就是记录到历史堆栈中的事件。堆栈也在页面上显示,指针标记的当前记录用粗体显示。
清单 7. 测试历史堆栈的简单 HTML 页面
| <html> <head> <title></title> </head> <body> <div id="historybuttons"></div> <div> <a href="#" onclick="do_add(); return false;">Add Random Resource</a> </div> <div id="output" style="margin-top:40px;"></div> </body> </html> |
| <script type="text/javascript" src="history.js"></script> <script type="text/javascript"> var myHistory = new HistoryStack(); myHistory.load(); function do_add() { var num = Math.round(Math.random() * 1000); myHistory.addResource(num); display(); return false; } function do_back() { myHistory.go(-1); display(); } function do_forward() { myHistory.go(1); display(); } function do_reload() { myHistory.go(0); } function display() { // 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; // Display the current history stack, highlighting the current // position. var str = '<div>History:</div>'; for (i=0; i < myHistory.stack.length; i++) { if (i == myHistory.current) { str += '<div><b>' + myHistory.stack[i] + '</b></div>'; } else { str += '<div>' + myHistory.stack[i] + '</div>'; } } document.getElementById("output").innerHTML = str; } window.onload = function () { display(); }; </script> |
图 2. 历史堆栈的测试页面 |
| <div id="historybuttons"></div> |
| <script type="text/javascript" src="history.js"></script> |
| var myHistory = new HistoryStack(); myHistory.load(); |
在针对历史堆栈的测试应用程序中,只存储随机数作为事件。我们可以在历史记录中存储需要的任何信息,但是要记住,当用户单击应用程序的后退按钮时,还要确定历史堆栈中的内容是什么。应用程序只有两个动作与 x_get_table() 和 x_get_image() 函数有关。因此对于每个表链接,可以存储名称 table 再加上 start 和 step 值作为事件标识符,比如 table-10-5。类似地,可以存储名称 image 和将被查看图像的 index,如 image-20。
在第 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); } |
图 3. 与相册应用程序结合的历史记录按钮 |
| CHCurrent = 4 CHStack = table-0-5%2Cimage-1%2Cimage-2%2Cimage-3%2Ctable-3-5 |