日期:2014-05-16  浏览次数:20611 次

ajax/dwr--后退的处理
Ajax改变了浏览器的默认规则,在使用Ajax应用时,后退往往会得不到我们还要的结果。那么如何来处理这种方式,使之更适合我们的习惯呢。
  Brad Neuberg的开源Really Simple History (RSH)框架是一个简单的解决这个问题的方案。其实现的方式如下:用隐藏的iFrame,Timer和隐藏的Form来检测历史浏览的变化,其中隐藏的 Form用来保存页面的数据信息。
  当然其他还有如Backbase 和 Dojo还可以解决这种问题,但复杂度就高了些。
  下面就以先前的样例程序来看一下如何使用RSH框架,其实很容易的:

包含RSH框架的JS脚本:
<script src="/oblog312/dhtmlHistory.js"></script>



初始化框架并定义后退时的回调函数:
<script language="JavaScript">
  function initialize() {
    // 初始化框架
    dhtmlHistory.initialize();
    // 加入回调函数
    dhtmlHistory.addListener(historyChange);
  }

  /** Our callback to receive history 
      change events. */
  function historyChange(newLocation, historyData) {

    //更新数据
    DWRUtil.removeAllRows("goodsbody");
    var arrayUrl = newLocation.split(":");
    if (arrayUrl.length == 2) {
        document.getElementById("type").value = arrayUrl[0];
        document.rentalForm.select.checked = false;
        document.getElementById("totalRecords").innerHTML = historyData.length;
        DWRUtil.addRows("goodsbody", historyData, [ addCheckbox, getName, getPrice, getCount]);
      }
  }
</script> 

在数据更新时保留之前的地址:  
function fillTable(goods) {
    dhtmlHistory.add(document.getElementById("type").value+":"+new Date().getTime(), goods);
    
  document.rentalForm.select.checked = false;
  document.getElementById("totalRecords").innerHTML = goods.length;
  DWRUtil.addRows("goodsbody", goods, [ addCheckbox, getName, getPrice, getCount]);
}


当然还在页面初始化加入上面的初始化函数:
<body onload="initialize();">


  OK,大功告成,简单吧^_^