如何處理書籤和後退按鈕

 如何處理書籤和後退按鈕

  本文將展示一個開源JavaScript庫,該腳本庫給AJAX應用程序帶來了書籤和後退按鈕支持。在學習完這個教程後,開發人員將能夠獲得對一個AJAX問題的解決方案(甚至連Google Maps和Gmail現在都不提供該解決方案):一個強大的、可用的書籤和後退前進功能,其操作行爲如同其他的Web應用程序一樣。

  本文將闡述目前AJAX應用程序在使用書籤和後退按鈕方面所面臨的嚴重問題;展示Really Simple History(RSH)庫——一個可以解決以上問題的開源框架,並提供幾個運行中的例子。

  本文所展示的這個框架的主要發明分爲兩部分。首先是一個隱藏的HTML表單,用於緩存大量短期會話的客戶端信息;這種緩存功能爲頁面導航提供了強大的支持。其次是超鏈接錨點和隱藏Iframe的組合,它們被嵌入後退和前進按鈕,用來截獲和記錄瀏覽器的歷史記錄事件。以上兩種技術都被包裝在一個簡單的JavaScript庫中來簡化開發。

問題
  書籤和後退按鈕在傳統的多頁面Web應用程序中運行得非常好。當用戶瀏覽web站點的時候,其瀏覽器的地址欄記錄隨新的URL而更新,這些記錄可以被粘貼到電子郵件或者書籤中供以後使用。後退和前進按鈕也可以正常操作,使用戶可以在訪問過的頁面中向前或向後翻動。

  但是AJAX應用程序卻不一樣,它們是運行在單個Web頁面中的複雜程序。瀏覽器並不是爲這類程序而構建的——這類Web應用程序已經過時,它們在每次鼠標點擊的時候都需要重新刷新整個頁面。

  在這種類似於Gmail的AJAX軟件中,瀏覽器的地址欄在用戶選擇功能和改變程序狀態的時候保持不變,這使得無法在特定的應用程序視圖中使用書籤。此外,如果用戶按下“後退”按鈕來“撤銷”上次的操作,他們會驚奇地發現,瀏覽器會完全離開該應用程序的Web頁面。

解決方案
  開源RSH框架可以解決這些問題,它爲AJAX應用程序提供了書籤和控制後退、前進按鈕的功能。RSH目前還處於Beta階段,可以在Firefox 1.0、Netscape 7+、Internet Explorer 6+等瀏覽器上運行;目前還不支持Safari(有關說明,請參見我的文章“Coding in Paradise: Safari: No DHTML History Possible”)。

  目前有幾個AJAX框架對書籤和歷史記錄問題有所幫助;但這些框架目前都有幾個由於實現而造成的重大Bug(有關詳細信息,請參見“Coding in Paradise: AJAX History Libraries”)。此外,很多AJAX歷史記錄框架被綁定到較大的庫上,例如Backbase和Dojo;這些框架爲AJAX應用程序引入了完全不同的編程模型,迫使開發人員使用全新的方式來獲得歷史記錄功能。

  相較之下,RSH是一個可以包含在現有AJAX系統中的簡單模塊。此外,RSH庫採用了一些技術以避免產生影響其他歷史記錄框架的Bug。

  RSH框架由兩個JavaScript類組成:DhtmlHistory和HistoryStorage。

  DhtmlHistory類爲AJAX應用程序提供歷史記錄抽象。AJAX頁面使用add()方法添加歷史記錄事件到瀏覽器,指定新的地址和相關的歷史記錄數據。DhtmlHistory類使用一個錨散列(如#new-location)更新瀏覽器當前的URL,同時把歷史記錄數據和該新URL關聯。AJAX應用程序將自己註冊爲歷史記錄的監聽器,當用戶使用後退和前進按鈕進行瀏覽時,歷史記錄事件被觸發,爲瀏覽器提供新的位置以及與add()調用一起保存的任何歷史記錄數據。

  第二個類:HistoryStorage,允許開發人員保存任意數量的已存歷史記錄數據。在普通Web頁面中,當用戶導航到一個新的web站點時,瀏覽器卸載並清除web頁面上的所有應用程序和JavaScript狀態;如果用戶用後退按鈕返回,所有的數據都丟失了。HistoryStorage類通過一個包含簡單散列表方法(例如put()、get()、hasKey())的API來解決這類問題。上面的方法允許開發人員在用戶離開Web頁面之後保存任意數量的數據;當用戶按後退按鈕重新返回時,歷史記錄數據可以通過HistoryStorage類來訪問。在內部,我們通過使用隱藏的表單字段來實現此功能,這是因爲瀏覽器會自動保存表單字段中的值,甚至在用戶離開Web頁面的時候也如此。

例子
  讓我們先從一個簡單的例子開始。

  首先,任何需要使用RSH框架的頁面都必須包含dhtmlHistory.js腳本:

Java代碼 複製代碼
  1. <!-- Load the Really Simple   
  2. History framework -->   
  3. <script type="text/javascript"  
  4. src="../../framework/dhtmlHistory.js">   
  5. </script>  
<!-- Load the Really Simple
History framework -->
<script type="text/javascript"
src="../../framework/dhtmlHistory.js">
</script>

  DHTML歷史記錄應用程序也必須在與AJAX Web頁面相同的目錄下包含blank.html文件;這個文件與RSH框架打包在一起,且對於Internet Explorer來說是必需的。順便提一下,RSH使用一個隱藏Iframe來跟蹤和添加Internet Explorer的歷史記錄變化;這個Iframe需要我們指定一個實際的文件位置才能正常工作,這就是blank.html。

  RSH框架創建了一個叫做dhtmlHistory的全局對象,這是操縱瀏覽器歷史記錄的入口點。使用dhtmlHistory的第一步是在Web頁面加載完成後初始化dhtmlHistory對象:

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();  
window.onload = initialize;
function initialize() {
// initialize the DHTML History
// framework
dhtmlHistory.initialize();


  然後,開發人員使用dhtmlHistory.addListener()方法訂閱歷史記錄變化事件。這個方法帶有一個JavaScript回調函數,當DHTML歷史記錄變化事件發生時,該函數接收兩個參數:新的頁面位置以及任何可與該事件關聯的可選歷史記錄數據:

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();   
  6. // subscribe to DHTML history change   
  7. // events   
  8. dhtmlHistory.addListener(historyChange);  
window.onload = initialize;
function initialize() {
// initialize the DHTML History
// framework
dhtmlHistory.initialize();
// subscribe to DHTML history change
// events
dhtmlHistory.addListener(historyChange);


  historyChange()方法很簡單,該函數在用戶導航到一個新位置後接收newLocation以及任何與該事件關聯的可選historyData。

Java代碼 複製代碼
  1. /** Our callback to receive history change  
  2. events. */  
  3. function historyChange(newLocation,   
  4. historyData) {   
  5. debug("A history change has occurred: "  
  6. "newLocation="+newLocation   
  7. ", historyData="+historyData,   
  8. true);   
  9. }  
/** 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)控制是否在打印新的調試消息之前清除原有的全部消息。

  開發人員使用add()方法添加歷史記錄事件。添加歷史記錄事件涉及爲歷史記錄變化指定一個新地址,例如edit:SomePage,以及提供一個和該事件一起保存的可選historyData值。

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();   
  6. // subscribe to DHTML history change   
  7. // events   
  8. dhtmlHistory.addListener(historyChange);   
  9. // if this is the first time we have   
  10. // loaded the page...   
  11. if (dhtmlHistory.isFirstLoad()) {   
  12. debug("Adding values to browser "  
  13. "history"false);   
  14. // start adding history   
  15. dhtmlHistory.add("helloworld",   
  16. "Hello World Data");   
  17. dhtmlHistory.add("foobar"33);   
  18. dhtmlHistory.add("boobah"true);   
  19. var complexObject = new Object();   
  20. complexObject.value1 =   
  21. "This is the first value";   
  22. complexObject.value2 =   
  23. "This is the second data";   
  24. complexObject.value3 = new Array();   
  25. complexObject.value3[0] = "array 1";   
  26. complexObject.value3[1] = "array 2";   
  27. dhtmlHistory.add("complexObject",   
  28. complexObject);  
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地址欄中。例如,對地址爲http://codinginparadise.org/my_ajax_app的AJAX Web頁面調用dhtmlHistory.add("helloworld", "Hello World Data")之後,用戶將會在其瀏覽器URL地址欄中看到如下的地址:

http://codinginparadise.org/my_ajax_app#helloworld
  然後用戶可以將這個頁面做成書籤,如果以後用到這個書籤,AJAX應用程序可以讀取#helloworld值,並用它來初始化Web頁面。散列後面的地址值是RSH框架可以透明編碼和解碼的URL地址。

  HistoryData非常有用,它保存比簡單的URL更爲複雜的AJAX地址變化狀態。這是一個可選值,可以是任何JavaScript類型,例如Number、String或Object。使用該保存功能的一個例子是在一個富文本編輯器中保存所有文本(比如在用戶離開當前頁面時)。當用戶再回到這個地址時,瀏覽器將會將該對象返回給歷史記錄變化監聽器。

  開發人員可以爲historyData提供帶有嵌套對象和表示複雜狀態的數組的完整JavaScript對象;JSON (JavaScript Object Notation)所支持的在歷史記錄數據中都支持,包括簡單數據類型和null類型。然而,DOM對象以及可用腳本編寫的瀏覽器對象(如XMLHttpRequest)不會被保存。請注意,historyData並不隨書籤一起保存,當瀏覽器關閉,瀏覽器緩存被清空,或者用戶清除歷史記錄的時候,它就會消失。

  使用dhtmlHistory的最後一步是isFirstLoad()方法。在某些瀏覽器中,如果導航到一個Web頁面,再跳轉到另一個不同的頁面,然後按“後退”按鈕返回到起始的站點,第一頁將完全重新加載,並觸發onload事件。這樣會對想要在第一次加載頁面時用某種方式對其進行初始化(而其後則不使用這種方式重新加載該頁面)的代碼造成破壞。isFirstLoad()方法可以區分是第一次加載一個Web頁面還是用戶導航到保存在歷史記錄中的Web頁面時觸發的“假加載”事件。

  在示例代碼中,我們只想在第一次加載頁面的時候添加歷史記錄事件;如果用戶在加載頁面後按後退按鈕返回該頁面,我們就不想重新添加任何歷史記錄事件:

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();   
  6. // subscribe to DHTML history change   
  7. // events   
  8. dhtmlHistory.addListener(historyChange);   
  9. // if this is the first time we have   
  10. // loaded the page...   
  11. if (dhtmlHistory.isFirstLoad()) {   
  12. debug("Adding values to browser "  
  13. "history"false);   
  14. // start adding history   
  15. dhtmlHistory.add("helloworld",   
  16. "Hello World Data");   
  17. dhtmlHistory.add("foobar"33);   
  18. dhtmlHistory.add("boobah"true);   
  19. var complexObject = new Object();   
  20. complexObject.value1 =   
  21. "This is the first value";   
  22. complexObject.value2 =   
  23. "This is the second data";   
  24. complexObject.value3 = new Array();   
  25. complexObject.value3[0] = "array 1";   
  26. complexObject.value3[1] = "array 2";   
  27. dhtmlHistory.add("complexObject",   
  28. complexObject);  
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);

  讓我們繼續使用historyStorage類。類似於dhtmlHistory,historyStorage通過一個叫historyStorage的全局對象來公開它的功能。該對象有幾個模擬散列的方法,比如put(keyName、keyValue)、get(keyName)和hasKey(keyName)。鍵名稱必須是字符串,同時鍵值可以是複雜的JavaScript對象甚至是XML格式的字符串。在我們的源代碼例子中,在第一次加載頁面時,我們使用put()將簡單的XML放入historyStorage:

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();   
  6. // subscribe to DHTML history change   
  7. // events   
  8. dhtmlHistory.addListener(historyChange);   
  9. // if this is the first time we have   
  10. // loaded the page...   
  11. if (dhtmlHistory.isFirstLoad()) {   
  12. debug("Adding values to browser "  
  13. "history"false);   
  14. // start adding history   
  15. dhtmlHistory.add("helloworld",   
  16. "Hello World Data");   
  17. dhtmlHistory.add("foobar"33);   
  18. dhtmlHistory.add("boobah"true);   
  19. var complexObject = new Object();   
  20. complexObject.value1 =   
  21. "This is the first value";   
  22. complexObject.value2 =   
  23. "This is the second data";   
  24. complexObject.value3 = new Array();   
  25. complexObject.value3[0] = "array 1";   
  26. complexObject.value3[1] = "array 2";   
  27. dhtmlHistory.add("complexObject",   
  28. complexObject);   
  29. // cache some values in the history   
  30. // storage   
  31. debug("Storing key 'fakeXML' into "  
  32. "history storage"false);   
  33. var fakeXML =   
  34. '<?xml version="1.0" '  
  35. +      'encoding="ISO-8859-1"?>'  
  36. +      '<foobar>'  
  37. +         '<foo-entry/>'  
  38. +      '</foobar>';   
  39. historyStorage.put("fakeXML", fakeXML);}  
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);}

  然後,如果用戶離開頁面後又通過後退按鈕返回該頁面,我們可以使用get()方法提取保存的值,或者使用hasKey()方法檢查該值是否存在。

Java代碼 複製代碼
  1. window.onload = initialize;   
  2. function initialize() {   
  3. // initialize the DHTML History   
  4. // framework   
  5. dhtmlHistory.initialize();   
  6. // subscribe to DHTML history change   
  7. // events   
  8. dhtmlHistory.addListener(historyChange);   
  9. // if this is the first time we have   
  10. // loaded the page...   
  11. if (dhtmlHistory.isFirstLoad()) {   
  12. debug("Adding values to browser "  
  13. "history"false);   
  14. // start adding history   
  15. dhtmlHistory.add("helloworld",   
  16. "Hello World Data");   
  17. dhtmlHistory.add("foobar"33);   
  18. dhtmlHistory.add("boobah"true);   
  19. var complexObject = new Object();   
  20. complexObject.value1 =   
  21. "This is the first value";   
  22. complexObject.value2 =   
  23. "This is the second data";   
  24. complexObject.value3 = new Array();   
  25. complexObject.value3[0] = "array 1";   
  26. complexObject.value3[1] = "array 2";   
  27. dhtmlHistory.add("complexObject",   
  28. complexObject);   
  29. // cache some values in the history   
  30. // storage   
  31. debug("Storing key 'fakeXML' into "  
  32. "history storage"false);   
  33. var fakeXML =   
  34. '<?xml version="1.0" '  
  35. +      'encoding="ISO-8859-1"?>'  
  36. +      '<foobar>'  
  37. +         '<foo-entry/>'  
  38. +      '</foobar>';   
  39. historyStorage.put("fakeXML", fakeXML);   
  40. }   
  41. // retrieve our values from the history   
  42. // storage   
  43. var savedXML =   
  44. historyStorage.get("fakeXML");   
  45. savedXML = prettyPrintXml(savedXML);   
  46. var hasKey =   
  47. historyStorage.hasKey("fakeXML");   
  48. var message =   
  49. "historyStorage.hasKey('fakeXML')="  
  50. + hasKey + "<br>"  
  51. "historyStorage.get('fakeXML')=<br>"  
  52. + savedXML;   
  53. debug(message, false);   
  54. }  
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>"
+ savedXML;
debug(message, false);
}

  prettyPrintXml()是一個定義在完整示例源代碼中的實用方法;此函數準備在web頁面中顯示以便用於調試的簡單XML。

  請注意,相關數據只在該頁面的歷史記錄中進行持久化;如果瀏覽器被關閉,或者用戶打開一個新窗口並再次鍵入AJAX應用程序的地址,則該歷史記錄數據對於新的Web頁面不可用。歷史記錄數據只有在用到後退或前進按鈕時才被持久化,當用戶關閉瀏覽器或清空緩存的時候就會消失。如果想真正長期持久化,請參閱Ajax MAssive Storage System (AMASS)。

  我們的簡單示例已經完成。您可以演示或下載完整源代碼。

示例2:O'Reilly Mail
  我們的第二個例子是一個簡單的AJAX電子郵件模擬應用程序的示例,即O'Reilly Mail,它類似於Gmail。O'Reilly Mail描述瞭如何使用dhtmlHistory類來控制瀏覽器的歷史記錄,以及如何使用historyStorage對象來緩存歷史記錄數據。

  O'Reilly Mail用戶界面由兩部分組成。在頁面的左邊是一個帶有不同電子郵件文件夾和選項的菜單,例如收件箱、草稿箱等。當用戶選擇了一個菜單項(如收件箱),就用這個菜單項的內容更新右邊的頁面。在一個現實應用程序中,我們會遠程獲取並顯示選擇的信箱內容,不過在O'Reilly Mail中,我們只顯示已選擇的選項。

  O'Reilly Mail使用RSH框架向瀏覽器歷史記錄中添加菜單變化並更新地址欄,允許用戶利用瀏覽器的後退和前進按鈕爲應用程序做收藏書籤和跳到上次變化的菜單。

  我們添加一個特殊的菜單項——地址簿,以說明如何來使用historyStorage。地址簿是一個由聯繫人名稱和郵件地址組成的JavaScript數組,在一個現實應用程序中,我們會從一臺遠程服務器取得這個數組。不過,在O'Reilly Mail中,我們在本地創建這個數組,添加幾個名稱和電子郵件地址,然後將其保存在historyStorage對象中。如果用戶離開Web頁面後又返回該頁面,那麼O'Reilly Mail應用程序將重新從緩存檢索地址簿,而不是再次聯繫遠程服務器。

  我們用initialize()方法保存和檢索地址簿:

Java代碼 複製代碼
  1. /** Our function that initializes when the page  
  2. is finished loading. */  
  3. function initialize() {   
  4. // initialize the DHTML History framework   
  5. dhtmlHistory.initialize();   
  6. // add ourselves as a DHTML History listener   
  7. dhtmlHistory.addListener(handleHistoryChange);   
  8. // if we haven't retrieved the address book   
  9. // yet, grab it and then cache it into our   
  10. // history storage   
  11. if (window.addressBook == undefined) {   
  12. // Store the address book as a global   
  13. // object.   
  14. // In a real application we would remotely   
  15. // fetch this from a server in the   
  16. // background.   
  17. window.addressBook =   
  18. ["Brad Neuberg '[email protected]'",   
  19. "John Doe '[email protected]'",   
  20. "Deanna Neuberg '[email protected]'"];   
  21. // cache the address book so it exists   
  22. // even if the user leaves the page and   
  23. // then returns with the back button   
  24. historyStorage.put("addressBook",   
  25. addressBook);   
  26. }   
  27. else {   
  28. // fetch the cached address book from   
  29. // the history storage   
  30. window.addressBook =   
  31. historyStorage.get("addressBook");   
  32. }  
/** Our function that initializes when the page
is finished loading. */
function initialize() {
// initialize the DHTML History framework
dhtmlHistory.initialize();
// add ourselves as a DHTML History listener
dhtmlHistory.addListener(handleHistoryChange);
// if we haven't retrieved the address book
// yet, grab it and then cache it into our
// history storage
if (window.addressBook == undefined) {
// Store the address book as a global
// object.
// In a real application we would remotely
// fetch this from a server in the
// background.
window.addressBook =
["Brad Neuberg '[email protected]'",
"John Doe '[email protected]'",
"Deanna Neuberg '[email protected]'"];
// cache the address book so it exists
// even if the user leaves the page and
// then returns with the back button
historyStorage.put("addressBook",
addressBook);
}
else {
// fetch the cached address book from
// the history storage
window.addressBook =
historyStorage.get("addressBook");
}

  處理歷史記錄變化的代碼也很簡單。在下面的源代碼中,無論用戶按後退還是前進按鈕,都將調用handleHistoryChange。使用O'Reilly Mail定義的displayLocation實用方法,我們可得到newLocation並使用它將我們的用戶界面更新到正確的狀態。

Java代碼 複製代碼
  1. /** Handles history change events. */  
  2. function handleHistoryChange(newLocation,   
  3. historyData) {   
  4. // if there is no location then display   
  5. // the default, which is the inbox   
  6. if (newLocation == "") {   
  7. newLocation = "section:inbox";   
  8. }   
  9. // extract the section to display from   
  10. // the location change; newLocation will   
  11. // begin with the word "section:"   
  12. newLocation =   
  13. newLocation.replace(/section/:/, "");   
  14. // update the browser to respond to this   
  15. // DHTML history change   
  16. displayLocation(newLocation, historyData);   
  17. }   
  18. /** Displays the given location in the  
  19. right-hand side content area. */  
  20. function displayLocation(newLocation,   
  21. sectionData) {   
  22. // get the menu element that was selected   
  23. var selectedElement =   
  24. document.getElementById(newLocation);   
  25. // clear out the old selected menu item   
  26. var menu = document.getElementById("menu");   
  27. for (var i = 0; i < menu.childNodes.length;   
  28. i++) {   
  29. var currentElement = menu.childNodes[i];   
  30. // see if this is a DOM Element node   
  31. if (currentElement.nodeType == 1) {   
  32. // clear any class name   
  33. currentElement.className = "";   
  34. }   
  35. }   
  36. // cause the new selected menu item to   
  37. // appear differently in the UI   
  38. selectedElement.className = "selected";   
  39. // display the new section in the right-hand   
  40. // side of the screen; determine what   
  41. // our sectionData is   
  42. // display the address book differently by   
  43. // using our local address data we cached   
  44. // earlier   
  45. if (newLocation == "addressbook") {   
  46. // format and display the address book   
  47. sectionData = "<p>Your addressbook:</p>";   
  48. sectionData += "<ul>";   
  49. // fetch the address book from the cache   
  50. // if we don't have it yet   
  51. if (window.addressBook == undefined) {   
  52. window.addressBook =   
  53. historyStorage.get("addressBook");   
  54. }   
  55. // format the address book for display   
  56. for (var i = 0;   
  57. i < window.addressBook.length;   
  58. i++) {   
  59. sectionData += "<li>"  
  60. + window.addressBook[i]   
  61. "</li>";   
  62. }   
  63. sectionData += "</ul>";   
  64. }   
  65. // If there is no sectionData, then   
  66. // remotely retrieve it; in this example   
  67. // we use fake data for everything but the   
  68. // address book   
  69. if (sectionData == null) {   
  70. // in a real application we would remotely   
  71. // fetch this section's content   
  72. sectionData = "<p>This is section: "  
  73. + selectedElement.innerHTML + "</p>";   
  74. }   
  75. // update the content's title and main text   
  76. var contentTitle =   
  77. document.getElementById("content-title");   
  78. var contentValue =   
  79. document.getElementById("content-value");   
  80. contentTitle.innerHTML =   
  81. selectedElement.innerHTML;   
  82. contentValue.innerHTML = sectionData;   
  83. }  
/** 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;
}

  您可以演示O'Reilly Mail或者下載O'Reilly Mail源代碼。

結束語
  現在我們已經瞭解瞭如何使用RSH API來使AJAX應用程序支持書籤以及後退和前進按鈕,並提供了示例代碼,您可參考該示例來創建自己的應用程序。希望您能利用書籤和歷史記錄的支持來增強AJAX應用程序。

參考資料
下載此文的所有示例代碼。
下載RSH框架。
演示O'Reilly Mail或者下載O'Reilly Mail源代碼。完整的示例下載還包括更多供試用的示例。
Coding in Paradise:作者的weblog,內容涵蓋了AJAX、DHTML以及Java技術和利用協作技術的新開發,例如WikiWikis。
"Coding in Paradise: AJAX: Bookmarks and Back Buttons, Advanced Example"
"Coding in Paradise: Safari: No DHTML History Possible"
"Coding in Paradise: AJAX Tutorial: Saving Session Across Page Loads Without Cookies, On The Client Side"
"Coding in Paradise: AJAX History Libraries"
原文出處:http://www.onjava.com/pub/a/onjava/2005/10/26/ajax-handling-bookmarks-and-back-button.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章