2012-11-05 211 views
3

我正在處理單個頁面應用程序,其中每個部分通過ajax加載。首頁是自己的頁面,然後點擊「關於」,它會將您帶到單頁面應用程序。我已經得到的URL來改變瀏覽器的每個部分和前進和後退按鈕在這個片段的工作:history.pushState打破瀏覽器後退按鈕

$('.down-button').on('click', function() { 
     var stateObj = { principles: "about-principles" }; 
     history.pushState(stateObj, null, "about-principles"); 
     $('#principles-wrap').scrollintoview({ duration: 1000 }); 
    }); 

但是,當您單擊返回主頁的一路,網頁不刷新,你仍然在'關於'頁面。或者,如果您點擊返回主頁的鏈接,則會出現404錯誤。

我錯過了什麼?

EDIT

這裏是代碼的其餘部分:

function button_scroll() { 
// Call scrollIntoView js file into this 
$.getScript("/js/jquery.scrollintoview.min.js", function() { 
    $('.down-button').on('click', function() { 
     var stateObj = { principles: "about-principles" }; 
     history.pushState(stateObj, null, "about-principles"); 
     $('#principles-wrap').scrollintoview({ duration: 1000 }); 
    }); 
    $('.up-button-principles').on('click', function() { 
     var stateObj = { mission: "about-mission" }; 
     history.pushState(stateObj, null, "about-mission"); 
     $('#mission-wrap').scrollintoview({ duration: 1000 }); 
    }); 
    $('.down-button-principles').on('click', function() { 
     var stateObj = { snapshot: "about-snapshot" }; 
     history.pushState(stateObj, null, "about-snapshot"); 
     $('#snapshot-wrap').scrollintoview({ duration: 1000 }); 
    }); 
    $('.up-button-snapshot').on('click', function() { 
     var stateObj = { principles: "about-principles" }; 
     history.pushState(stateObj, null, "about-principles"); 
     $('#principles-wrap').scrollintoview({ duration: 1000 }); 
    }); 
    $('.down-button-snapshot').on('click', function() { 
     var stateObj = { people: "about-people" }; 
     history.pushState(stateObj, null, "about-people"); 
     $('#people-wrap').scrollintoview({ duration: 1000 }); 
    }); 
    $('.up-button-people').on('click', function() { 
     var stateObj = { snapshot: "about-snapshot" }; 
     history.pushState(stateObj, null, "about-snapshot"); 
     $('#snapshot-wrap').scrollintoview({ duration: 1000 }); 
    }); 

}); 

}

//Dropdown 
$(document).ready(function() { 
$(window).on('load resize', function() { 
    resize_mission(); 
    resize_principles(); 
    resize_snapshot(); 
    resize_people(); 
    button_scroll(); 
}); 
var dropDown = $('.dropdown'); 
$('h3.about').on('click', function() { 
    dropDown.slideDown('fast', function() { 
     var url = 'about2.php' 
     var stateObj = { mission: "about-mission" }; 
     history.pushState(stateObj, null, "about-mission"); 
     $.get(url, function(data) { 
      resize_mission(); 
      resize_principles(); 
      resize_snapshot(); 
      resize_people(); 
      button_scroll(); 
     }); 
    }); 
}); 

});

這就是我正在做的。沒有插件。

回答

2

您是否停止關於頁面上的點擊事件到首頁的默認操作?如果您在鏈接上使用preventDefault()stopPropagation(),請點擊您的關於頁面?你是否也停止hashchange事件的默認行爲?我們可能需要在執行歷史記錄時看到更多的代碼。你使用任何插件的歷史?

+0

我添加了我的其他代碼。不,我沒有做一個preventDefault()或使用插件。你可以在這裏看到這個頁面:http://teneo.telegraphbranding.com/ – reknirt

+0

你可能想看看Ben Alman的JQuery BBQ插件(http://benalman.com/projects/jquery-bbq-plugin/)。它非常易於使用,並且可以跨瀏覽器使用(以及使用當前代碼不會使用的舊瀏覽器)。我一直在我的項目中使用它(就像很多JQuery開發人員一樣),它使生活變得更容易:)。不過,只要有機會,我會再看看您的更新代碼。 – Zappa

+0

我如何在我的網站上實施燒烤插件?我對文檔有點困惑。再次,非常感謝您的幫助! – reknirt