讓Ajax網頁帶有後退功能

來源:互聯網
上載者:User
ajax|網頁

在AJAX做了一個項目後,測試人員告訴我,新聞列表翻了很多頁後,當查看過一條新聞的詳細資料返回目錄的時候,不能返回到已經翻過的頁碼,而要從第一頁重新翻,這樣很鬱悶。網上面找到一篇<開發保留標準瀏覽器功能的AJAX應用程式>的文章,按照裡面的介紹,把響應的代碼節選出來,保留在這裡。

我實現原理是:每翻一次頁,把當前的頁碼存入碎url的片段標識中,待客戶查看完詳細資料後返回到目錄,在目錄的body的onload事件執行setOptionValue,將片段的內容擷取下來,再執行讀取目錄的函數;以達到儲存翻頁資訊的功能。

//使用URI的片段標識符部分,建立狀態的記錄;
function makeHistory(newHash)
{
  window.location.hash = newHash;
  expectedHash = window.location.hash;
  alert(expectedHash);
  return true;
}
//檢查瀏覽器記錄中的URI
function handleHistory()

  if ( window.location.hash != expectedHash )
  {
     alert(window.location.hash + "|" + expectedHash);
    expectedHash = window.location.hash;
    var newoption = expectedHash;
    setOptionValue( newoption );
  }
  return true;
}

function pollHash() {
  handleHistory();
  //window.setInterval("handleHistory()", 1000);
  return true;
}

//擷取片段中的內容,可進行任何操作
function setOptionValue(value)
{
//  var myForm = document.make_history;
//  var mySelect = myForm.change_year;
//  mySelect.options[value-1].selected = true;
    document.getElementById("testDiv").innerHTML = value.replace('#','');
    Test(value.replace('#',''));
    alert(value.replace('#',''));
  return true;
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.