Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created September 21, 2023 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/010903e67b49a5e612a761c9678942fc to your computer and use it in GitHub Desktop.
Save code-boxx/010903e67b49a5e612a761c9678942fc to your computer and use it in GitHub Desktop.
Simple AJAX Breadcrumb Menu

SIMPLE AJAX BREADCRUMB MENU

https://code-boxx.com/ajax-breadcrumb-menu/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) WRAPPER */
.breadcrumb {
display: flex;
flex-wrap: wrap;
}
/* (B) ITEMS */
.breadcrumb div {
text-decoration: none;
cursor: pointer;
}
/* (C) SEPERATOR */
.breadcrumb div::after { content: ">"; }
.breadcrumb div:last-child::after { content: ""; }
/* (D) COSMETICS - THEMES */
/* (D1) GENERAL */
.breadcrumb, .breadcrumb * {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
.breadcrumb {
padding: 15px;
border-radius: 10px;
}
.breadcrumb div { font-weight: 700; }
.breadcrumb div::after { padding: 0 10px; }
/* (D2) RED */
.breadcrumb.red { background: #ffe4e4 }
.breadcrumb.red div { color: #c53030; }
.breadcrumb.red div::after { color: #ffa9a9; }
/* (D3) GREEN */
.breadcrumb.green { background: #bbffdd }
.breadcrumb.green div { color: #219531; }
.breadcrumb.green div::after { color: #77e594; }
/* (D4) BLUE */
.breadcrumb.blue { background: #daf1ff }
.breadcrumb.blue div { color: #5067db; }
.breadcrumb.blue div::after { color: #a6c6e9; }
/* (D4) ORANGE */
.breadcrumb.orange { background: #ffebda }
.breadcrumb.orange div { color: #e18b2f; }
.breadcrumb.orange div::after { color: #ffbe96; }
/* (X) NOT IMPORTANT */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#demoWrap {
padding: 10px; margin-top: 20px;
border: 1px solid #c7c7c7;
background: #f2f2f2;
}
<!DOCTYPE html>
<html>
<head>
<title>AJAX Breadcrumb Menu Demo</title>
<!-- (A) LOAD CSS + JS -->
<link rel="stylesheet" href="breadcrumb.css">
<script src="breadcrumb.js"></script>
</head>
<body>
<!-- (B) WRAPPERS -->
<nav id="demoMenu"></nav>
<div id="demoWrap"></div>
<!-- (C) ATTACH -->
<script>
// (C1) CREATE BREADCRUMB INSTANCE
var bread = breadify({
menu : document.getElementById("demoMenu"),
wrap : document.getElementById("demoWrap")
});
// (C2) ADD BREADCRUMB(S)
bread.add("Page A", "pageA.html");
bread.add("Page B", "pageB.html");
bread.go();
</script>
</body>
</html>
function breadify (instance) {
// (A) PROPERTIES + SET CSS
instance.loading = false; // currently loading
instance.crumbs = []; // breadcrumbs
instance.menu.className = "breadcrumb red"; // change the theme color if you want
// (B) ADD A CRUMB
instance.add = (name, url) => { if (!instance.loading) {
let c = document.createElement("div"),
i = instance.crumbs.length;
c.innerHTML = name;
c.onclick = () => instance.go(i);
instance.crumbs.push({ c : c, u : url });
instance.menu.appendChild(c);
}};
// (C) AJAX LOAD CRUMB
instance.go = i => { if (!instance.loading) {
// (C1) WHICH CRUMB TO LOAD?
let len = instance.crumbs.length - 1;
if (len<0 || i==len) { return; } // stop if error or user select last crumb
if (isNaN(i)) { i = len; } // none - load last crumb
if (i<0 || i>len) { i = len; } // prevent error - load last crumb
// (C2) FETCH URL
instance.loading = true; // "lock" until loaded
fetch(instance.crumbs[i]["u"])
.then(res => res.text())
.then(txt => instance.wrap.innerHTML = txt)
.catch(err => console.error(err))
.finally(() => {
for (j=i+1; j<=len; j++) { instance.crumbs[j]["c"].remove(); }
instance.crumbs = instance.crumbs.slice(0, i+1);
instance.loading = false; // unlock
});
}};
// (D) DONE!
return instance;
}
<h1>PAGE A</h1>
<p>It works!</p>
<p onclick="bread.add('Page B', 'pageB.html'); bread.go();">
Go To Page B
</p>
<h1>PAGE B</h1>
<p>It works!</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment