私はpace.js
を使用しましたが、pace
の問題はページが完全に読み込まれた後に表示されました。js
私は試して解決策を作りました。ページ上のすべてのリンクをonclickし、jquery grabsをクリックしてブラウザから直接リンクに行くイベントを停止し、そのリンクにajax get要求を送信します。ページが読み込まれている間、現在のページにアニメーションが表示されます。ページが完全に読み込まれると、現在のドキュメント全体が受信したデータに置き換えられ、アドレスバーのページURLが置き換えられます。
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function() {
var dt;
if (/#/.test(this.href)) {
return true;
}else{
document.getElementById("before-loader").style.display= "block";//show loading animation
var that = this;
$.ajax({
type: "GET",
url: that.href,
success: function(response){
document.open();
document.write(response);
document.title = response.pageTitle;
window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", that.href);
document.close();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ' ' + errorThrown);
document.getElementById("before-loader").style.display= "none";//hide loading animation
}
});
return false;
}
});
});
</script>
ブラウザの[戻る]ボタンをクリックすると、アドレスバーのURLが変更されますが、ページの内容は変わりません。 どうすればこの問題を解決できますか?
更新
se0kjun
の答えは私の問題を解決(私はそれがコピーした後に動作するように期待していた/提供されたコードを貼り付け)が、正しい方向に私を入れていません。いくつかの研究とフローの書き換えの後、それは動作しますが、私はまだそれが動作する理由は混乱しています。
$(document).ready(function() {
$('a').click(function() {
if (/#/.test(this.href)) {
return true;
}else{
window.history.pushState(null, 'Production Workflow & Sales Management', this.href);
loadContent(this.href);
return false;
}
});
});
function loadContent(href) {
document.getElementById("before-loader").style.display= "block";
$.ajax({
type: "GET",
url: href,
success: function(response){
document.open();
document.title = response.pageTitle;
document.write(response);
document.close();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ' ' + errorThrown);
document.getElementById("before-loader").style.display= "none";
}
});
}
window.onpopstate = function(event) {
link = location.pathname.replace(/^.*[\\/]/, ""); // get filename only
loadContent(link);
};
多分私はもっと勉強すべきです。
です。 http://stackoverflow.com/questions/8038726/how-to-trigger-change-when-using-the-back-button-with-history-pushstate-and-pops – se0kjun
しかし、 'pushstate'と' popstateの使い方'この機能は?私は試しましたが、それは私に同じ結果を与えます。ページの内容は変わりません。私に例を挙げてもらえますか?ページが 'pushstate'と' popstate'のどこに格納されていますか? –