Skip to content

Instantly share code, notes, and snippets.

@AliAlmasi
Created July 22, 2025 00:41
Show Gist options
  • Save AliAlmasi/f5cdd4544248150dd78c0428594e7ed7 to your computer and use it in GitHub Desktop.
Save AliAlmasi/f5cdd4544248150dd78c0428594e7ed7 to your computer and use it in GitHub Desktop.

Land one level up in your path

Current Path Result
/a/b/c /a/b
/a/b /a
/a /
/ / (stays)

What this does:

  • .filter(Boolean) cleans out empty strings, like the one before a in /a.
  • .pop() knocks off the last piece.
  • Reconstructs the path and falls back to / if it’s empty.
function goUp(){
let parts = window.location.pathname.split('/').filter(Boolean);
parts.pop();
let newPath = '/' + parts.join('/');
window.location.href = newPath || '/';
}
// goUp() will take you one level up.
window.location.href = '/' + window.location.pathname.split('/').filter(Boolean).slice(0, -1).join('/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment