@Lukeが言ったように、パンくずリストがナビゲートするためのリンクを追加していません。次のようにテキストとリンクを保存する必要があります。
function breadcrumbStateSaver(link, text) {
if (typeof (Storage) != "undefined") {
if (sessionStorage.breadcrumb) {
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //use any delim you prefer
}
else {
sessionStorage.breadcrumb = text;
sessionStorage.locations = link;
}
}
テキストとリンクの両方を 1 つのストレージ変数に追加し、区切り文字で分割して抽出するだけです!
クリック時にリンクをキャプチャする代わりに、ページが からロードされたときに実際にリンクを取得できますdocument.location.href
。テキストはdocument.title
、サーバーで設定される属性から取得できます。だから、document.ready
$(document).ready(function(){
breadcrumbStateSaver(document.title, document.location.href);
showBreadCrumb();
});
内のブレッドクラムを更新するには<ul>
:
function showBreadCrumb(){
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
for(let index in crumbs)
$("#navigation_links").append($("<li><a href="+links[index]+">"+ crumbs[index] +"</a></li>"));
}
このようにして、ユーザーが Web サイトをナビゲートするたびに記録されます。ユーザーがパンくずリストをナビゲートして、パンくずリストをクリックして元のページに戻るようにする場合は、上記のコードを次のように変更します。
function showBreadCrumb(){
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
let crumbindex = parseInt(sessionStorage.crumbindex);
for(let index in crumbs){
$("#navigation_links").append($("<li><a class='crumb"+ (index==crumbindex ? "active" : "") + "' href='"+links[index]+"' data-node='"+index+"'>"+ crumbs[index] +"</a></li>")); //data-node will help in retrieving the index and class will be set to active for selected node
}
}
また、次を追加します。
$(document).ready(function(){
$('#navigation_links').on('click', 'li a.crumb', function(){
if (typeof (Storage) != "undefined") {
sessionStorage.crumbindex = $(this).data('node');
sessionStorage.navlinkclicked = "true"; //This will prevent from adding breadcrumb again
}
});
breadcrumbStateSaver(document.title, document.location.href);
showBreadCrumb();
});
ノード インデックスを使用すると、次のようにブレッドクラムを挿入できます。
function breadcrumbStateSaver(link, text) {
if (typeof (Storage) != "undefined") {
if (sessionStorage.breadcrumb) {
if (sessionStorage.navlinkclicked && sessionStorage.navlinkclicked=="true") {
sessionStorage.navlinkclicked = "false"; //prevent changes or you can remove the rest of the crumbs from sessionStorage.breadcrumb and locations using a for loop
}
else {
if(sessionStorage.crumbindex && sessionStorage.crumbindex!=-1) {
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
let crumbindex = parseInt(sessionStorage.crumbindex);
sessionStorage.breadcrumb = crumbs[0];
sessionStorage.locations= links[0];
for(let index in crumbs){
if(index==0) continue;
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + crumbs[index];
sessionStorage.locations= sessionStorage.locations + ";" + links[index];
if(index==crumbindex) break;
}
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //insert the new link after crumbindex location
sessionStorage.crumbindex = -1;
}
else{
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //keep appending
}
}
}
else {
sessionStorage.breadcrumb = text;
sessionStorage.locations = link;
}
}
ユーザーのトラバーサルを記録するのではなく、単にナビゲーション用のブレッドクラムが必要な場合は、window.location.pathname
. ページがインデックス ページを含む適切な階層に格納されている場合は、URL パスを分割し、それに応じて showBreadCrumb() を更新するだけです。セッションストレージを使用する必要はありません!
function showBreadCrumb(){
let crumbs = window.location.pathname.split("/");
for(let index=0; index<crumbs.length-1; index++){
let text = crumbs[index];
if(index==0) text = "Home";
$("#navigation_links").append($("<li><a class='crumb"+ (index==crumbs.length-2? "active" : "") +"' href='"+buildRelativeLink(crumbs.length-index)+"'>"+ text +"</a></li>"));
}
}
function buildRelativeLink(level)
{
level = level - 1;
let link="";
for (let i=1; i<level; i++)
{
link=link+ "../"; //Used to navigate one level up towards parent
}
return link;
}