' + '/foobar>'; historyStorage.put("fakeXML", fakeXML); }
然后,如果用户从这个页面漂移走(导航走)又通过返回按钮返回了,我们可以用get()提出我们存储的值或者用haskey()检查他是否存在。
window.onload = initialize; 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); // cache some values in the history // storage debug("Storing key 'fakeXML' into " + "history storage", false); var fakeXML = '?xml version="1.0" ' + 'encoding="ISO-8859-1"?>' + 'foobar>' + 'foo-entry/>' + '/foobar>'; historyStorage.put("fakeXML", fakeXML); } // retrieve our values from the history // storage var savedXML = historyStorage.get("fakeXML"); savedXML = prettyPrintXml(savedXML); var hasKey = historyStorage.hasKey("fakeXML"); var message = "historyStorage.hasKey('fakeXML')=" + hasKey + "br>" + "historyStorage.get('fakeXML')=br>
上一页123456下一页 eHistoryChange 都被调用。我们得到新的地址(newLocation) 使用他更新我们的用户接口来改变状态,通过使用一个叫displayLocation的O'Reilly Mail的工具方法。
/** Handles history change events. */function handleHistoryChange(newLocation, historyData) { // if there is no location then display // the default, which is the inbox if (newLocation == "") { newLocation = "section:inbox"; } // extract the section to display from // the location change; newLocation will // begin with the word "section:" newLocation = newLocation.replace(/section:/, ""); // update the browser to respond to this // DHTML history change displayLocation(newLocation, historyData);}/** Displays the given location in the right-hand side content area. */function displayLocation(newLocation, sectionData) { // get the menu element that was selected var selectedElement = document.getElementById(newLocation); // clear out the old selected menu item var menu = document.getElementById("menu"); for (var i = 0; i menu.childNodes.length; i++) { var currentElement = menu.childNodes[i]; // see if this is a DOM Element node if (currentElement.nodeType == 1) { // clear any class name currentElement.className = ""; } } // cause the new selected menu item to // appear differently in the UI selectedElement.className = "selected"; // display the new section in the right-hand // side of the screen; determine what // our sectionData is // display the address book differently by // using our local address data we cached // earlier if (newLocation == "addressbook") { // format and display the address book sectionData = "p>Your addressbook:/p>"; sectionData += "ul>"; // fetch the address book from the cache // if we don't have it yet if (window.addressBook == undefined) { window.addressBook = historyStorage.get("addressBook"); } // format the address book for display for (var i = 0; i window.addressBook.length; i++) { sectionData += "li>" + window.addressBook[i] + "/li>"; } sectionData += "/ul>"; } // If there is no sectionData, then // remotely retrieve it; in this example // we use fake data for everything but the // address book if (sectionData == null) { // in a real application we would remotely // fetch this section's content sectionData = "p>This is section: " + selectedElement.innerHTML + "/p>"; } // update the content's title and main text var contentTitle = document.getElementById("content-title"); var contentValue = document.getElementById("content-value"); contentTitle.innerHTML = selectedElement.innerHTML; contentValue.innerHTML = sectionData;}
演示(Demo)O'Reilly Mail或者下载(download)O'Reilly Mail的源代码。
你现在已经学习了使用Really Simple History API 让你的AJAX应用响应书签和前进回退按钮,而且有代码可以作为创建你自己的应用的素材。我热切地期上一页123456下一页 story.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页面上打一条消息,第2个Boolean变量,在代码里是true,控制一个新的debug消息打印前是否要清除以前存在的所有消息。
一个开发者使用add()方法加入历史事件。加入一个历史事件包括根据历史的改变指定一个新的地址,就像"edit:SomePage"标记, 还提供一个事件发生时可选的会被存储到历史数据historyData值.
window.onload = initialize; 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栏里。例如,一个AJAX web页面停留在http://codinginparadise.org/my_ajax_app,调用了dhtmlHistory.add("helloworld", "Hello World Data" 后,用户将在浏览器的URL栏里看到下面的地址
http://codinginparadise.org/my_ajax_app#helloworld
然后他们可以把这个页面做成书签,如果他们使用这个书签,你的AJAX应用可以读出#helloworld值然后使用她去初始化web页面。Hash里的地址值被Really Simple History 框架显式的编码和解码(URL encoded and decoded) (这是为了解决字符的编码问题)
对当AJAX地址改变时保存更多的复杂的状态来说,historyData 比一个更容易的匹配一个URL的东西更有用。他是一个可选的值,可以是任何javascript类型,比如Number, String, 或者 Object 类型。有一个例子是用这个在一个多文本编辑器(rich text editor)保存所有的文本,例如,如果用户从这个页面漂移(或者说从这个页面导航到其他页面,离开了这个页面)走。当一个用户再回到这个地址,浏览器会把这个对象返回给历史改变侦听器(history change listener)。
开发者可以提供一个完全的historyData 的javascript对象,用嵌套的对象objects和排列arrays来描绘复杂的状态。只要是JSON (JavaScript Object Notation) 允许的那么在历史数据里就是允许的,包括简单数据类型和null型。DOM的对象和可编程的浏览器对象比如XMLHttpRequest ,不会被保存。注意historyData 不会被书签持久化,如果浏览器关掉,或者浏览器的缓存被清空,或者用户清除历史的时候,会消失掉。
使用dhtmlHistory 最后一步,是isFirstLoad() 方法。如果你导航到一个web页面,再跳到一个不同的页面,然后按下回退按钮返回起始的网站,第一页将完全重新装载,并激发onload事件。这样能产生破坏性,当代码在第一次装载时想要用某种方式初始化页面的时候,不会再刷新页面。isFirstLoad() 方法让区别是最开始第一次装载页面,还是相对的,在用户导航回到他自己的浏览器历史中记录的网页时激发load事件,成为可能。
在例子代码中,我们只想在第一次页面装载的时候加入历史事件,如果用户在第一次装载后,按回退按钮返回页面,我们就不想重新加入任何历史事件。
window.onload = initialize; function initialize() { // initialize the DHTML History // framework dhtmlHistory.initialize(); // subscribe to DHTML history change // events dhtml上一页123456下一页