history pushStateとreplaceStateメソッドを使用して、ページを移動しながら履歴を更新するWebアプリを作成しました。
スクリプト自体はほぼ完全に機能します。ページを正しくロードし、スローする必要がある場合はページエラーをスローします。しかし、pushStateが複数の重複したエントリを履歴にプッシュする(そしてその前のエントリを置き換える)とは、奇妙な問題に気づきました。
たとえば、次のことを(順番に)実行するとします。
index.phpをロードします(履歴はIndexになります)
profile.phpに移動します(履歴はプロファイル、インデックスになります)。
search.phpに移動します(履歴は検索、検索、インデックスになります)
dashboard.phpに移動します
そして最後に、これは私の履歴に現れるものです(最も新しいものから最も古いものの順に):
ダッシュ
ボード
ダッシュボードダッシュボード
検索
インデックス
この問題は、ユーザーが進むボタンまたは戻るボタンをクリックすると、誤ったページにリダイレクトされるか、1回戻るために複数回クリックする必要があることです。それは、彼らが行って履歴を確認しても意味がありません。
これは私がこれまでに持っているものです:
var Traveller = function(){
this._initialised = false;
this._pageData = null;
this._pageRequest = null;
this._history = [];
this._currentPath = null;
this.abort = function(){
if(this._pageRequest){
this._pageRequest.abort();
}
};
// initialise traveller (call replaceState on load instead of pushState)
return this.init();
};
/*1*/Traveller.prototype.init = function(){
// get full pathname and request the relevant page to load up
this._initialLoadPath = (window.location.pathname + window.location.search);
this.send(this._initialLoadPath);
};
/*2*/Traveller.prototype.send = function(path){
this._currentPath = path.replace(/^\/+|\/+$/g, "");
// abort any running requests to prevent multiple
// pages from being loaded into the DOM
this.abort();
return this._pageRequest = _ajax({
url: path,
dataType: "json",
success: function(response){
// render the page to the dom using the json data returned
// (this part has been skipped in the render method as it
// doesn't involve manipulating the history object at all
window.Traveller.render(response);
}
});
};
/*3*/Traveller.prototype.render = function(data){
this._pageData = data;
this.updateHistory();
};
/*4*/Traveller.prototype.updateHistory = function(){
/* example _pageData would be:
{
"page": {
"title": "This is a title",
"styles": [ "stylea.css", "styleb.css" ],
"scripts": [ "scripta.js", "scriptb.js" ]
}
}
*/
var state = this._pageData;
if(!this._initialised){
window.history.replaceState(state, state.title, "/" + this._currentPath);
this._initialised = true;
} else {
window.history.pushState(state, state.title, "/" + this._currentPath);
}
document.title = state.title;
};
Traveller.prototype.redirect = function(href){
this.send(href);
};
// initialise traveller
window.Traveller = new Traveller();
document.addEventListener("click", function(event){
if(event.target.tagName === "a"){
var link = event.target;
if(link.target !== "_blank" && link.href !== "#"){
event.preventDefault();
// example link would be /profile.php
window.Traveller.redirect(link.href);
}
}
});
すべてのヘルプは、高く評価され
乾杯。
updateHistory関数にブラウザの履歴を追加しているようですね。これで、updateHistoryTravelerを初期化しているときに(1つ目は2回)、2つ目は呼び出される可能性があります(window.Traveller = new Traveller();、constructor-> init-> send-> render-> updateHistory)、さらにeventListener redirectからもclick呼び出されます。私はそれをテストしていません、単なる推測なので、答えではなくコメントとして追加しました。