DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 如何使用Ajax解決書簽和後退按鈕失效問
如何使用Ajax解決書簽和後退按鈕失效問
編輯:AJAX詳解     
本文將展示一個開源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。

  目前有幾個AJax框架對書簽和歷史記錄問題有所幫助;但這些框架目前都有幾個由於實現而造成的重大Bug。此外,很多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腳本:

<!-- 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對象:

window.onload = initialize;

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)控制是否在打印新的調試消息之前清除原有的全部消息。

  開發人員使用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地址欄中。例如,對地址為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頁面時觸發的“假加載”事件。

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

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:

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()方法檢查該值是否存在。

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()方法保存和檢索地址簿:

/** 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並使用它將我們的用戶界面更新到正確的狀態。

/** 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;
}



  結束語

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved