Skip to content

Instantly share code, notes, and snippets.

@akku1139
Created February 28, 2025 00:44
Show Gist options
  • Save akku1139/6527ec5333c98df477fdf3aeedf3fbb3 to your computer and use it in GitHub Desktop.
Save akku1139/6527ec5333c98df477fdf3aeedf3fbb3 to your computer and use it in GitHub Desktop.
aタグをクリックしたときにリロードせずページ遷移できる
document.addEventListener("click", (e) => {
if(e.target.tagName === "A" && new URL(e.target.href).origin === location.origin) {
fetch(e.target.href).then((r) =>
r.text().then((t) => {
history.pushState(null, null, e.target.href);
document.documentElement.innerHTML = t;
})
);
e.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment