function initialize() { //
initialize the DHTML History //
framework dhtmlHistory.initialize(); 然后,开发人员使用dhtmlHistory.addListener()方法订阅历史记录变化事件。这个方法带有一个JavaScript回调函数,当DHTML历史记录变化事件发生时,该函数接收两个参数:新的页面位置以及任何可与该事件关联的可选历史记录数据:
window.onload = initialize;
function initialize() { //
initialize the DHTML History //
framework dhtmlHistory.initialize();
// subscribe to DHTML
history change //
events dhtmlHistory.addListener(historyChange); historyChange()方法很简单,该函数在用户导航到一个新位置后接收newLocation以及任何与该事件关联的可选historyData。
/** Our callback to receive history change events. */ function
historyChange(newLocation, historyData) { debug("A history change
has occurred: " + "newLocation="+newLocation + ",
historyData="+historyData,
true); } 上面用到的debug()方法是定义在示例源文件中的一个实用函数,它与完整示例打包在一起供下载。debug()只是用来将消息打印到Web页面上;第二个布尔型参数(在上述代码中值为true)控制是否在打印新的调试消息之前清除原有的全部消息。
function initialize() { //
initialize the DHTML History //
framework dhtmlHistory.initialize();
// subscribe to DHTML
history change //
events dhtmlHistory.addListener(historyChange);
// if this is
the first time we have // loaded the page... if
(dhtmlHistory.isFirstLoad()) { debug("Adding values to browser " +
"history", false); // start adding
history dhtmlHistory.add("helloworld", "Hello World
Data"); dhtmlHistory.add("foobar",
33); dhtmlHistory.add("boobah", true);
var complexObject =
new Object(); complexObject.value1 = "This is the first
value"; complexObject.value2 = "This is the second
data"; complexObject.value3 = new
Array(); complexObject.value3[0] = "array
1"; complexObject.value3[1] = "array
2";
dhtmlHistory.add("complexObject",
complexObject); 在add()被调用之后,新的地址将立即作为一个锚值(链接地址)显示在浏览器的URL地址栏中。例如,对地址为http://codinginparadise.org/my_ajax_app的AJAX
Web页面调用dhtmlHistory.add("helloworld", "Hello World
Data")之后,用户将会在其浏览器URL地址栏中看到如下的地址: