麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > AJAX > 正文

AJAX如何處理書簽和后退按鈕

2024-09-01 08:26:12
字體:
來源:轉載
供稿:網友

  本文將展示一個開源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應用程序。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产亚洲欧美在线视频 | 国产在线一级片 | 精品一区二区久久久久久按摩 | 成人三级电影网站 | 最新中文在线视频 | 日韩一级成人 | 一级黄色在线观看 | 日韩黄色片在线观看 | 日韩av成人 | 欧美在线a | 91热久久免费频精品黑人99 | 国产人成精品综合欧美成人 | 999久久久国产999久久久 | h视频在线播放 | 国产毛毛片一区二区三区四区 | 精品国产一区二区三区在线观看 | 毛片国产 | 91性高湖久久久久久久久网站 | 亚洲精品久久久久久久久久久 | 日本成人在线免费 | 国产91丝袜在线播放 | 亚洲综合视频在线播放 | 亚洲一区二区三区视频免费 | 久久久久一区二区三区四区五区 | 国产人成精品一区二区三 | 久久精品国产99国产精品亚洲 | 国产91对白叫床清晰播放 | 黄色大片免费网站 | 国产91九色在线播放 | 国色天香综合网 | 精品影视一区二区 | 黄视频在线网站 | 久久成人综合网 | 欧美黄色大片免费观看 | 羞羞视频免费观看入口 | 国产精品久久久久久久四虎电影 | 国产大片在线观看 | 成人黄色短视频在线观看 | 91九色视频在线观看 | 91精品国产综合久久男男 | 成人av一区二区免费播放 |